Sei sulla pagina 1di 2

JSON POST & PUT (single & batch) on NetWeaver Gateway

Posted by Debdutt Mondal


inShare1

in SAP for Mobile on 15-Apr-2013 18:13:49

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 http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection?$top=1&$format=jso n Headers Name:x-csrf-token Value:fetch Response {"d":{"results":[{"__metadata":{"id":"http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/Travela gencyCollection('00000001')","uri":"http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/Travelage ncyCollection('00000001')","type":"RMTSAMPLEFLIGHT.Travelagency"},"agencynum":"00000001","NAME":"DEBU","STR EET":"","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 http://ldcigiq.wdf.sap.corp:50015/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/TravelagencyCollection('00000001') 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":"I ND","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 http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/40546820-3ea7-2f10-dfabbe373c0da357?QuickLink=index&overridelayout=true&55959128903935