Bug 254010

Summary: Fetch POST Requests Not Working on Safari
Product: WebKit Reporter: Pranay Yadav <pranayyadav08>
Component: Page LoadingAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Blocker CC: achristensen, ap, beidson, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar
Version: Safari 16   
Hardware: All   
OS: iOS 16   

Description Pranay Yadav 2023-03-15 22:37:13 PDT
It seems that fetch is not working properly on both ARM and x86 machines, resulting in request bodies not being sent to the intended URL, despite using various code examples and workarounds. The expected request format should look like
"POST / HTTP/1.1\r\nHost: localhost:53421\r\nContent-Type: application/json\r\nOrigin: http://localhost:53421\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nAccept: application/json\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15\r\nReferer: http://localhost:53421/\r\nContent-Length: 22\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n{\"get_response\": true}"
but the actual request sent is missing the JSON body
"POST / HTTP/1.1\r\nHost: localhost:53421\r\nContent-Type: application/json\r\nOrigin: http://localhost:53421\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nAccept: application/json\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15\r\nReferer: http://localhost:53421/\r\nContent-Length: 22\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n"
It is important to note that after a certain number of requests, the body is added and it works as expected. It is also worth mentioning that the issue only occurs in safari, and not on other browsers.
Comment 1 Alexey Proskuryakov 2023-03-16 09:04:41 PDT
Could you please provide some of the examples that don't work? 

This is very basic functionality, it's fairly unlikely that the issue is so general. There must be some specific detail that triggers the issue.
Comment 2 Pranay Yadav 2023-03-16 10:35:06 PDT
the following snipped of code is used to make the request : 
fetch(REQUEST_URL, {
  method: "POST",
  headers: {
  "Content-Type": "application/json",
  "Accept": "application/json",
  },
  body: '{"get_response": true}',
})

but the request received on the backend is : 
"POST / HTTP/1.1\r\nHost: localhost:60145\r\nContent-Type: application/json\r\nOrigin: http://localhost:60145\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nAccept: application/json\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15\r\nReferer: http://localhost:60145/\r\nContent-Length: 22\r\nAccept-Language: en-IN,en-GB;q=0.9,en;q=0.8\r\n\r\n"

and after 300+ requests, the body was finally added and the request received on backend was : 
"POST / HTTP/1.1\r\nHost: localhost:60145\r\nContent-Type: application/json\r\nOrigin: http://localhost:60145\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive\r\nAccept: application/json\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15\r\nReferer: http://localhost:60145/\r\nContent-Length: 22\r\nAccept-Language: en-IN,en-GB;q=0.9,en;q=0.8\r\n\r\n{\"get_response\": true}"
Comment 3 Alexey Proskuryakov 2023-03-16 10:47:57 PDT
So this isn't even a cross-origin request, both are localhost:60145 for you.

I cannot reproduce this with these steps:

- Put fetch.html (below) in LayoutTests/http/tests in a WebKit checkout.
- run-webkit-httpd
- load http://127.0.0.1:8000/fetch.html in Safari
- terminate run-webkit-httpd
- nc -l 8000
- Click the Test button on the page.

I always get the request body in nc output.


----- fetch.html

<button onclick="test()">Test</button>
<script>
function test() {
    fetch("http://127.0.0.1:8000/", {
     method: "POST",
     headers: {
     "Content-Type": "application/json",
     "Accept": "application/json",
     },
     body: '{"get_response": true}',
    })
}
</script>
Comment 4 Radar WebKit Bug Importer 2023-03-22 22:38:13 PDT
<rdar://problem/107117845>