HTTP RESPONSE query

R

Ram

Hello Everyone,

I have a query related to getting a http response via c program. The
idea is to get the body of a url and store it in an array. I am able
to do this but I get the entire HTTP response. I would just like to
get the body of my http response and not the headers. Is there are way
I can do this in c.

For e.g. I do gethostbyname("domain.com")

i have a char request[] ="GET ....."

Can anyone help me on how to get only the body and ignore the headers?
Is there a way I can pass some parameters in the GET message such that
i just get the response as the body?

Thanks in advance

--best
 
C

Chris M. Thomasson

Ram said:
Hello Everyone,

I have a query related to getting a http response via c program. The
idea is to get the body of a url and store it in an array. I am able
to do this but I get the entire HTTP response. I would just like to
get the body of my http response and not the headers. Is there are way
I can do this in c.

For e.g. I do gethostbyname("domain.com")

i have a char request[] ="GET ....."

Can anyone help me on how to get only the body and ignore the headers?
Is there a way I can pass some parameters in the GET message such that
i just get the response as the body?

Thanks in advance

You could search the data for the first occurrence of "\r\n\r\n" and then
bump the pointer by `strlen("\r\n\r\n")' which should render a pointer to be
beginning of the body, or the end of the entire response; something simple
like:
____________________________________________________________
#include <stdio.h>
#include <string.h>


static char const*
get_body(
char const* const buf
) {
char const* body = strstr(buf, "\r\n\r\n");
if (body) {
return body + strlen("\r\n\r\n");
}
return NULL;
}


int main(void) {
puts(get_body("HTTP/1.0 200 OK\r\n\r\nPayload!"));
return 0;
}
____________________________________________________________


Is that what your looking for?
 
C

Chris M. Thomasson

Chris M. Thomasson said:
Ram said:
Hello Everyone,

I have a query related to getting a http response via c program. The
idea is to get the body of a url and store it in an array. I am able
to do this but I get the entire HTTP response. I would just like to
get the body of my http response and not the headers. Is there are way
I can do this in c.

For e.g. I do gethostbyname("domain.com")

i have a char request[] ="GET ....."

Can anyone help me on how to get only the body and ignore the headers?
Is there a way I can pass some parameters in the GET message such that
i just get the response as the body?

Thanks in advance

You could search the data for the first occurrence of "\r\n\r\n" and then
bump the pointer by `strlen("\r\n\r\n")' which should render a pointer to
be beginning of the body, or the end of the entire response; something
simple like:
____________________________________________________________
#include <stdio.h>
#include <string.h>


static char const*
get_body(
char const* const buf
) {
char const* body = strstr(buf, "\r\n\r\n");
if (body) {
return body + strlen("\r\n\r\n");
}
return NULL;
}


int main(void) {
puts(get_body("HTTP/1.0 200 OK\r\n\r\nPayload!"));
return 0;
}
____________________________________________________________


Is that what your looking for?

One caveat... You would need to ensure that the data ends in a '\0'. This
can be accomplished in several different ways.
 
R

Ram

Ram said:
I have a query related to getting a http response via c program. The
idea is to get the body of a url and store it in an array. I am able
to do this but I get the entire HTTP response. I would just like to
get the body of my http response and not the headers. Is there are way
I can do this in c.
For e.g. I do gethostbyname("domain.com")
 i have a char request[] ="GET ....."
Can anyone help me on how to get only the body and ignore the headers?
Is there a way I can pass some parameters in the GET message such that
i just get the response as the body?

thanks everyone and specifically chris. got it to work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top