ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.38k stars 1.64k forks source link

Headers are not transmitted using Ocelot #1694

Closed skulidropek closed 1 year ago

skulidropek commented 1 year ago

Hello everyone, can someone help me with this problem. I am trying to add my own header so that my client code does not require a token. But no matter how hard I try to do it, nothing comes out. I get an error message that the token is invalid because it does not transmit the header, but if I transmit the header with my token through the client using my API, then everything works

{
  "Routes": [
    /* OPEN AI */
    {
      "DownstreamPathTemplate": "/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "api.openai.com",
          "Port": 443
        }
      ],
      "UpstreamPathTemplate": "/openai/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ],
      "AddHeadersToRequest": {
        "Authorization": "Bearer APIKEY"
      },
      //"AuthenticationOptions": {
      //  "AuthenticationProviderKey": "Bearer",
      //  "AllowedScopes": []
      //},
      //"RouteClaimsRequirement": {
      //  "Role": "Administrator"
      //},
      "SwaggerKey": "openai"
    }

  ],
  "SwaggerEndPoints": [
    {
      "Key": "openai",
      "Config": [
        {
          "Name": "OpenAI API",
          "Version": "v1",
          "Url": "https://www.dropbox.com/scl/fi/qr7vnh7wl21o8ymiohha3/openapi-2.json?rlkey=e6kfeph7bybx1nyp1gyj8ulrk&dl=1"
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5247"
  }
}
raman-m commented 1 year ago

Hi Skuli! Thanks for your interest in Ocelot!

      "AddHeadersToRequest": {
        "Authorization": "Bearer APIKEY"
      },

Are you transforming headers extracting data from claims by this Ocelot feature: Claims to Headers Tranformation ??? Yes or NO?


We have another header-to-header transforms feature: Headers Transformation This feature just renames and forwards your headers.

From your description it is not clear which feature you are trying to use? It seems it is Claims to Headers transformation, but your configuration is incorrect, probably...

skulidropek commented 1 year ago

Hello, thanks for the answer. I'm trying to statically add a token in a file instead of "APITOKEN" like

"AddHeadersToRequest": {
        "Authorization": "Bearer sk-....."
 },
raman-m commented 1 year ago

It seems you cannot use AddHeadersToRequest property because it is designed for dynamic claims data extraction. In that case Claims Transformation feature is not your option! You need to use the Headers Transformation to add a header to request statically!!!

Have I answered your question?

skulidropek commented 1 year ago

Thank you very much, everything works. For some reason, it was written everywhere that it was necessary to use AddHeadersToRequest and I spent 2 days tinkering with this problem. Thanks again