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('000000...')","uri":"http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection('000000...')","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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
35 | |
25 | |
17 | |
13 | |
8 | |
7 | |
6 | |
6 | |
6 | |
6 |