jg943
jg943

Reputation: 309

POST HTTP Response

I'm making a simple C web server that parses a POST request containing and xml body. I am trying to send back the response, but I cant seem to get it right. Here is my code:

char res[2000];
strcpy(res, "HTTP/1.1 201 OK\nContent-Type: text/html\n");

printf("%s", res);;
char re[4026];
strcpy(re,"<html><body><p>");
strcat(re, result);
strcat(re, "</p></body></html>\n");
printf("\n%s\n", re);

char tm[1000];
sprintf(tm, "Content-Length: ");
char len[4035];
int l= strlen(re);
sprintf(len, "%d\n", l);
printf("\n%s\n", len);
strcat(tm, len);
printf("\n%s\n", tm);

strcat(res, tm);
strcat(res, re);
printf("\n%s", res);
strcat(res, "\0");

send(sock, res, strlen(res),0);

I'm not sure why, but when I send, noting is received and I stay in the loop.

Just in case the code is a little hard to follow this is the string im am generating:

HTTP/1.1 201 OK
Content-Type: text/html
Content-Length: 37
<html><body><p>139</p></body></html>

Upvotes: 0

Views: 188

Answers (1)

John Conde
John Conde

Reputation: 219834

You need another new line after your headers and before your body:

HTTP/1.1 201 OK
Content-Type: text/html
Content-Length: 37

<html><body><p>139</p></body></html>

Upvotes: 2

Related Questions