Express gateway gives 404

2.3k Views Asked by At

I'm the service url as GET REQUEST http://myipaddress:5000/api/Tenant/tenants/{TenantID}

The TenantID will be dynamic

I also have the POST as http://myipaddress:5000/api/Tenant/tenants

In this post request payload is passed in request body.

My gateway config yml file is as below

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  api:
    host: localhost
    paths: '/ip'
  tenant-api:
    host: localhost
    paths: '/api/Tenant/tenants/*'

serviceEndpoints:
  httpbin:
    url: 'https://httpbin.org'
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - api
      - tenant-api
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - proxy:
          - action:
              serviceEndpoint: httpbin 
              changeOrigin: true
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

I get 404 when i try to perform the GET Request via proxy. Also can you let me know how can i add the POST api endpoints in my gatewayconfig.yml

1

There are 1 best solutions below

5
On BEST ANSWER

The first issue is that you have multiple actions in proxy policy

- action:  # this one is always executing and goes to the httpbin
      serviceEndpoint: httpbin 
      changeOrigin: true
- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

remove the first action to make all calls go to tenant-svc

- action:
      serviceEndpoint: tenant-svc 
      changeOrigin: true

This configuration will accept all methods GET,POST whatever on '/api/Tenant/tenants/*' urls

to make Express-Gateway process /api/Tenant/tenants url you can modify api endpoint like:

paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

https://www.express-gateway.io/docs/configuration/gateway.config.yml/apiEndpoints#markdown

I would assume no special handling of GET POST is required. In case you need Gateway to filter only specific methods you can add

methods: 'POST,PUT' 

to your api Endpoint configuration

So final config can look like

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 

serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
policies:
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit
pipelines:
  default:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true

Or you can have multiple API Endpoints to the same pipeline with methods

  tenant-api-1:
    host: localhost
    methods: 'GET' 
    paths: '/api/Tenant/tenants/*'

  tenant-api-2:
    host: localhost
    methods: 'POST' 
    paths:  '/api/Tenant/tenants' 

Update: Multi service usage

http:
  port: 8080
admin:
  port: 9876
  hostname: localhost
apiEndpoints:
  tenant-api:
    host: localhost
    methods: 'GET,POST,PUT' 
    paths: ['/api/Tenant/tenants/*', '/api/Tenant/tenants' ] 
  product-api:
    host: localhost
    paths: ['/api/products/*'] 
serviceEndpoints:
  tenant-svc:
    url: 'http://localhost:5000'
  product-svc:
    url: 'http://localhost:6000'
policies:
  - proxy
pipelines:
  tenant:
    apiEndpoints:
      - tenant-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: tenant-svc 
              changeOrigin: true
  products:
    apiEndpoints:
      - product-api
    policies:
      - proxy:
          - action:
              serviceEndpoint: product-svc 
              changeOrigin: true