cancel
Showing results for 
Search instead for 
Did you mean: 

HTTP 'PUT' request for Netweaver Gateway Service (OData) using JSON format

Former Member
0 Kudos

Hi there,

I am trying to update data using the update method of a Netweaver Gateway (OData) Service from SAPUI5 or the Gateway Client (that shouldn't matter).

So far I can only get a valid HTTP Response if I am using XML as the request body data.

Is it possible to use an HTTP request with a request body data using a JSON string instead of the XML string? If this can be done, what do I have to do before using the 'update()' method of an ODataModel object.

Best Regards

Patrick

Accepted Solutions (1)

Accepted Solutions (1)

kammaje_cis
Active Contributor
0 Kudos

H Patrick,

Yes json payload is possible.

Check this link for more on json payload. http://www.odata.org/documentation/odata-v2-documentation/json-format/

You also need to give the below request header to tell the server that payload is json.

Content-Type: application/json

Thanks

Krishna

Former Member
0 Kudos
Hi Krishna,
I tried to change the content type within the request header, but I always get status code 500.
Do I have to format the JSON request by myself. XML is working like a charm (I just have to add the key-value pairs and the proper request body is wrapped around.
This is my current JSON request body.

{

    "rowID": "50001444",

    "cellValue": "true",

    "columnID": "50002026"

}

Or is there a way to use JSON within the Gateway Client to test it without the use of JavaScript?

Best Regards and thanks in advance

Patrick

kammaje_cis
Active Contributor
0 Kudos

With 500, what error do you get?

arunchembra1
Participant
0 Kudos

Hi Patrick,

First call your get service using $format=json then  apply the below headers with the url and request body.

Use Headers:

1. x-csrf-token

2. Accept:application/json

3. Content-Type:application/json

Request Body:

{

    "rowID": "50001444",

    "cellValue": "true",

    "columnID": "50002026"

}

Thanks,

Arun Chembra

kammaje_cis
Active Contributor
0 Kudos

You can also use Chrome Advanced Rest Client which is easy to use.

chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo/RestClient.html

Just use the key-value pairs and give content type as application/json.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

I am trying to use oData Update functionality. I have followed the steps mentioned in the thread to create the request. I am using below code to initialize the oDataModel.

var saveModel = new sap.ui.model.odata.ODataModel("http://ec2-54-213-97-224.us-west-2.compute.amazonaws.com:8000/workshop/sessiona/01/services/MarketIn...", true,null,null,{"Content-Type": "application/json;charset=utf-8"});

                    var oTable = sap.ui.getCore().byId('tableid');

                    var oModel = oTable.getModel();

                    var ModelData = oModel.getData();

                    for(var i = 0 ; i < 1 ; i++)

                              {

                                        var id = ModelData[i].id;

                                        var oEntry = {

                                                    "FRACTION":"4.3",

                                                    "MARKETID" : "00001",

                                                    "YEAR":"2013",

                                                    "MONTH":"08",

                                                    "INDUSTRY":"ENTERTAINMENT"

                              };

                                        var oParams = {};

                                        oParams.fnSuccess = function (){};

                                        oParams.fnError = function () {};

                                        saveModel.update("/MarketInfo('"+id+"')", oEntry, oParams);

 

                              }

I have set the bJSON flag to true and also set the Content -Type. However when I run the application I get 415 Unsupported Media Type Error.

Below is the trace:

  1. Request URL:http://ec2-54-213-97-224.us-west-2.compute.amazonaws.com:8000/workshop/sessiona/01/services/MarketIn...')
  2. Request Method:PUT
  3. Status Code:415 Unsupported Media Type
  4. Request Headersview parsed
    1. PUT /workshop/sessiona/01/services/MarketInfo.xsodata/MarketInfo('00002') HTTP/1.1 Host: ec2-54-213-97-224.us-west-2.compute.amazonaws.com:8000 Connection: keep-alive Content-Length: 91 Authorization: Basic c3lzdGVtOm1hbmFnZXI= MaxDataServiceVersion: 2.0 Origin: http://ec2-54-213-97-224.us-west-2.compute.amazonaws.com:8000 Accept-Language: en-US User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 Content-Type: application/json Accept: application/json DataServiceVersion: 2.0 Referer: http://ec2-54-213-97-224.us-west-2.compute.amazonaws.com:8000/workshop/sessiona/01/ui/WebContent/ind... Accept-Encoding: gzip,deflate,sdch Cookie: __gads=ID=17a6adb714195f75:T=1371410403:S=ALNI_MYkHmlb5LM1b37EG5BpjK7nqxEcEQ; xsSessionId=B97C7B06BC9D174E8A6C2CA3E183D79C; sapxslb=5D6D1EE6312C454791227AFA22D076A8
  5. Request Payloadview parsed
    1. {"FRACTION":"4.3","MARKETID":"00001","YEAR":"2013","MONTH":"08","INDUSTRY":"ENTERTAINMENT"}
  6. Response Headersview parsed
    1. HTTP/1.1 415 Unsupported Media Type content-type: application/json;charset=utf-8 content-language: en-US content-length: 202 cache-control: no-cache expires: Thu, 01 Jan 1970 00:00:00 GMT

The content type for request header is only application / json and this seems to be creating the problem. Please let me know how I can override the content type.

Thanks,

Aamod

kammaje_cis
Active Contributor
0 Kudos

Aamod,

This is answered/closed question.

Post your question as a new thread/question so that it gets noticed.

Thanks

Krishna

Former Member
0 Kudos

So, now it is working. the only things I changed from xml to JSON was:

  • I've set the bJSON flag of the ODataModel to true
  • I've added the Content-Type to the request header with the value application/json.
  • I use all URLs without the $format URI parameter.

Best Regards and thanks again

Patrick