Safari doesn't properly send a http POST to a server, same code works on Chrome and Firefox)

https://stackoverflow.com/questions/76523771/browser-safari-refuses-to-send-the-body-via-javascript-fetch-api-with-post


This is a Safari-specific issue. When using the fetch API in Javascript (but the same might happen when using XMLHttpRequest), safari refuses to send the content of the POST request.


Is there a way around this so it also works on Safari?

Posted on Jun 21, 2023 7:45 AM

Reply
Question marked as Top-ranking reply

Posted on Feb 4, 2024 12:40 PM

Ok so my problem was fixed by asking my tcp socket to continue receiving the request data until the body is equal to the content length:


headers, body = request_data.split("\r\n\r\n", 1)  # Split request data into headers and body

# get the content length from the headers
content_length = 0
for line in headers.split("\r\n"):
    if "Content-Length" in line:
        content_length = int(line.split(": ")[1])
        break

while len(body) < content_length:
    body += conn.recv(1024).decode('UTF-8')


Basically what this means if you are not using a tcp socket and still wondering why this is happening:

Your web server did NOT receive the full request YET, safari sends it on chunks for some reason, hope this helps, keep receiving the request until the body of the POST is received.

Similar questions

5 replies
Question marked as Top-ranking reply

Feb 4, 2024 12:40 PM in response to Gerben Wierda

Ok so my problem was fixed by asking my tcp socket to continue receiving the request data until the body is equal to the content length:


headers, body = request_data.split("\r\n\r\n", 1)  # Split request data into headers and body

# get the content length from the headers
content_length = 0
for line in headers.split("\r\n"):
    if "Content-Length" in line:
        content_length = int(line.split(": ")[1])
        break

while len(body) < content_length:
    body += conn.recv(1024).decode('UTF-8')


Basically what this means if you are not using a tcp socket and still wondering why this is happening:

Your web server did NOT receive the full request YET, safari sends it on chunks for some reason, hope this helps, keep receiving the request until the body of the POST is received.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Safari doesn't properly send a http POST to a server, same code works on Chrome and Firefox)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.