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

How get response from external Rest API service

0 Likes
11,975

Hello, I have a rest service API, if Invoke this service from Postman I get follow response:

But If I invoke this services from ABAP, I get:

Can Anyone help me? My source code below, I have based on example: REST API Service From ABAP

REPORT z_pruebas.
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://weeqp.myserv.net/QP/Wic/API/Token{ p_inv }|.
lv_string = 'http://sds.azrewebsitesddI/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 = '[email protected]'.


  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( ).


      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.
2 REPLIES 2
Read only

geert-janklaps
SAP Mentor
SAP Mentor
7,586

Hi,

Did you try using following code:

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

In stead of:

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

After changing the code you can also remove this part, since you won't need to convert the xstring:

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.

Best regards,

Geert-Jan Klaps

Read only

0 Likes
7,586

Hello 8d8214c7f9734f45be69f95cc0d5aeee I did that you tod me and I get the string with this value:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">##<HTML><HEAD><TITLE>Length Required</TITLE>##<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>##<BODY><h2>Length Required</h2>##<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>##</BODY></HTML>##

As I can See, I get an HTML, I dont know why, and message its:

The request must be chunked or have a content length

Do you know about this message?