Home > Enterprise >  POST response 0 before actual message
POST response 0 before actual message

Time:12-22

So I did some post request in C, simply with write and read. The server is not made by me, so I was wondering if we send POST request to http from write and read function, is it normal to have the POST response as "0\r\n" or sometimes even just nothing (Content-length=0), and when I re-read the response, I can read the actual message?

Is this because how POST request work?

the flow is sth like this

REQUEST ===========>
                     <============ 0\r\n
                     <============ response
                     <============ 0\r\n

my POST request header would look like

POST /path HTTP/1.1
Host: localhost:5000
authorization: bearer
Accept: application/json; charset=utf-8
content-Type: application/json; charset=utf-8
Content-Length: 242

{"some":"json"}

and sometimes the response is just blank before the actual response

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 91
ETag: W/"5b-Jn5H7ksEN9TK a5eBmSVDbbNQMU"
Date: Wed, 21 Dec 2022 09:09:56 GMT
Connection: keep-alive
Keep-Alive: timeout=5

{"some":"json2"}

I'm not sure if im tagging this question right, im new to rest api, but any help would be appreciated, tqu

CodePudding user response:

It is strange (because of the 0) but not at all uncommon, per se. It looks like an example of chunked transfer encoding. The terminating "0" is the correct termination for chunked transfer.

I guess the initial "0" might be a keep-alive of sorts, or a clever attempt of confirming that the server is indeed processing the request, and/or artificially lowering the dreaded TTFB metric when it's measured against the actual content rather than the first byte of headers. It doesn't sound right to me, because I feel that it ought to signal the end of the connection.

Check out the headers of the POST response to verify that you're indeed receiving Transfer-Encoding: chunked, and double-check that the initial sequence is indeed "0".

  • Related