0

I use Ocelot for api gateway and encountered problem with configuration. I'm trying to replace part of the 'Location' header value that comes from api (when api returns CreatedAtRoute(...) result). See part of ocelot.json:

// ...
{
  "UpstreamPathTemplate": "/hotels",
  "UpstreamHttpMethod": [ "Post", "Put" ],
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "<api_host>",
      "Port": 80
    }
  ],
  "DownstreamPathTemplate": "/api/hotels",
  "DownstreamHeaderTransform": {
    "Location": "{DownstreamBaseUrl}/api, {BaseUrl}" // the issue here
  }
},
// ...

What I want in headers when posting new object (42 is just random number):

Location: http://<gateway_host>/hotels/42

What I actually get:

Location: http://<api_host>/api/hotels/42

May be it doesn't matter but both apps are running in docker.

Elvis
  • 108
  • 1
  • 7

1 Answers1

0

First, I want to emphasize it happens also using regular process run - not only in docker environment.

The request I use is POST to: https://localhost:7051/api/project-manager/examples?exampleName=example1 (/api/project-manager is a prefix that Ocelot adds). and the (bad) location returned to Ocelot's client is https://localhost:2261/examples/example1 same as I got when tried directly to the web api without the GW:

 content-length: 2868 
 content-type: application/json; charset=utf-8 
 date: Mon,01 Aug 2022 11:27:23 GMT 
 location: https://localhost:2261/examples/example1 
 server: Kestrel 

While I have expected to get location: https://localhost:7051/api/project-manager/examples/example1

The lack is in both - port and the path transformation: the /api/project-manager prefix ocelot is configured to add.


As for the solution, I saw another discussion which provides a solution. So this discussion might be a duplication of: How add Gateway base path on CreatedAtRoute Location Header - URL Rewrite

It helped in my case by adding the following DownstreamHeaderTransform to config:

      "UpstreamPathTemplate": "/api/project-manager/{everything}",
      "DownstreamHeaderTransform": {
        "Location": "{DownstreamBaseUrl}, {BaseUrl}/api/project-manager"
      },

Which produced a correct (transformed) location:

 access-control-allow-origin: * 
 content-length: 2864 
 content-type: application/json; charset=utf-8 
 date: Tue,02 Aug 2022 07:52:25 GMT 
 location: https://localhost:7051/api/project-manager/examples/example1 
 server: Kestrel