Solved

How to pass HTTP Request header for REST Service

  • 7 March 2019
  • 2 replies
  • 913 views

Hi All,

 

We are trying to integrate with a REST Service API from K2 cloud. The REST endpoint uses the API Key in the header for Authorization. Can you please let us know the ways to pass the API Key in the HTTP Request Header.

 

Thanks and Regards,

Thileeban.C

icon

Best answer by boringNerd 8 March 2019, 04:43

Hi,


 


Are you able to register the REST service instance first with just the Descriptor Location to your Swagger definition? If so, you should see a service object generated under the REST service instance called HttpHeader. It is placed under System Types.


 



 


You can create a SmartObject from the HttpHeader service object, then use that SmartObject to generate a Http Header serialized JSON string that you can use.


 


If you cannot generate a HttpHeader SmartObject for some reason, this is an example of a HttpHeader serialized string containing exactly one header property and its value:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty","Value":"Value"}



 


You just need to replace the header property and value that I bold with your own property and value.


 


This is another example which contains multiple header properties and values:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","$values":[{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty2","Value":"Value2"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty3","Value":"Value3"}]}


 


This one is a little tricky. If you want to add multiple header properties, you need to place them in an array. I have bold the array for your convenience. Inside the array, you should see multiple JSON objects with the same definition. Here is one of them:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"}


 


If you want to add more header properties, just append the JSON object to the array, just as I have done below:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","$values":[{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty2","Value":"Value2"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty3","Value":"Value3"}, {"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty4","Value":"Value4"}]}


 


Quick tip: To help you better visualize the JSON strings, you can go to jsonviewer.stack.hu, paste the JSON strings in, then click on Viewer.


 



View original

2 replies

Badge +13

Hi,


 


Are you able to register the REST service instance first with just the Descriptor Location to your Swagger definition? If so, you should see a service object generated under the REST service instance called HttpHeader. It is placed under System Types.


 



 


You can create a SmartObject from the HttpHeader service object, then use that SmartObject to generate a Http Header serialized JSON string that you can use.


 


If you cannot generate a HttpHeader SmartObject for some reason, this is an example of a HttpHeader serialized string containing exactly one header property and its value:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty","Value":"Value"}



 


You just need to replace the header property and value that I bold with your own property and value.


 


This is another example which contains multiple header properties and values:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","$values":[{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty2","Value":"Value2"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty3","Value":"Value3"}]}


 


This one is a little tricky. If you want to add multiple header properties, you need to place them in an array. I have bold the array for your convenience. Inside the array, you should see multiple JSON objects with the same definition. Here is one of them:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"}


 


If you want to add more header properties, just append the JSON object to the array, just as I have done below:


 


{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader[], SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","$values":[{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty1","Value":"Value1"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty2","Value":"Value2"},{"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty3","Value":"Value3"}, {"$type":"SourceCode.SmartObjects.Services.Endpoints.Common.HttpHeader, SourceCode.SmartObjects.Services.Endpoints.Common, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"HeaderProperty4","Value":"Value4"}]}


 


Quick tip: To help you better visualize the JSON strings, you can go to jsonviewer.stack.hu, paste the JSON strings in, then click on Viewer.


 



Userlevel 2
Badge +4

Hi,

 

I have the header string but how do I pass it to the REST call? I have a smartObject, which makes the REST call but it has no parameters to pass the authorization header in.

 

I just found out you set "Add HTTP Request Header Property to Methods" to true when creating/updating the REST methods. This allows you to pass HTTP headers.  

 

Thanks

Reply