<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Calling POST Rest API in ABAP in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838604#M273871</link>
    <description>&lt;P&gt;Please help with any comments. thanks.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Feb 2019 17:04:21 GMT</pubDate>
    <dc:creator>bhakti2</dc:creator>
    <dc:date>2019-02-07T17:04:21Z</dc:date>
    <item>
      <title>Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaq-p/838603</link>
      <description>&lt;P&gt;hi, how do I know the contents of string body to pass to the post method ? The Rest API is an ODATA in ABAP system and I am trying to call it from another SAP system from an ABAP program. I am passing exact same content I receive from the GET method because I know it should work, I have written the API that way. I am getting 415 error in the below code in lv_reponse. How should I populate wf_string1 to call the post method here ? lo_rest_client-&amp;gt;if_rest_resource~post( lo_request ).&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}
.L0S31 {
font-style: italic;
color: #808080;
}
.L0S32 {
color: #3399FF;
}
.L0S33 {
color: #4DA619;
}
.L0S52 {
color: #0000FF;
}
.L0S55 {
color: #800080;
}
.L0S70 {
color: #808080;
}

report yrest_zgw_po_post_api_token2.

data: wf_user type string .
data: wf_password type string .
data: rlength type i,
      txlen   type string.
data: lo_http_client type ref to if_http_client .
data: wf_string type string .
data: wf_string1 type string .
data: wf_proxy type string,
      wf_port  type string,
      lv_url   type string,
      lv_token type string.
data: lo_rest_client type ref to cl_rest_http_client,
      lo_response    type ref to if_rest_entity,
      lv_http_status type i.
start-of-selection .
  lv_url = 'http://xxxxxxxx.xx.xxxxx.com:8000/sap/opu/odata/sap/ZGW_PO_SRV/zekkoekpoSet'.

  cl_http_client=&amp;gt;create_by_url(
       exporting
         url                = lv_url 
       importing
         client             = lo_http_client
       exceptions
         argument_not_found = 1
         plugin_not_active  = 2
         internal_error     = 3
         others             = 4 ).

* - Rest client for token

  create object lo_rest_client exporting io_http_client = lo_http_client.

  lo_rest_client-&amp;gt;if_rest_client~set_request_header( exporting iv_name = 'X-CSRF-Token' iv_value = 'Fetch' ).

  lo_http_client-&amp;gt;propertytype_accept_cookie = if_http_client=&amp;gt;co_enabled.

  lo_http_client-&amp;gt;propertytype_logon_popup = lo_http_client-&amp;gt;co_disabled.

  call method lo_http_client-&amp;gt;send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2.

  call method lo_http_client-&amp;gt;receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  if sy-subrc &amp;lt;&amp;gt; 0.
    message e000(oo) with 'Processing failed !'.
  endif.

  wf_string1 = lo_http_client-&amp;gt;response-&amp;gt;get_cdata( ).

* - Rest client for token

  lo_rest_client-&amp;gt;if_rest_client~get( exceptions others = 1 ).

  lo_response = lo_rest_client-&amp;gt;if_rest_client~get_response_entity( ).

  lv_http_status = lo_response-&amp;gt;get_header_field( '~status_code' ).

  lv_token = lo_response-&amp;gt;get_header_field( 'X-CSRF-Token' ).


*-------Call the PUt method, use the token. code2.

*  free: lo_http_client, lo_rest_client.

  data lo_request type ref to if_rest_entity.

  data lv_response type string.

*  cl_http_client=&amp;gt;create_by_url(
*    exporting  url = lv_url
*    importing  client = lo_http_client
*    exceptions others = 1 ).

*lo_http_client-&amp;gt;request-&amp;gt;set_content_type( 'application/json' ).

*  create object lo_rest_client exporting io_http_client = lo_http_client.

  lo_request = lo_rest_client-&amp;gt;if_rest_client~create_request_entity( ).

  lo_request-&amp;gt;set_header_field( iv_name = 'X-CSRF-Token' iv_value = lv_token ).

  lo_request-&amp;gt;set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).

