REST API Service From ABAP

abap rest api with headers
DATA : http_client TYPE REF TO if_http_client.
DATA : l_str_length     TYPE i.
DATA : mime      TYPE w3mimetabtype.

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.

*HTTP Client Abstraction
  DATA  lo_client TYPE REF TO if_http_client.

*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.

*Pass the URL to get Data
**  lv_string = |http://myservice.net/QP/Wic/API/Token{ p_inv }|.
lv_string = 'http://.azurewebsites.net/QP/API/Token'.

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

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

  http_client->propertytype_logon_popup = http_client->co_disabled.
  http_client->request->set_method( 'POST' ).


  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'Content-Type:'
      value = 'application/x-www-form-urlencoded'.


  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'grant_type:'
      value = 'password'.


    CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'username:'
      value = 'myubname@myserver.com'.


  CALL METHOD http_client->request->set_header_field
    EXPORTING
      name  = 'password:'
      value = 'abc123'.


*Structure of HTTP Connection and Dispatch of Data
  http_client->send( ).
*Receipt of HTTP Response

      CALL METHOD 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 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 = http_client->response->get_data( ).

*Return the HTTP body of this entity as string data
DATA(lv_string2) = http_client->response->get_cdata( ).
data: cadena2 TYPE string.
  cadena2 = lv_string2+255(89).

BREAK-POINT.

      http_client->close( ).

      l_str_length = xstrlen( lv_xstring ).

      CALL FUNCTION 'RSFO_XSTRING_TO_MIME'
        EXPORTING
          c_xstring = lv_xstring
          i_length  = l_str_length
        TABLES
          c_t_mime  = mime.

 

Leave a Reply

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

Go up