on 2019 Feb 07 2:02 AM
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( ).
Request clarification before answering.
I have implemented service call as below and its working well.
SM59 Destination. HTTP Connection to Ext server
SRMTOSAPCPI
Target host = bpserxxxxxxxxxx.us2.hana.ondemand.com (server part of service url)
Login = basic auth. Userid and password, don’t send logon tkt, SSL active
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=>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->if_rest_client~create_request_entity( ).
* Set HTTP version
lo_http_client->request->set_version( if_http_request=>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=>set_request_uri(
exporting
request = lo_http_client->request " HTTP Framework (iHTTP) HTTP Request
uri = lv_url ). " URI String (in the Form of /path?query-string)
endif.
lo_request->set_content_type( exporting iv_media_type = if_rest_media_type=>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->set_string_data( lv_body ).
* HTTP Post
lo_rest_client->if_rest_resource~post( lo_request ).
* HTTP response
lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
lo_response->set_content_type( exporting iv_media_type = if_rest_media_type=>gc_appl_json ).
* HTTP return status
data(http_status) = lo_response->get_header_field( '~status_code' ).
* HTTP JSON return string
data(json_response) = lo_response->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=>generate( json = json_response ).
assign lv_str_data->* to field-symbol(<fs_str_data>).
if <fs_str_data> is assigned.
assign component 'DATA' of structure <fs_str_data> to field-symbol(<fs_data>).
if <fs_data> is bound.
assign <fs_data>->* to field-symbol(<ft_data>).
if <ft_data> is assigned.
assign component 'businessPartnerCode' of structure <ft_data> to field-symbol(<fs_bpno2>).
if <fs_bpno2> is assigned.
assign <fs_bpno2>->* to field-symbol(<fs_bpno3>).
endif.
endif.
endif.
endif.
if <fs_bpno3> is assigned.
ev_bpnumber = <fs_bpno3>.
endif.
endif.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
61 | |
8 | |
7 | |
6 | |
6 | |
4 | |
4 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.