Hello.

I have a problem  with using  Serval Rest api , Performing a POST request.

In PDF manual,  Code 16. POST request using the REST API(XDK_Guide_HTTP.pdf) , I use this code and it works.

But I meet a problem when I changed the code postRequest.payload_ptr to send variable sensor data .

--------------------------------------------------------------------

...

unsigned char buff[47];

sprintf(buff,  "{\"Name\": \"Luke\", \"Lux\": %d}", (int)milliLuxData);  //  sensor value (brightness, lux)

...

postRequest.payload_ptr = (uint8_t*)buff;

...

RestClient_request(msg_ptr, &onRESTRequestSent, &onRESTResponseReceived);

--------------------------------------------------------------------

Communication is ok. but  Http Rest api server's (on my PC) receiving Post method's input parameter is null.

As Code 16.  hard typed string "{\"num\": 123, ... }"  is work.( input parameter filled out values )

I tested other cases (use pointer instead of array buffer), but they are not working. ( not working code have 'sprintf' )

-------------------------

unsigned char* buff = malloc(48);

sprintf(buff, "{\"Name\": \"Luke\", \"Lux\": %d}", (int)milliLuxData);

...

postRequest.payload_ptr = (uint8_t*)buff;

...

In my opinion , any cases,  if I use  sprintf  , it might fail.

Is it going wrong with using  sprintf  for XDK's SDK ?

or is there another problem what I don't know?

 

 

0 (0 Votes)
RE: Serval Rest api, Post request, payload_ptr problem.
Answer
1/11/18 6:26 PM as a reply to SangHyuk Woo.

Hello SangHyuk,

First, I would like to welcome you to the XDK community.,

As for your issue, generally, sprintf() is working on the XDK as expected - the issue does not lie with the library here.

I have tried replicating your issue, by also sending a payload that is first put into a buffer using sprintf(), but in my case it always works.

First, I would recommend to allocate the buffer as static char buff[47] to make sure it persists even after the function is finished, and use memset() to set all of the values in buff to zero. In the worst case, the content of the buffer may be overwritten automatically, before sending is completed.

Additionally, before you call RestClient_Request(), I would recommend to print both the buffer and the content at postRequest.payload_ptr as follows:

printf("Payload: %s\n\r", (char *) postRequest.payload_ptr);
printf("Buffer: %s\n\r", buff);

This will show if the content is actually put currectly.

Finally, since your code actually works with a hard-coded string, I do not see why it should not work with a buffer instead of hard-coded values instead. As such, if it doesn't work after you implemented the change I proposed, please post your code here, so we have the same understanding of your current code.


Please let me know if that was helpful and feel free to ask if you have further questions.

Kind regards,
Franjo

0 (0 Votes)
RE: Serval Rest api, Post request, payload_ptr problem.
Answer
1/15/18 1:15 AM as a reply to Franjo Stjepandic.

Hi Franjo, I aprreciate you. 

As you recommend, I added memset and the problem has fixed.

It works.

(it may filled with garbage char in array .  initialisation problem )

 

corrected code is below

#define REST_POST_BUFF_LENGTH 47

static char buff[REST_POST_BUFF_LENGTH];

static void Init(void)
{
...
    memset(buff, UINT32_C(0), REST_POST_BUFF_LENGTH);
    retcode_t rc = HttpClient_initialize();
...
}

static void RESTPost(void *param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    Ip_Port_T destPort = Ip_convertIntToPort(DEST_SERVER_PORT);

	Rest_ContentFormat_T acceptedFormats[1] = { REST_CONTENT_FMT_JSON };
	RestClient_ReqInfo_T postRequest;
	postRequest.method = REST_POST;
	postRequest.uriPath_ptr = "/api/XDK";
	postRequest.uriQuery_ptr = "";
	postRequest.acceptBuffer_ptr = acceptedFormats;
	postRequest.numAccept = 1;
	postRequest.payloadLen = REST_POST_BUFF_LENGTH;//sizeof(buff);
//	postRequest.payload_ptr = (uint8_t*) "{\"num\": 123, \"str\": \"Hello\"}";
	postRequest.payload_ptr = (uint8_t*)buff;
	postRequest.contentFormat = REST_CONTENT_FMT_JSON;
	postRequest.rangeLength = 0;
	postRequest.host = NULL;
	Msg_T* msg_ptr;
	RestClient_initReqMsg(&destAddr, destPort, &msg_ptr, REST_CLIENT_PROTOCOL_HTTP);
	RestClient_fillReqMsg(msg_ptr, &postRequest);
	// send the request
	RestClient_request(msg_ptr, &onRESTRequestSent, &onRESTResponseReceived);

	printf("after send, buff : %p, %s , %d \r\n", (char*)buff, buff, sizeof(*buff));
	printf("postRequest.payload_ptr : %p, %s , %d \r\n", postRequest.payload_ptr, (char*)postRequest.payload_ptr, sizeof(*postRequest.payload_ptr));
}

 

0 (0 Votes)
RE: Serval Rest api, Post request, payload_ptr problem.
Answer
1/15/18 5:15 PM as a reply to SangHyuk Woo.

Hello SangHyuk,

I am glad to hear that you were able to resolve this issue.

Feel free to ask if you have further questions. If you need help on another topic, I would appreciate it if you create a new thread for that.

Kind regards,
Franjo

0 (0 Votes)