Adding header to request on IBM API Connect

808 Views Asked by At

When receiving a request on API Connect I want to add a header to the subsequent call that it makes to my backend, I've tried using the set-variable policy like so:

execute:
  - set-variable:
      title: SetCustomHeader
      actions:
        - value: xxx
          set: X-HEADER-NAME

And using the Gateway Script:

  - gatewayscript:
      title: gatewayscript
      version: 1.0.0
      source: 'apim.setvariable(''X-HEADER-NAME'', ''xxx'');'

I've tried having the action as either set or add, and I've tried setting the variable name as just the name of the header, and message.headers.X-HEADER-NAME.

According to questions like this one, as well as IBM documentation either one should work, but when logging the request headers on my backend I still don't see the header.

What am I missing?

1

There are 1 best solutions below

0
Adarsh Thakur On

You need to mention the apim context variables in order to make api connect understand that you want to set the header value. so the value will be :

- gatewayscript:
      title: gatewayscript
      version: 1.0.0
      source: 'apim.setvariable('message.headers.name' ,'value','set');'

Where "name" is the name of the header and "value" is the value of header to be set.

You can find all the api connect context variables here : https://www.ibm.com/docs/en/api-connect/10.0.x?topic=reference-api-connect-context-variables