2015 Oct 29 9:59 AM
Hi People,
I am struggling for quite some time now with this issue. I need to send details in the form of json and update it in web service API through POST method.
I have tried a couple of codes which I referred from the net, but no success.
Can you please provide some inputs regarding this?
Thanks.
2015 Oct 30 4:33 AM
2015 Oct 30 4:48 AM
Hi Dharmin
Try this
*" This program is a call to the actual SAP HANA System
DATA: lo_http_client TYPE REF TO if_http_client.
data: lv_http_status type i.
data: lv_len type i.
data: lv_result type string.
data: lv_service type string.
data: lv_request_uri type string.
" Dummy Output Fields
data: lv01 type string.
data: lv02 type string.
data: lv03 type string.
data: lv04 type string.
data: lv05 type string.
data: lv06 type string.
data: lv07 type string.
data: lv08 type string.
data: lv09 type string.
data: lv10 type string.
data: lv11 type string.
data: lv12 type string.
data: lv13 type string.
data: lv14 type string.
data: lv15 type string.
data: lv16 type string.
data: lv17 type string.
data: lv18 type string.
data: lv19 type string.
data: lv20 type string.
"-------------------------------------------------------------------------------------------------------------------------
" The Source for this code was based on code from here: http://scn.sap.com/thread/3307407
" Adjust the URL to that of your service
lv_service = 'http://xxxxxxx/mca/poc/dataservices/odata/catalog_poc.xsodata/createYourServiceName'.
cl_http_client=>create_by_url(
EXPORTING
url = lv_service
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
" Exception Handling
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = '~request_method'
value = 'POST'.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Content-Type'
value = 'application/json; charset=utf-8'."
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = '~uri_scheme_expanded'
value = 'NONE'.
" Pass Login Parameters - Start
CALL METHOD lo_http_client->authenticate(
EXPORTING
" CLIENT = '223'
USERNAME = 'XXX043'
PASSWORD = 'xxxx1').
concatenate '{"PARM1":"200000304",'
'"PARM2":"200000075",'
'"PARM3":"987654321"}'
into lv_request_uri.
*
lv_len = strlen( lv_request_uri ).
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = lv_request_uri
offset = 0
length = lv_len.
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.
CLEAR lv_result .
lv_http_status = lo_http_client->response->get_header_field('~status_code').
if lv_http_status EQ'201'.
" Yippity Doo Daaaaa!
endif.
lv_result = lo_http_client->response->get_cdata( ).
* lv01 = lv_result+0(150).
* lv02 = lv_result+150(150).
* lv03 = lv_result+300(150).
* lv04 = lv_result+450(150).
* lv05 = lv_result+600(150).
* lv06 = lv_result+750(150).
* lv07 = lv_result+900(150).
* lv08 = lv_result+1050(150).
* lv09 = lv_result+1300(140). " Value successfully returned in this segment
* "lv10 = lv_result+1450(150).
ev_result = lv_http_status.
Regards
Arden
2015 Oct 30 5:47 AM
Hi Arden,
Thanks for the reply.
Yes i have build the similar code. The response from my API should be like this -
Response
{
success: true,
message: “10 products added”
}
However what I am receiving in get data is
{
success: true,
Product: “Data should be in JSON format”
}
I am passing data in same fashion as shown by you. But still the same error. Not getting the cause.
A couple of questions here would be -
->My API contains URL with method and username and authorization string. So should I pass this entire url (which I tried but still same error) or only the main webpage URL?
->I even tried by calling main webpage URL and then adding the remaining API path to the '~request uri' field.
-> Also, reg. the username and password details, I need to pass the login credentials for that particular web service right for which I am posting values?
Kindly let me know reg. above queries.
Thanks.
2015 Oct 30 10:33 AM
Hi,
I am getting status code as 200, but don't know why getting the above message in get data from API.
2015 Oct 30 11:15 PM
Hi Dharmin
Is the Data actually posting successfully on the system you're updating?
Regards
Arden
2015 Nov 01 12:15 PM
Hi Arden,
No its not. I am sending the data in Json format itself. Still its showing the message 'Data should be in JSON format'. Not getting anything in debugging as well.
2015 Nov 01 7:19 PM
2015 Nov 02 4:27 AM
Please find below the code..
DATA: lo_http_client TYPE REF TO if_http_client,
lo_rest_client TYPE REF TO cl_rest_http_client,
lv_result_url TYPE string,
lv_result TYPE string,
http_status TYPE string,
lv_body TYPE string,
lv_text TYPE string,
lv_url TYPE string,
wf_string2 TYPE string,
lv_json TYPE string.
data: rlength type i,
txlen type string .
*
CONCATENATE
'http://app.browntape.com/*****************'
'?username=*********************'
'&auth_string=*************************'
INTO
lv_result_url RESPECTING BLANKS.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = lv_result_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.
*-- // Manually writen some JSON data to check if its getting passed.. //
CONCATENATE '{"custom_code":"DDD","readable_name":"Google Nexus 5X 32 G","product_family":"Nexus","opening_stock":"1000",'
'"low_stock_warning_at":"10","default_cost":"35000.00","least_selling_price":"36000.00","currency":"INR",'
'"length_mm":"10.00","breadth_mm":"10.00","height_mm":"10.00","weight_gm":"800.00","image_url":" "}'
INTO wf_string2.
lv_json = wf_string2.
clear :rlength , txlen .
rlength = strlen( wf_string2 ).
move: rlength to txlen .
CALL METHOD lo_http_client->request->set_method( if_http_request=>co_request_method_post ).
"set protocol version
CALL METHOD lo_http_client->request->set_version(
if_http_request=>co_protocol_version_1_1 ).
"content type
CALL METHOD lo_http_client->request->if_http_entity~set_content_type
EXPORTING
content_type = 'application/json; charset=utf-8'.
"Set Header field
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept'
value = '*/*'.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept-Encoding'
value = 'gzip,deflate,sdch'.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept-Language'
value = 'en-US,en;q=0.8'.
*****Set the Data
CALL METHOD lo_http_client->request->set_cdata
EXPORTING
data = lv_json
offset = 0
length = rlength.
****Make the call
lo_http_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2 ).
****Receive the Response Object
lo_http_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3 ).
***Get the response content in Character format
CLEAR lv_result .
lv_result = lo_http_client->response->get_cdata( ).
WRITE:lv_result.
2015 Nov 02 4:40 AM
Hi Dharmin
Start by calculating the correct Offset value, this is a very important value.
Follow my code from above.
Regards
Arden
2015 Nov 02 5:06 AM
Offset value as in are you saying the response returned by get_cdata()? Or can you explain in a little more detail. I am just getting a line as this one .
success: true,
Product: “Data should be in JSON format”
also, I am getting status code as 200 and not 201. Is there any X-CSRF token or something related over here?
2015 Nov 02 11:24 AM
Hi Guys,
My post method works in POSTMAN. Can you let me know how can I map it with SAP? I am only passing the key data and value of new record under BODY tab after passing the URL and selecting POST method.
But when i try in SAP like this
CALL METHOD lo_request->set_form_field
exporting
name = 'data'
value = '[{"custom_code":"1001","stock":"6"}]'.
It does not work.
Any idea?
2015 Nov 03 4:16 AM
Hi
any one can please throw some light on this? I am really stuck and its causing me a lot of time.
Thanks.
2015 Nov 03 8:39 AM
Hi Dharmin
Sorry, my comments on the Offset value were incorrect.
If I was you I would start by reducing the additional parameters you're sending through
"set protocol version
CALL METHOD lo_http_client->request->set_version(
if_http_request=>co_protocol_version_1_1 ).
"Set Header field
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept'
value = '*/*'.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept-Encoding'
value = 'gzip,deflate,sdch'.
CALL METHOD lo_http_client->request->set_header_field
EXPORTING
name = 'Accept-Language'
value = 'en-US,en;q=0.8'.
I'm not sure if these are absolutely necessary
Regards
Arden
2015 Nov 03 9:19 AM
Hi,
I tried passing only the method, content type and set_cdata() and get_cdata().
But no luck.
2015 Nov 03 9:51 AM
Print out your code, then print out my code.
Compare the differences...
If they are looking identical then you may have a token issue to get around. See if the system you're posting too can switch off any token requirements.
But seeing as your Postman usage works (are you definitely sure...entries in the table??) this may not be the case.
Regards
Arden