Consume REST API and Send Custom Headers in OnBeforeRequest

Consume REST API and Send Custom Headers in OnBeforeRequest

  

I have a REST API I can consume and can provide the headers to this as an input parameter:

 I have used the OnBeforeRequest action of this API to prefill a header (Authorization):

and this is then assigned to the customised request that gets sent back out:

However, I am now struggling to add a second header to this OnBeforeRequest.

I have read the documentation at https://success.outsystems.com/Documentation/10/Extensibility_and_Integration/REST/Consume_REST_APIs/Simple_Customizations and the instructions just say "Add your own logic to customize the request."

Can anyone provide a bit of direction as to how I should append a second, third or fourth header please?


Hi Russell,

Please let me know if understood it correctly, 

1. you are consuming some rest api 

2. you are passing some custome header information in it.

3. Now you want to pass some different information in the header for same api method? or you have multiple method in your same api?

4. If you have same api and then its header information must be same, only parameters might be different.

5. If you are consuming different rest api then you can consume it separately by clicking on rest and select consume api and you will have separate on before request for it.

Hope it helps.

Regards

Manish jawla 

Hi Manish, I have one gatewayAPIAccount URL, but there are different methods that I can access. In the below screenshot you can see GetSummary and GetDetails:

You can see that both methods take a ContentType, which will always be "application/json" so rather than have to provide that as an input parameter, i am trying to add it as a header before the request is made in OnBeforeRequest. I can get this working for one header, but don't know how to add multiple headers to the customised request.

Hi Russell,

I got your point but usually we use single on before request for common settings of same api like api url/username/password/tokens not for some parameters which will have different values in every request. 

if ContentType value will remian same for all the methods then you dont need to have separate on before request header, same will work for different methods as well but if values are different then

You can have 2 work arounds:

1. you need to have 1 more parameter which will decide whether which type of ContentYype you want to pass and save these ContentType in your DB static entity.

2. You can consume second method separately but again it is overhead.

Best way would recommend is either keep it as an input parameter or have some other parameter which will help you in customizing the same on before request. 

regards,

Manish jawla

Hi Russel,

Are you sure you're appending the headers? You're assigning to the current of the list, make sure you assign to a record and after append it to the list of headers.

Could this be the issue?

Cheers


Henrique Batista wrote:

Hi Russel,

Are you sure you're appending the headers? You're assigning to the current of the list, make sure you assign to a record and after append it to the list of headers.

Could this be the issue?

Cheers


Hi Henrique, it is the appending part that is causing me the blind spot.

I now have multiple methods, all from the same API, all sharing the same OnBeforeRequest. Although I am assigning to the current value, this seems to work for my creating an "Authorization" header.

I would also like to add a second header of "Content-Tyoe" where the value will always be "application/json" but cannot work out how to append this to the headers. The example OnbeforeRequest shows only an Assign node of CustomizedRequest = Request.

To get around this, every one of my REST API methods now takes an input parameter of "contenttype" and I have to provide this input parameter in every server action. This is what I was trying to avoid, so if you can point me the right way i would be ever so grateful!

Russel, you can use the REST Extender component to set the content type.

Hanno

Thank you, Hanno. I have downloaded your extension and will take a look. I will mark your answer as solution for now, as it seems that there isn't a native way to add mujltiple headers in OutSystems.

Thank you for your help :)

Russell Codd wrote:

Henrique Batista wrote:

Hi Russel,

Are you sure you're appending the headers? You're assigning to the current of the list, make sure you assign to a record and after append it to the list of headers.

Could this be the issue?

Cheers


Hi Henrique, it is the appending part that is causing me the blind spot.

I now have multiple methods, all from the same API, all sharing the same OnBeforeRequest. Although I am assigning to the current value, this seems to work for my creating an "Authorization" header.

I would also like to add a second header of "Content-Tyoe" where the value will always be "application/json" but cannot work out how to append this to the headers. The example OnbeforeRequest shows only an Assign node of CustomizedRequest = Request.

To get around this, every one of my REST API methods now takes an input parameter of "contenttype" and I have to provide this input parameter in every server action. This is what I was trying to avoid, so if you can point me the right way i would be ever so grateful!


In system there's a server action called listappend.
You can use this to append an element to the Request.Headers list.
Ofcourse the element you append has to be a NameValue variable.

Since headers is a list, it can hold (as it should!) multiple items. I don't think there is a need for an extra extension just to add multiple headers....

Hi Joey, would you be able to provide a screenshot of that, especially showing where it should go in relation to the CustomizedRequest = request node?

Solution





This way you can choose to add even more headers.

Solution

Joey, that looks like it might be perfect, thank you so much. I will try this today and come back to the thread. Thank you so much.

No problem, I hope this is working for you!

Good one, Joey.