rhyolight reported intermittent 400 errors on webhook deliveries, suspecting that the Content-Length header did not match the actual POST body size, leading to truncated JSON and SyntaxError: Unexpected end of input on the receiving server. They provided evidence via admin logs and a screenshot. tgxworld suggested verifying if the server logs all incoming requests to determine if the issue lies with the request body or the server’s handling. rhyolight confirmed the hypothesis by replicating the error locally using curl; they found that minifying the JSON body altered its byte count, causing a mismatch with the original Content-Length header, which resulted in parsing failures. Adjusting the header to match the actual body size resolved the local test. tgxworld requested the specific curl command for further investigation. However, rhyolight concluded the thread, noting that the issue had resolved itself as of 2016-09-21, making further reproduction unnecessary.
Some of the HTTP requests being sent to one of my webhooks are being returned with 400 errors. From what I can tell on the client of these requests, it seems like the request body might be getting cut short, or that the Content-Length of the requests doesn’t match the POST body.
For you admins on our hosted instance, you can see the webhook deliveries here.
I cannot tell why some of these requests are valid and some are not. The same logic is processes each request and just logging it to console. From inspecting them in the UI, they all look good, but some of them returned 400. The server logs show SyntaxError: Unexpected end of input which leads me to believe the JSON is possibly invalid because the POST body was cut short?
So I just replicated this error locally with curl based upon the last failing webhook I saw. I extracted the JSON POST body, minified it, and sent it along with the headers Discourse used when sending the data. I saw that upon minifying the JSON POST body, it was not the same size in bytes as the Content-Length header used by Discourse, so my curl command failed. When I changed the Content-Length to match the number of bytes actually in the POST body, it worked.
So the problem is that the Content-Lengthsometimes does not match the number of bytes in the POST body, causing JSON parsing to fail because the entirely of the JSON CLOB was not loaded.