Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 1 month ago.
How to get the body from http response
Hi all, I am using the http example https://os.mbed.com/teams/sandbox/code/http-example/
The connect works fine and the body can be show in terminal with the function dump_response.
printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
Since that is a printf function and I don't know how to get the body as string variable, can anyone give some tips so I can create a new variable to contain the body?
Thank you.
Kaspar
1 Answer
7 years, 1 month ago.
There's probably hundreds of ways you could do this! I would try this:
char* body = malloc(res->get_body_length()); if(body == NULL){ //you've ran out of memory! Handle as appropriate. } strcpy(body, res->get_body_as_string().c_str()); // Do stuff with the body free(body);
But really if you think about it you already have the body as a const char*. If you don't need to alter the body data, and you don't want the additional over head (more typing!) of calling "res->get_body_as_string().c_str()" all the time then you can probably just do this:
const char* body = res->get_body_as_string().c_str());
Give those a go and see what happens.