*lo_request-&amp;gt;set_content_type( iv_media_type = if_rest_media_type=&amp;gt;gc_appl_json ).

  lo_request-&amp;gt;set_string_data( wf_string1 ).

  lo_rest_client-&amp;gt;if_rest_resource~post( lo_request ).

  lo_response = lo_rest_client-&amp;gt;if_rest_client~get_response_entity( ).

  lv_http_status = lo_response-&amp;gt;get_header_field( '~status_code' ).

  lv_response = lo_response-&amp;gt;get_string_data( ).&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Feb 2019 02:02:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaq-p/838603</guid>
      <dc:creator>bhakti2</dc:creator>
      <dc:date>2019-02-07T02:02:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838604#M273871</link>
      <description>&lt;P&gt;Please help with any comments. thanks.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Feb 2019 17:04:21 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838604#M273871</guid>
      <dc:creator>bhakti2</dc:creator>
      <dc:date>2019-02-07T17:04:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838605#M273872</link>
      <description>&lt;P&gt;Hi &lt;A href="https://answers.sap.com/users/6510/bhakti.html"&gt;Bhakti joshi&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I would like to write you that I have the solution but I am having the same problem. &lt;/P&gt;&lt;P&gt;Could you solve this issue. It would be fantastic to know about it. &lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;José Jaimes &lt;/P&gt;</description>
      <pubDate>Fri, 02 Aug 2019 00:47:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838605#M273872</guid>
      <dc:creator>former_member619401</dc:creator>
      <dc:date>2019-08-02T00:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838606#M273873</link>
      <description>&lt;P&gt;hi Jose Jaimes,&lt;/P&gt;&lt;P&gt; I simple concatenated the payload.  I first made it work in the GW client and copied the structure of the payload from the GW client. pls let me know if u stil have doubt.&lt;/P&gt;&lt;P&gt; concatenate '{"d": {"__metadata": {"id": "http://xxxxxxxx.xx.xxxxxx.COM:8000/sap/opu/odata/sap/ZXX_PO_SRV/zekkoekpoSet(Ebeln=''4500097814'',Ebelp=''00030'')",'&lt;BR /&gt;
 '"uri": "http://xxxxxxxx.xx.xxxxxx.COM:8000/sap/opu/odata/sap/ZXX_PO_SRV/zekkoekpoSet(Ebeln=''4500097814'',Ebelp=''00030'')","type": "ZXX_PO_SRV.zekkoekpo"},'&lt;BR /&gt;
 '"Ebeln":"4500097814","Ebelp":"00030","Bukrs":"1111","Bsart":"AN","Matnr":"B-4000001","Werks":"XXXX","Meins":"EA","Menge":"0.000","Loekz":""}}'&lt;BR /&gt;
 into lv_gtwy_payload..&lt;/P&gt;</description>
      <pubDate>Mon, 05 Aug 2019 21:44:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838606#M273873</guid>
      <dc:creator>bhakti2</dc:creator>
      <dc:date>2019-08-05T21:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838607#M273874</link>
      <description>&lt;P&gt;Hi Bhakti,&lt;/P&gt;&lt;P&gt;I am having the same requirement. But in my case the result is always 200.&lt;/P&gt;&lt;P&gt;Can you pls provide the final code which is working for you.&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Sindhu&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 14:43:54 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838607#M273874</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-04-22T14:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838608#M273875</link>
      <description>&lt;P&gt;Myself too facing the same issue.....&lt;/P&gt;&lt;P&gt;any one can post the suggestion.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2020 10:28:56 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838608#M273875</guid>
      <dc:creator>former_member717457</dc:creator>
      <dc:date>2020-12-07T10:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838609#M273876</link>
      <description>&lt;P&gt;Initially we required read(GET) entity set to get payload from metadata....&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2020 10:30:47 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838609#M273876</guid>
      <dc:creator>former_member717457</dc:creator>
      <dc:date>2020-12-07T10:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838610#M273877</link>
      <description>&lt;P&gt;I have implemented service call as below and its working well.&lt;/P&gt;&lt;P&gt;SM59 Destination. HTTP Connection to Ext server&lt;/P&gt;&lt;P&gt;SRMTOSAPCPI&lt;/P&gt;&lt;P&gt;Target host = bpserxxxxxxxxxx.us2.hana.ondemand.com  (server part of service url)&lt;/P&gt;&lt;P&gt;Login = basic auth. Userid and password, don’t send logon tkt, SSL
active&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;    data: lo_http_client type ref to if_http_client,

          lo_rest_client type ref to cl_rest_http_client,

          lo_request     type ref to if_rest_entity,

          lo_response    type ref to if_rest_entity,

          lv_url         type        string,

          lv_body        type        string.


   
cl_http_client=&amp;gt;create_by_destination(


     exporting


       destination              = 'SRMTOSAPCPI'    " Logical destination (specified in
function call)


     importing


       client                   = lo_http_client    " HTTP Client Abstraction


     exceptions


      
argument_not_found       = 1


      
destination_not_found    = 2


      
destination_no_authority = 3


      
plugin_not_active        = 4


       internal_error           = 5


       others                   = 6   ).


