on ‎2021 Feb 12 5:30 AM
Hello,
I have a requirement where i am accessing two API'S , first API return Bearer Token and for accessing Second API for authentication instead user id and password the bearer token will be used which is received from First API call
Let me know how to call API with Bearer Token.
Thanks
Vikash
Request clarification before answering.
just add Header
Authorization: Bearer xxxxxxxTOKENxxxx
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Where do i need to add this authorization?
i have written this code
data: lo_request1 type ref to if_rest_entity.
* lo_rest_client type ref to cl_rest_http_client.
CLEAR:LV_GITHIB_url .
LV_GITHIB_url = 'https://xxxxxxxxxxxxxxxxx'.
cl_http_client=>create_by_url(
EXPORTING
url = LV_GITHIB_url
ssl_id = 'ANONYM'
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF sy-subrc <> 0.
CLEAR ev_response.
RETURN.
ENDIF.
DATA(lo_rest_client) = new cl_rest_http_client( IO_HTTP_CLIENT = http_client ).
http_client->propertytype_logon_popup = http_client->co_disabled.
lo_request1 = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request1->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request1->set_header_field( iv_name = 'X-CSRF-Token' iv_value = ev_reason1 ).
lo_request1->set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).
cl_http_utility=>set_request_uri( request = http_client->request
uri = LV_GITHIB_url ).
lo_request1->set_string_data( wf_string1 ).
lo_rest_client->if_rest_resource~post( lo_request1 ).
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
lv_http_status = lo_response->get_header_field( '~status_code' ).
lv_response = lo_response->get_string_data( ).
write:/ lv_http_status.
write:/ lv_response.
Hello sergei.haller.u-niq,
yes i am using the correct Token and i tested it on postman and its working there,
if i donot use http_client->propertytype_logon_popup = http_client->co_disabled. this code login screen popup for user id nd password even after using below code.
LV_GITHIB_url = 'https://xxxxxxxxxxxxxxxxxxxxxxxx'.
cl_http_client=>create_by_url(
EXPORTING
url = LV_GITHIB_url
ssl_id = 'ANONYM'
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF sy-subrc <> 0.
CLEAR ev_response.
RETURN.
ENDIF.
DATA(lo_rest_client) = new cl_rest_http_client( IO_HTTP_CLIENT = http_client ).
lo_request1 = lo_rest_client->if_rest_client~create_request_entity( ).
*http_client->propertytype_logon_popup = http_client->co_disabled.
lo_request1->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request1->set_header_field( iv_name = 'Authorization' iv_value = |Bearer { ev_reason1 }| ).
lo_request1->set_header_field( iv_name = 'X-CSRF-Token' iv_value = ev_reason1 ).
lo_request1->set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).
cl_http_utility=>set_request_uri( request = http_client->request
uri = LV_GITHIB_url ).
lo_request1->set_string_data( wf_string1 ).
lo_rest_client->if_rest_resource~post( lo_request1 ).
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
lv_http_status = lo_response->get_header_field( '~status_code' ).
lv_response = lo_response->get_string_data( ).
| User | Count |
|---|---|
| 11 | |
| 7 | |
| 6 | |
| 5 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.