Using wiced_http_get for posting JSON

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi there

I tried to sending data using HTTP POST using the wiced_http_get function, as below, but the body doesn't seem to be coming through correctly.

#define EXAMPLE_POST \

    "POST /post_test HTTP/1.1\r\n" \

    "Host: 54.213.213.231\r\n" \

    "Accept: */* \r\n"\

    "Content-Type: application/json \r\n" \

    "Content-Length: %d \r\n" \

    "\r\n"\

    "%s" \

    "\r\n"

and then

sprintf(body_json,"{\"value\":\"%u\" }", 32); //Simple JSON example

sprintf(post, EXAMPLE_POST, strlen(body_json),body_json );

WPRINT_APP_INFO(( post )); //This result looks correct

wiced_http_get(&ip_address, post, buffer, BUFFER_LENGTH);

I've tried including different structured bodies, and different Content-Type's i.e. application/x-www-form-urlencoded to no luck.

Using Wireshark, the package seems to be structured correctly. But any HTTP endpoints I've tried (Node.js, Xively etc.) just view the package sent as gibberish. Could this be a low-level TCP issue, in that wiced_http_get was not designed for bodies?

Or is there a better recommended way for structuring POST bodies in the WICED SDK?

Thanks.

Another interesting note is that using LwIP, Wireshark would not pick up the packets. Can anyone recommend a cause/workaround?

0 Likes
10 Replies
Anonymous
Not applicable

Can you provide the sniffer trace that you acquired with Wireshark?

It appears as though you will have more luck debugging the other HTTP endpoint as to why it is interpreting the package as gibberish.

0 Likes
Anonymous
Not applicable

Thanks nikvh. I was interested in whether anyone had built applications using HTTP POST requests, and whether the wiced_http_get function was designed to be used with body content. Next step would be debugging from the endpoints, which have worked with all other platforms I have tried.

0 Likes
Anonymous
Not applicable

I am wondering the same think.

Any info on this would be great.

0 Likes
Anonymous
Not applicable

Try apitools.com for debugging and let me know if you have luck.

0 Likes
Anonymous
Not applicable

it does work for me.

My server receives and responds correctly to my post requests.

I used your code from above

Anonymous
Not applicable

Hey! I was trying to use the wiced_http_get to make a post request it keeps failing for me. This is how I am editing the Simple_Get_Request:

#define SIMPLE_POST_REQUEST \

        "POST /api/useridentity HTTP/1.1\r\n" \

        "Host: \r\n" \

        "mac: 89732jshgusz\r\n" \

        "Connection: close\r\n" \

        "body:identifier=awesome&awesome22=awesome2013 \r\n"\

        "Content-Type: application/x-www-form-urlencoded \r\n" \

        "\r\n"\

        "identifier=awesome&identifier2=2013 \r\n"\

        "\r\n"

This is how I am calling the wiced_http_get function:

wiced_http_get( &ip_address, SIMPLE_POST_REQUEST, buffer, BUFFER_LENGTH);

What is the mistake that I am making here?

0 Likes
Anonymous
Not applicable

Please try adding sprintf(post, SIMPLE_POST_REQUEST);

0 Likes
Anonymous
Not applicable

That didn't work. What I actually was missing was the content-length header without which the http protocol ignores the body. The moment I added content-length header, it worked like a charm.

0 Likes

Are you still looking for solutions?

http_stream contain the appropriate functions for you to POST.

You might want to take a look at

wiced_result_t http_stream_start_headers    ( http_stream_t* session, http_request_t method, const char* url );

where

method = HTTP_POST;

0 Likes
Anonymous
Not applicable

show your log,mybe i can help you.

0 Likes