* Create REST client instance

    create object lo_rest_client exporting io_http_client = lo_http_client.

* Create request instance

    lo_request = lo_rest_client-&amp;gt;if_rest_client~create_request_entity( ).

* Set HTTP version

    lo_http_client-&amp;gt;request-&amp;gt;set_version( if_http_request=&amp;gt;co_protocol_version_1_0 ).

* Set URL here if using the destination

    if lo_http_client is bound and lo_rest_client is bound.

* Set the URI if any

      cl_http_utility=&amp;gt;set_request_uri(

        exporting

          request = lo_http_client-&amp;gt;request    " HTTP Framework (iHTTP) HTTP Request

          uri     = lv_url      ).             " URI String (in the Form of /path?query-string)

    endif.



    lo_request-&amp;gt;set_content_type( exporting iv_media_type = if_rest_media_type=&amp;gt;gc_appl_json ).

*    this
concatenate string depends on request body of the service.


* first you shud run
it in GW client or any test tool and get the structure of the request body in
json and accordingly formulate it. if your request body is too big for a
concatenate operation then im sorry I don’t know how to form it

    concatenate '{ "number":"' iv_vendor '", "source":"ECC" }' into lv_body.

    lo_request-&amp;gt;set_string_data( lv_body ).



* HTTP Post

    lo_rest_client-&amp;gt;if_rest_resource~post( lo_request ).



* HTTP response

    lo_response = lo_rest_client-&amp;gt;if_rest_client~get_response_entity( ).

    lo_response-&amp;gt;set_content_type( exporting iv_media_type = if_rest_media_type=&amp;gt;gc_appl_json ).



* HTTP return status

    data(http_status)   = lo_response-&amp;gt;get_header_field( '~status_code' ).



* HTTP JSON return string

    data(json_response) = lo_response-&amp;gt;get_string_data( ).

*  convert json to table, navigate
the response XML. This is code is very much specific to response body.

    if json_response is not initial.

      data lv_bpnumber type char10.

      data(lv_str_data) = /ui2/cl_json=&amp;gt;generate( json = json_response ).

      assign lv_str_data-&amp;gt;* to field-symbol(&amp;lt;fs_str_data&amp;gt;).

      if &amp;lt;fs_str_data&amp;gt; is assigned.

        assign component 'DATA' of structure &amp;lt;fs_str_data&amp;gt; to field-symbol(&amp;lt;fs_data&amp;gt;).

        if &amp;lt;fs_data&amp;gt; is bound.

          assign &amp;lt;fs_data&amp;gt;-&amp;gt;* to field-symbol(&amp;lt;ft_data&amp;gt;).

          if &amp;lt;ft_data&amp;gt; is assigned.

            assign component 'businessPartnerCode' of structure &amp;lt;ft_data&amp;gt; to field-symbol(&amp;lt;fs_bpno2&amp;gt;).

            if &amp;lt;fs_bpno2&amp;gt; is assigned.

              assign &amp;lt;fs_bpno2&amp;gt;-&amp;gt;* to field-symbol(&amp;lt;fs_bpno3&amp;gt;).

            endif.

          endif.

        endif.

      endif.

      if &amp;lt;fs_bpno3&amp;gt; is assigned.

        ev_bpnumber = &amp;lt;fs_bpno3&amp;gt;.

      endif.

    endif.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 07 Dec 2020 16:16:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838610#M273877</guid>
      <dc:creator>bhakti2</dc:creator>
      <dc:date>2020-12-07T16:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838611#M273878</link>
      <description>&lt;P&gt;Thank you for sharing the code snippets. Let me try this buddy..&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2020 16:28:10 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838611#M273878</guid>
      <dc:creator>former_member717457</dc:creator>
      <dc:date>2020-12-07T16:28:10Z</dc:date>
    </item>
    <item>
      <title>Re: Calling POST Rest API in ABAP</title>
      <link>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838612#M273879</link>
      <description>&lt;P&gt;hi experts,&lt;/P&gt;&lt;P&gt;After the data is sent can we view the payload to know details which might be helpful in error handling ?&lt;/P&gt;&lt;P&gt;thanks &amp;amp; regards&lt;/P&gt;&lt;P&gt;Prasanth P&lt;/P&gt;</description>
      <pubDate>Mon, 15 Aug 2022 09:09:37 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/calling-post-rest-api-in-abap/qaa-p/838612#M273879</guid>
      <dc:creator>abfg</dc:creator>
      <dc:date>2022-08-15T09:09:37Z</dc:date>
    </item>
  </channel>
</rss>

