JSON POST & PUT (single & batch) on NetWeaver Gateway
I recently faced some issues while trying to update/create entities with JSON on Gateway. Mainly this was due to my lack of knowledge of the POST and PUT request format. In this blog I will describe how it worked for me for both single as well as batch update/create.
The easiest way to start with, is to make a GET call on the same collection in case of POST or on the same entity for a PUT. One more reason to make this call is to get the x-csrf-token and the sap-XSRF_GIQ_100 cookie (depends on which GW is being used) which will be required for the POST or PUT calls later.
The cookie was required to be added as a header because I was accessing the GW from a different domain (different location) and thus the browser was not sending the cookie back with the subsequent requests automatically.
GET Request
Let’s look at the Get call, the related headers and the response below
URL
Headers
Name:x-csrf-token Value:fetch
Response
{“d”:{“results”:[{“__metadata”:{“id”:”http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection(‘00000001‘)”,”uri”:”http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection(‘00000001‘)”,”type”:”RMTSAMPLEFLIGHT.Travelagency”},”agencynum”:”00000001″,”NAME”:”DEBU”,”STREET”:””,”POSTBOX”:””,”POSTCODE”:””,”CITY”:””,”COUNTRY”:””,”REGION”:””,”TELEPHONE”:””,”URL”:””,”LANGU”:””,”CURRENCY”:””,”mimeType”:”text/html”}]}}
Now once we have an entity, we can modify it to be used for the subsequent PUT and POST requests described below.
PUT Request
URL
Headers
Name:Content-Type Value:application/json; charset=utf-8
Name:x-csrf-token Value:vDXb3IhLuc-AHYojkfMG9A==
Name:cookie Value:sap-XSRF_GIQ_100=vDXb3IhLu…
Request Body
{“d”:{“agencynum”:”00000001″,”NAME”:”DEBU”,”STREET”:””,”POSTBOX”:””,”POSTCODE”:””,”CITY”:””,”COUNTRY”:””,”REGION”:””,”TELEPHONE”:””,”URL”:””,”LANGU”:””,”CURRENCY”:””,”mimeType”:”text/html”}}
Note: The “results” and “__metadata” need to be removed to form the request body.
POST Request
URL
http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection
Headers
Name:Content-Type Value:application/json; charset=utf-8
Name:x-csrf-token Value:vDXb3IhLuc-AHYojkfMG9A==
Name:cookie Value:sap-XSRF_GIQ_100=vDXb3IhLu…
Request Body
{“d”:{“agencynum”:”00022222″,”NAME”:”DEBU”,”STREET”:””,”POSTBOX”:””,”POSTCODE”:””,”CITY”:””,”COUNTRY”:””,”REGION”:””,”TELEPHONE”:””,”URL”:””,”LANGU”:””,”CURRENCY”:””,”mimeType”:”text/html”}}
Batch processing
URL
http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/$batch
Headers
Name:x-csrf-token Value:wN9Ebbp1BDWprGXDtL-TXA==
Name:cookie Value:sap-XSRF_GIQ_100=wN9Ebbp1BDWprGXDt…..
Name:Content-Type Value:multipart/mixed; boundary=batch
Request Body
–batch
Content-Type: multipart/mixed; boundary=changeset
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
PUT TravelagencyCollection(‘00055557’) HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 198
{“d”:{“agencynum”:”00055557″,”NAME”:”汉字漢”,”STREET”:””,”POSTBOX”:””,”POSTCODE”:””,”CITY”:””,”COUNTRY”:””,”REGION”:””,”TELEPHONE”:””,”URL”:””,”LANGU”:””,”CURRENCY”:””,”mimeType”:”text/html”}}
–changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
POST TravelagencyCollection HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 198
{“d”:{“agencynum”:”00044470″,”NAME”:”再見”,”STREET”:””,”POSTBOX”:””,”POSTCODE”:””,”CITY”:””,”COUNTRY”:”IND”,”REGION”:””,”TELEPHONE”:””,”URL”:””,”LANGU”:””,”CURRENCY”:””,”mimeType”:”text/html”}}
–changeset–
–batch–
Note:
If any one of the two request fails to succeed, both fails.
The content length must be accurate or more, if it is less, then you will get an exception stating “Error while parsing an XML stream: ‘unexpected end of string'”
Helpful links
Hope this helps!
Very Helpful!
I agree very helpful blog. I love to see HTTP in the raw as it always explains more than just a library call. Keep it up 🙂
Very helpful document. Saved me a lot of time.
Excellent
Thanks,
Syam
Hi Debdutt,
very helpful indeed.
Best Regards,
Andre
Nice elaboration.
Regards,
Pavan G
Hello Sir,
I have tried in my application where the entity type is media type. I tried pushing the json data as payload, it gives 202 accepted but, message: Empty response body: Check your batch request.
https://<system port>/sap/opu/odata/SAP/Ztest_SRV/$batch
this is the payload:
header:
csrf token: value
and content type: multipart/mixed;bounday=batch
body:
--batch
Content-Type: multipart/mixed; boundary=changeset
--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
POST SurveyResponseSet HTTP/1.1
Content-Type: application/json;charset=utf-8
{"Name":"{\"a\":{\"b\":{\"c\":{\
"d\":{\"e\":{\"survey_id\":\"73579975\",\"title\":\
"second_survey\",\"date_modified\":\"2016-02-22 11:13:00\",\
"analysis_url\":\"https://www.xys.com/MySurvey_Resp
onses.aspx?sm=vDM6KGcZ49pQXaf3KzRzcxgsSPCyc8qacWfE6jXg1B4_3D
\",\"language_id\":\"1\",\"num_responses\":\"3\",\"date_crea
ted\":\"2016-01-07 04:12:00\",\"question_count\":\"1\"}}}},\
"surveydetails\":{\"data\":{\"pages\":{\"page_id\":\"2269089
06\",\"questions\":{\"type\":{\"subtype\":\"vertical\",\"nam
e\":\"\",\"family\":\"single_choice\"},\"answers\":[{\"text\
":\"Red\",\"visible\":\"true\",\"position\":\"1\",\"type\":\
"row\",\"answer_id\":\"9755912582\"},{\"text\":\"Green\",\"v
isible\":\"true\",\"position\":\"2\",\"type\":\"row\",\"answ
er_id\":\"9755912583\"},{\"text\":\"Yellow\",\"visible\":\"t
rue\",\"position\":\"3\",\"type\":\"row\",\"answer_id\":\"97
55912584\"}],\"position\":\"1\",\"heading\":\"Color of Apple
?\",\"question_id\":\"897359653\"},\"position\":\"1\"}}}}}"}
--changeset
Content-Type: application/http
Content-Transfer-Encoding: binary
POST TravelagencyCollection HTTP/1.1
Content-Type: application/json; charset=utf-8
Content-Length: 198
{"Name":"{\"a\":{\"b\":{\"c\":{\
"d\":{\"e\":{\"survey_id\":\"73579975\",\"title\":\
"second_survey\",\"date_modified\":\"2016-02-22 11:13:00\",\
"analysis_url\":\"https://www.xys.com/MySurvey_Resp
onses.aspx?sm=vDM6KGcZ49pQXaf3KzRzcxgsSPCyc8qacWfE6jXg1B4_3D
\",\"language_id\":\"1\",\"num_responses\":\"3\",\"date_crea
ted\":\"2016-01-07 04:12:00\",\"question_count\":\"1\"}}}},\
"surveydetails\":{\"data\":{\"pages\":{\"page_id\":\"2269089
06\",\"questions\":{\"type\":{\"subtype\":\"vertical\",\"nam
e\":\"\",\"family\":\"single_choice\"},\"answers\":[{\"text\
":\"Red\",\"visible\":\"true\",\"position\":\"1\",\"type\":\
"row\",\"answer_id\":\"9755912582\"},{\"text\":\"Green\",\"v
isible\":\"true\",\"position\":\"2\",\"type\":\"row\",\"answ
er_id\":\"9755912583\"},{\"text\":\"Yellow\",\"visible\":\"t
rue\",\"position\":\"3\",\"type\":\"row\",\"answer_id\":\"97
55912584\"}],\"position\":\"1\",\"heading\":\"Color of Apple
?\",\"question_id\":\"897359653\"},\"position\":\"1\"}}}}}"}
--changeset--
--batch--
how to get it work for JSON, xml data i cant send due to technical issues in HCI?
please help me here experts.