Skip to Content
avatar image
Former Member

How to: RESTFUL POST XML document

Hi

I tried to call an external RESTful webservice API with POST method, and i'm supposed to send an XML document.

Below is my code. I always have an error response: Input file must be an xml document. I thought I have passed the XML string, but still getting this error. Anyone has any idea?

data: str_host type string,
        str_api  type string.


  DATA:
    lo_client      TYPE REF TO if_http_client,
    lv_result_url  TYPE string,
    lv_bin         TYPE xstring,
    lo_conv        TYPE REF TO cl_abap_conv_in_ce,
    lv_response    TYPE string,
    lo_rest_client TYPE REF TO if_rest_client,
    lo_rest_entity TYPE REF TO if_rest_entity.


  data:
    l_code           type sysubrc,
    l_message_string type string,
    l_code_string    type string.


  data: lv_string       type string,
        lv_xstring      type xstring,
        ls_match        TYPE match_result,
        lv_offset_start TYPE sy-tabix,
        lv_offset_end   TYPE sy-tabix,
        lv_model_code   TYPE char12,
        lv_length       TYPE sy-tabix.


  str_host = str_url.
  str_api = api_path.
  concatenate str_host str_api into lv_result_url.


  CALL METHOD cl_http_client=>create_by_destination
    EXPORTING
      destination              = 'ADAPTIVE'
    IMPORTING
      client                   = lo_client
    EXCEPTIONS
      argument_not_found       = 1
      destination_not_found    = 2
      destination_no_authority = 3
      plugin_not_active        = 4
      internal_error           = 5
      OTHERS                   = 6.


  concatenate 'http://' lv_result_url into lv_result_url.


  CALL METHOD cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
  call method lo_client->request->set_method( if_http_request=>CO_REQUEST_METHOD_POST ).


  CALL METHOD lo_client->request->set_header_field
    EXPORTING
      name  = 'Accept'
      VALUE = 'text/xml'.


  CALL METHOD lo_client->request->set_content_type
    EXPORTING
      content_type = 'text/xml'.


lv_string = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>'.
...

  if lv_string is not initial.
    CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
      EXPORTING
        text   = lv_string
      IMPORTING
        buffer = lv_xstring
      EXCEPTIONS
        failed = 1
        OTHERS = 2.

    CALL METHOD lo_client->request->set_data
      EXPORTING
        data = lv_xstring.
  endif.


* STEP-5 :  SEND HTTP REQUEST
  CALL METHOD lo_client->send
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2.


* STEP-6 :  GET HTTP RESPONSE
  CALL METHOD lo_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.


  if sy-subrc = 0.
    lv_bin = lo_client->response->get_data( ).
    lo_conv = cl_abap_conv_in_ce=>create( input = lv_bin ).
    lo_conv->read( IMPORTING data = lv_response ).
  endif.

  lo_client->close( ).
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

0 Answers