Application Development and Automation 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: 
Read only

Call a Web API passing API key

Former Member
0 Likes
1,946

I try to call a web API which returns an XML.

It returns 403 error with message: Incorrect or nonexistent API key.

Whats wrong? The API key is correct as also and I pass it as you see below.

DATA : LR_CLIENT TYPE REF TO IF_HTTP_CLIENT,
       LV_URL TYPE STRING value 'XXX',
       LV_BIN TYPE XSTRING,
       LV_BIN2 TYPE STRING,
       LO_CONV           TYPE REF TO CL_ABAP_CONV_IN_CE,
       LV_RESPONSE       TYPE STRING.

DATA: http_status_code TYPE i,

      status_text TYPE string.






CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
  EXPORTING
    URL          = LV_URL

  IMPORTING
    CLIENT        = LR_CLIENT.



call METHOD LR_CLIENT->request->set_method(
      if_http_request=>CO_REQUEST_METHOD_GET ).




CALL METHOD LR_CLIENT->REQUEST->SET_HEADER_FIELD
  EXPORTING
    NAME  = 'server_protocol'
    VALUE = 'HTTP/1.1'.



CALL METHOD LR_CLIENT->REQUEST->SET_HEADER_FIELD
  EXPORTING
    NAME  = 'url'
    VALUE = LV_URL."LV_URL.



CALL METHOD LR_CLIENT->REQUEST->SET_HEADER_FIELD
  EXPORTING
    NAME  = 'Zxxx-ApiKey'
    VALUE = '8958004E-5BAB-4051-9FCA-62B8704E8336'.



CALL METHOD LR_CLIENT->SEND
  EXCEPTIONS
    HTTP_COMMUNICATION_FAILURE = 1
    HTTP_INVALID_STATE         = 2.


CALL METHOD LR_CLIENT->RECEIVE
  EXCEPTIONS
    HTTP_COMMUNICATION_FAILURE = 1
    HTTP_INVALID_STATE         = 2
    HTTP_PROCESSING_FAILED     = 3.


LV_BIN = LR_CLIENT->RESPONSE->GET_DATA( ).



CALL FUNCTION 'ECATT_CONV_XSTRING_TO_STRING'
  EXPORTING
    IM_XSTRING  = LV_BIN
    IM_ENCODING = 'UTF-8'
  IMPORTING
    EX_STRING   = LV_BIN2.


CALL METHOD LR_CLIENT->response->get_status
  IMPORTING
    code   = http_status_code
    reason = status_text.


LR_CLIENT->CLOSE( ).

1 REPLY 1
Read only

Former Member
0 Likes
633

It's so simple guys! Why can't you answer?

I just want to append an EXTRA header field in HTTP request?

How can I do that without using XI?