Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to send custom http header element(API Key) using CL_HTTP_CLIENT

0 Kudos

Hello,

I am trying to do a REST API Call(GET) from ECC to a third party system and need to pass the API Key as a custom HTTP Header element. We are unable to do this via PI's REST Adapter as we are still on 7.4 SP 9.

I am using CL_HTTP_CLIENT class and added the custom HTTP Header element via set_header_field method but I get 401 Unauthorized. The same request is going through fine from postman.

Our ECC is on 7.4 SP7. Can't we pass custom http headers from ECC on this SP level?

Thanks,

Sarat

6 REPLIES 6

Sandra_Rossi
Active Contributor

No issue with HTTP headers. Certificate issue? You may activate the SMICM trace with level 2 to check the contents of the outgoing HTTP message. More information here: https://blogs.sap.com/2014/12/09/analyzing-authorization-problems-http-401-with-icm-and-security-aud...

0 Kudos

Hi Sandra,

I have increased the trace via SMICM and found the root cause. Somehow, the API key is getting truncated in the HTTP request. I checked the "Value" field of the method "set_header_field" of "request" attribute of cl_http_client class that I am using to set the header element and the variable is of type "String". So not sure how my API Key data is getting truncated.

Thoughts?

Thanks,

Sarat

0 Kudos

Please use the COMMENT button. ANSWER is for proposing a solution. You may also convert your answer into a comment.

0 Kudos

I don't know if there's a limit. How long is your API key and how many characters are effectively passed?

0 Kudos

Try confirm if you are filling whole length to set_header_field value. Maybe you are using some char length limited variable to fill it? Also how long is the key?

-- Tomas --

Looks like SAP is NOT truncating the API Key. However, it is converting the API Key Field Name(one of the HTTP Header elements) to lowercase and it is causing the RESTful API call to fail.

I know that field names are case-insensitive as per RFC 2616, but, vendor needs it in camel case. Is there a way to fix this in SAP?

Thanks,

Sarat