cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Calling POST Rest API in ABAP

bhakti2
Active Participant
0 Likes
29,926

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->if_rest_resource~post( lo_request ).

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=>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->if_rest_client~set_request_header( exporting iv_name = 'X-CSRF-Token' iv_value = 'Fetch' ).

  lo_http_client->propertytype_accept_cookie = if_http_client=>co_enabled.

  lo_http_client->propertytype_logon_popup = lo_http_client->co_disabled.

  call method lo_http_client->send
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2.

  call method lo_http_client->receive
    exceptions
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3.
  if sy-subrc <> 0.
    message e000(oo) with 'Processing failed !'.
  endif.

  wf_string1 = lo_http_client->response->get_cdata( ).

* - Rest client for token

  lo_rest_client->if_rest_client~get( exceptions others = 1 ).

  lo_response = lo_rest_client->if_rest_client~get_response_entity( ).

  lv_http_status = lo_response->get_header_field( '~status_code' ).

  lv_token = lo_response->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=>create_by_url(
*    exporting  url = lv_url
*    importing  client = lo_http_client
*    exceptions others = 1 ).

*lo_http_client->request->set_content_type( 'application/json' ).

*  create object lo_rest_client exporting io_http_client = lo_http_client.

  lo_request = lo_rest_client->if_rest_client~create_request_entity( ).

  lo_request->set_header_field( iv_name = 'X-CSRF-Token' iv_value = lv_token ).

  lo_request->set_header_field( iv_name = 'X-Requested-With' iv_value = 'X' ).

*lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).

  lo_request->set_string_data( wf_string1 ).

  lo_rest_client->if_rest_resource~post( lo_request ).

  lo_response = lo_rest_client->if_rest_client~get_response_entity( ).

  lv_http_status = lo_response->get_header_field( '~status_code' ).

  lv_response = lo_response->get_string_data( ).
View Entire Topic
bhakti2
Active Participant
0 Likes

hi Jose Jaimes,

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.

concatenate '{"d": {"__metadata": {"id": "http://xxxxxxxx.xx.xxxxxx.COM:8000/sap/opu/odata/sap/ZXX_PO_SRV/zekkoekpoSet(Ebeln=''4500097814'',Ebelp=''00030'')",'
'"uri": "http://xxxxxxxx.xx.xxxxxx.COM:8000/sap/opu/odata/sap/ZXX_PO_SRV/zekkoekpoSet(Ebeln=''4500097814'',Ebelp=''00030'')","type": "ZXX_PO_SRV.zekkoekpo"},'
'"Ebeln":"4500097814","Ebelp":"00030","Bukrs":"1111","Bsart":"AN","Matnr":"B-4000001","Werks":"XXXX","Meins":"EA","Menge":"0.000","Loekz":""}}'
into lv_gtwy_payload..

Former Member
0 Likes

Hi Bhakti,

I am having the same requirement. But in my case the result is always 200.

Can you pls provide the final code which is working for you.

Thanks

Sindhu

former_member717457
Participant
0 Likes

Initially we required read(GET) entity set to get payload from metadata....