Hello,

 

I am currently testing the XDK REST implementation, and after successfully connecting to my test server (Flask-Restful with a single route) all requests sent by the XDK return as "400 - BAD REQUEST". The server sends "Failed to decode JSON object: Expecting value: line 1 column 1 (char 0)" as debug information, but I used the example JSON string from the HTTP guide.

 

Here my code for the JSON composition:

printf("Trying to send.....\r\n");

// assemble the request message
Ip_Address_T destAddr;
Ip_convertOctetsToAddr(192, 168, 0, 1, &destAddr);
Ip_Port_T port = Ip_convertIntToPort(5000);
Rest_ContentFormat_T acceptedFormats[1] = { REST_CONTENT_FMT_HTML };
RestClient_ReqInfo_T postRequest;
postRequest.method = REST_POST;
postRequest.uriPath_ptr = "/data/add";
postRequest.uriQuery_ptr = "";
postRequest.acceptBuffer_ptr = acceptedFormats;
postRequest.numAccept = 1;
postRequest.payloadLen = 34;
postRequest.rangeLength = 0;
postRequest.host = NULL;
postRequest.payload_ptr = (uint8_t*) "{\"num\": 123, \"str\": \"Hello\"}";
postRequest.contentFormat = REST_CONTENT_FMT_JSON;
Msg_T * msg_ptr;
RestClient_initReqMsg(&destAddr, port, &msg_ptr, REST_CLIENT_PROTOCOL_HTTP);
RestClient_fillReqMsg(msg_ptr, &postRequest);
// send the request
RestClient_request(msg_ptr, &onRESTRequestSent, &onRESTResponseReceived);

Is the error on the XDK side?

 

Best regards,

Florian

 

EDIT:

I activated more debug printing on the server side, and printing the request body using flask.request.data, it seems the JSON body is nearly empty:

A standard POST request sent via browser plugin has a body like

b'{\n "Valname": "Value", \n ....more entries....}'

whereas the body received from the XDK is just

b''

 

Am I not filling the  message body?

0 (0 Votes)
RE: 400 Bad Request on POSTing JSON using Serval_Rest
Answer
10/9/17 3:26 PM as a reply to Florian Grabi.

Hello Florian,

the REST implementation currently can only use 4 different headers, which are

Content-Type
Transfer-Encoding
Host
Range

the only unchangeable header here is Transfer-Encoding, which is always set to chunked. Regarding your issue, I assume that your flask server cannot handle this transfer-encoding, at least not without setting a Content-Length in the headers (which is currently not possible with the XDK Rest API).

I found a similar issue in the flask repository here. My guess is that the standard post request from your browser plugin does not use the chunked Transfer-Encoding, but a Content-Length instead. Which is also why the browser plugin’s POST request works, while the XDK’s does not.

Please tell me if this was helpful, and do not hesitate to ask further questions.

Kind regards,
Manuel

0 (0 Votes)
RE: 400 Bad Request on POSTing JSON using Serval_Rest
Answer
10/17/17 12:49 PM as a reply to Manuel Cerny.

Hello Manuel,

thank you very much for the info! You were spot on, the problem was the empty Content-Length header. A colleague of mine tried it with a node.js server application, and after activating chunked reception, the transfer worked :-)

I am currently setting up a standard nginx server with uWSGI, which should be able to handle the chunked payload just fine.

Best regards

Florian

 

0 (0 Votes)
RE: 400 Bad Request on POSTing JSON using Serval_Rest
Answer
10/17/17 4:29 PM as a reply to Florian Grabi.

Hello Florian,

I am glad to hear that I could help you.

Please do not hesitate to ask if you have further questions.

Kind regards,
Manuel

0 (0 Votes)