cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CL_HTTP_CLIENT URL Parameters with POST method - JSON REST API Consumption

0 Likes
15,840

Hi,

We are trying to consume a JSON based REST API in ABAP using cl_http_client interface.

Following is the request format as prescribed by the API provider:

1. Protocol Header – Authorization: Valid User Name and password in encoded in Base64 in the HTTP header called “Authorization” with a value “BASIC XXXXXXX” where XXXXXXX is the base64 encoded value for the string that contains username and password. For e.g. user0001:Pasww0rd!

2. URL Parameter – “client_id” with the value of the client Id l

3. URL Parameter – “client_secret” with the value of the client secret

Following is a code snippet of what we have tried so far.

data(lv_url) = 'https://api_domain/path?client_id=ABC&client_secret=XYZ'.
cl_http_client=>create_by_url(
 exporting
 url = lv_url
 importing
 client = data(lo_http_client)
 exceptions
 argument_not_found = 1
 plugin_not_active = 2
 internal_error = 3
 others = 4 ).


check lo_http_client is bound.
lo_http_client->propertytype_logon_popup = if_http_client=>co_disabled.<br>
lo_http_client->authenticate(
exporting
 username = 'username'
 password = 'password' ).


lo_http_client->request->set_method(
 exporting
 method = if_http_entity=>co_request_method_get ).


lo_http_client->request->set_content_type(
 exporting
 content_type = if_rest_media_type=>gc_appl_json ).


lo_http_client->request->set_cdata(
 exporting
 data = '{ JSON_Payload }' ).


lo_http_client->send(
 exceptions
 http_communication_failure = 1
 http_invalid_state = 2 ).


check sy-subrc = 0.
lo_http_client->receive(
 exceptions
 http_communication_failure = 1
 http_invalid_state = 2
 http_processing_failed = 3 ).


check sy-subrc = 0.
data(lv_response) = lo_http_client->response->get_cdata( ).

The response I'm receiving for this is:

HTTP/1.0 401 Unauthorized

content-type: application/json

access-control-expose-headers: APIm-Debug-Trans-Id, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-Global-Transaction-IDwww-authenticate: default

x-backside-transport: FAIL FAIL

connection: close

{ "httpCode":"401", "httpMessage":"Unauthorized", "moreInformation":"Client id in wrong location." }

Looking at the error: Client id in wrong location, I doubt whether the fields client_id and client_secret are being sent as url parameters as expected.

data(lv_data) = lo_http_client->request->get_uri_parameter( exporting name = 'client_id' ).

returns empty value, while

data(lv_data) = lo_http_client->request->get_form_field( exporting name = 'client_id' ).

returns the correct value. Request your kind assistance in tracking the problem here.

Accepted Solutions (0)

Answers (0)