[HTTP] POST content and response function
응답
15. 7. 27 오후 12:50
Hi,

I started using HTTP as a communication method and I am a little confused about a few things. I started with the HCA_httpClientApp as a base. 
What I am wondering is, how can I set the actual content if I want to use a POST? I mean the Serval_Http.h file provides a few methods to set the URL, Content-Type and the like of the message pointer, but I couldn't find a Method like "HttpMsg_setContent(Msg_T *msg_prt, char const *content)".

Also, I would like to have a bidirectional communication so that I need to implement the XDK as a server and a client. How can I set the method, that is called when a request comes in? I assume I need to use the HttpServer_respond method, but I don't really get how to use it. It sounds to me that I use this method to send back a response, but again, where can I specify the content and how do I even know that a request arrived, that I can respond to?

Thanks in advance for your help.

BR,
Florian

 
0 (0 투표)
[HTTP] POST content and response function
응답
15. 7. 29 오전 7:17 as a reply to Florian Dittrich.
Hi Florian,

you can set the payload like this:

    char payload[] = "{json=true}";
    req_info.payload_ptr = payload;
    req_info.payloadLen = sizeof payload;
    rc = RestClient_fillReqMsg(msg_ptr, &req_info);

That sets the content for your request. 
To implement a server on the XDK, you need to listen to incoming connections and respond according to them. HttpServer_respond is the main callback/delegate to do this. 
I'll try to get you an example on an easy ping app.

- Florian

0 (0 투표)
[HTTP] POST content and response function
응답
15. 7. 29 오전 8:34 as a reply to Florian Harr.
Hi Florian,

thanks for your reply and the effort to provide an example, that would help me a lot.

Does that mean, that I am supposed to use the Rest functions completely instead of the ones from HTTP?
I'm now using the functions of Serval_RestClient.h but I am receiving Rest Code 165 which is the REST_PROXYING_NOT_SUPPORTED error. Does that mean I need to set the proxy somehow or is that a HTTP version problem, because it says "/* 5.05 */" in the code, which would be "HTTP Version not supported".

BR,
Florian
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 5 오후 6:06 as a reply to Florian Dittrich.
Hi Florian,

I think we might have a missunderstanding here. You write POST in the title and you talk about exchanging messages between two XDKs with callbacks but apparently you have something else in mind.

Would you be so kind and try to explain what you'd like to do again?

- Florian
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 6 오전 10:30 as a reply to Florian Harr.
Hi Florian,

sure, sorry if I was a little unclear about what I want to achieve.

I'd like to send the sensor data of the XDK via HTTP POST to a Java server. So no communication between different XDKs just between the XDK and the server. But I'd also like to send commands from the server to the XDK. For example disable a certain sensor or turn on/off lamps. So the XDK kind of needs to be a client and a server at the same time. 
I need to use either websockets or HTTP but I was struggling to use websockets, so I decided to focus on HTTP.

Thanks again for your support.

BR,
Florian
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 7 오후 5:02 as a reply to Florian Dittrich.
Florian, 

I assume this goes back to your SensorMonitor implementation we've been looking into together a while ago?

- Florian
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 10 오전 5:41 as a reply to Florian Harr.
Hi Florian,

yes exactly. I've added a new source file handling the REST communication, which gets initialized instead of the WNS_com file, if thats specified on the sd card. 

BR,
Florian
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 11 오전 10:41 as a reply to Florian Dittrich.
I fixed the problem I was having with the Rest Code 165. It was not a proxy issue, I just didn't realize that the server IP needs to be a uint32_t istead of a String. As soon as I used the "Ip_convertOctetsToAddr" function it worked.

Now the GET requests are working properly but the POST is still making trouble. The payload of the response looks like this:

