How to consume REST API Services from ABAP with headers

abap rest api with headers

To consume or invoke a REST API service from ABAP with headers you need to pass these headers, on this example Im going to show you how yoy can pass headers and body to a rest service.

This specification show which parameters and values we need to pass to servie.

Heders:

Parameter Value
TokenClienteCert 6860D3B63C3AD9DAD4DD77912E3C4103209720301B26E35EE747629FAB702611

Body:

Parameter Value
username admin
password abc123
grant_type password

Ok, now that we know what need to pass to our service, lets do it in code.

DATA : lo_http_client TYPE REF TO if_http_client.
DATA : l_str_length     TYPE i.
DATA : mime      TYPE w3mimetabtype.

  DATA: l_datos TYPE ty_datos,
        t_datos TYPE STANDARD TABLE OF ty_datos.
  DATA gv_json_output TYPE string.

SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-s01.
PARAMETERS: p_inv TYPE belnr_d DEFAULT '12345'.
SELECTION-SCREEN: END OF BLOCK b1.

START-OF-SELECTION.


*Data variables for storing response in xstring and string
  DATA  : lv_xstring   TYPE xstring,
          lv_string    TYPE string,
          lv_node_name TYPE string.

  CLEAR : lv_xstring, lv_string, lv_node_name.

  DATA  : lv_cdata     TYPE string.
  DATA  : lv_content_length_value TYPE i.

  CLEAR : lv_cdata, lv_content_length_value.

*Pass the URL to get Data
  lv_string = 'http://yourserver.com/prueba.php'.


*Creation of New IF_lo_http_client Object
  cl_http_client=>create_by_url(
  EXPORTING
    url                = lv_string
  IMPORTING
    client             = lo_http_client
  EXCEPTIONS
    argument_not_found = 1
    plugin_not_active  = 2
    internal_error     = 3
    ).

  IF sy-subrc IS NOT INITIAL.
* Handle errors
  ENDIF.

  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.

* lo_http_client->request->set_method( 'POST' ).

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = '~request_method'
      value = 'POST'.

  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = 'content-type:'
      value = 'application/x-www-form-urlencoded'.


  CALL METHOD lo_http_client->request->set_header_field "We pass header and value
    EXPORTING
      name  = 'TokenClienteCert:'
      value = '6860D3B63C3AD9DAD4DD77912E3C4103209720301B26E35EE747629FAB702611'.


  CALL METHOD lo_http_client->request->set_form_field
    EXPORTING
      name  = 'grant_type'
      value = 'password'.

  CALL METHOD lo_http_client->request->set_form_field
    EXPORTING
      name  = 'username'
      value = 'admin'.


  CALL METHOD lo_http_client->request->set_form_field
    EXPORTING
      name  = 'password'
      value = 'abc123'.




  CALL METHOD lo_http_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.



  CALL METHOD lo_http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.





  IF sy-subrc NE 0.
    DATA subrc TYPE sysubrc.
    DATA errortext TYPE string.

    CALL METHOD lo_http_client->get_last_error
      IMPORTING
        code    = subrc
        message = errortext.

    WRITE: / 'communication_error( receive )',
           / 'code: ', subrc, 'message: ', errortext.
    EXIT.
  ELSE.
****Get the response content in Character format
*  content = client->response->get_cdata( ).
  ENDIF.

**  lo_client->receive( ).
**  IF sy-subrc IS NOT INITIAL.
*** Handle errors
**  ENDIF.

END-OF-SELECTION.

**Return the HTTP body of this entity as binary data
*lv_xstring = lo_http_client->response->get_data( ).

*Return the HTTP body of this entity as string data
  DATA(lv_string2) = lo_http_client->response->get_cdata( ).



  BREAK-POINT.


  /ui2/cl_json=>deserialize(
    EXPORTING json = lv_string2
              pretty_name = /ui2/cl_json=>pretty_mode-camel_case
    CHANGING  data = t_datos ).


BREAK-POINT.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up