{"timestamp":1439212314366,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'text/json;charset=UTF-8' not supported","path":"/address"}

First I assumed that the problem is lying on the server side, but if I copy the body of the POST and use Postman to send the POST everything works fine. I dont't know if that is a problem, but I was assuming that the content type Header is set to "application/json" but according to the error message is set to "text/json;charset=UTF-8". Might that be an issue?
I was setting the Content-Type like this:

    Rest_ContentFormat_T acceptBuffer[] = { REST_CONTENT_FMT_JSON };  
    RestClient_ReqInfo_T requestInfo;  
    requestInfo.contentFormat = REST_CONTENT_FMT_JSON;
    requestInfo.acceptBuffer_ptr = acceptBuffer;
    requestInfo.numAccept = sizeof acceptBuffer;

Thanks for the support.

BR,
Florian

[Edit]I tried to use the text/json with the postman and I also get the " Unsupported Media Type" Error. Is there a possibility to set the Content Type Header manually to "application/json"?[/Edit]
 
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 12 오전 4:46 as a reply to Florian Dittrich.

Florian,

I looked into the SDK source to try to find a way to set the content type manually, but couldn't really find any.

I've forwarded your request to our second level and I hope I can give you an answer soon!

- Florian

 

0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 12 오후 2:14 as a reply to Florian Harr.
Thank you, I appreciate your effort.
0 (0 투표)
[HTTP] POST content and response function
응답
15. 8. 21 오전 7:23 as a reply to Florian Dittrich.
Hi Florian,

I found a error in my code which might have lead to the exception.
I was using this line:
requestInfo.payloadLen = sizeof(payload_ptr);
which gave me just 4 characters of the payload when I was using "REST_CONTENT_FMT_TEXT_UTF8.
I am now using this code snippet:
extern uint8_t REST_sendRequest(Rest_Method_T method, const char *resource_url,
        char *payload_ptr, Rest_ContentFormat_T format) {        
      retcode_t rc = RC_OK;
    Msg_T* msg_ptr;

    AppSentCallback_T SentCallback = restSentCallback;
    rc = RestClient_initReqMsg(&REST_destAddr, destPort, &msg_ptr,
            REST_CLIENT_PROTOCOL_HTTP);
    if (rc != RC_OK || msg_ptr == NULL) {
            traceDebugErrorNoArg("RC_REST_CLIENT_INIT_REQ_ERROR\r\n");
    }

    Rest_ContentFormat_T acceptBuffer[] = { REST_CONTENT_FMT_JSON,
            REST_CONTENT_FMT_TEXT_UTF8 };
    uint8_t numAccept = sizeof(acceptBuffer) / sizeof(Rest_ContentFormat_T);
    RestClient_ReqInfo_T requestInfo;
    requestInfo.method = method;
    requestInfo.contentFormat = format;
    requestInfo.acceptBuffer_ptr = acceptBuffer;
    requestInfo.numAccept = numAccept;
    requestInfo.payload_ptr = payload_ptr;
    requestInfo.payloadLen = (uint8_t) strlen(payload_ptr);
//    requestInfo.payloadLen = 48;
    traceDebugInfo1("Length of payload %i\r\n", requestInfo.payloadLen);
    requestInfo.uriPath_ptr = resource_url;
    rc = RestClient_fillReqMsg(msg_ptr, &requestInfo);

    if (rc != RC_OK) {
        traceDebugError1("Failed to fill request: %i\r\n", rc);
        return rc;
    }

    traceDebugInfo2("Send Request: \"%s\" -> %s\r\n", resource_url,
            payload_ptr);
    rc = RestClient_request(msg_ptr, SentCallback,
            restHttpClientResponseCallback);
    if (rc != RC_OK) {
        traceDebugError1("Failed to request: %i \r\n ", rc);
        return rc;
    }
    return rc;
}
But there seems to be something wrong with the filling of the message pointer. I receive the following error:
INFO | XDK DEVICE 1:    10[EXH:2]M=0x0C, E=0x04, S=0x2 (0x8604)
I don't really now what that means. Is it possible that I need to initialize the msg_ptr somehow?
The thing is, if I set a number that is smaller or equal to 48 for the payloadLen attribute, I receive the payload on the other side. But my payload is about 90 to 120 characters long. Is there a restriction of the size of the JSON payload which set to 48 internally?
I tried to shrink my payloads as much as possible, so the size is smaller than 48 now, but I keep receiving the RC_REST_CLIENT_INIT_REQ_ERROR error. And at some point after a few seconds the XDK just stops sending and freezes.

BR,
Florian

[Edit] Additionally I am getting a new error.

 INFO | XDK DEVICE 1:    17[EXH:2]M=0x09, E=0x0E, S=0x2 (0x848E)
 INFO | XDK DEVICE 1:    17[HPC:2]Can't alloc Msg (33934)

This one occurs way less than the RC_REST_CLIENT_INIT_REQ_ERROR but also within the RestClient_initReqMsg Method.
[/Edit]
0 (0 투표)