Re: [whatwg/fetch] Proposal: Easier request/response rewrites (#671)

Thought I'd chime in and say that the initial example showing how to update an URL will not work in Firefox for requests with a body. The ways around it are excessively verbose. [Firefox does not support `Request.body` property](https://developer.mozilla.org/en-US/docs/Web/API/Response/body#browser_compatibility). Meaning it will return undefined and thus not get carried over in example code used by the thread author.

```js
// Change URL, but risk body being lost
let url = new URL(event.request.url)
url.hostname = "example.com"
request = new Request(url, request)
``` 

The best way to retrieve the body without using the body property seems to be the `blob` method of a Request.

```js
// Change URL.
let url = new URL(event.request.url)
url.hostname = "example.com"
request = new Request(url, {
   body: await event.request.blob()
})
```

My initial thoughts are that this is clumsy and probably not very performant. You'd think this whole process could be kept sync… The other problem is that you'd have to manually add back every property from the initial request as they do not seem to spreadable. 

```js
request = new Request(url, {
   body: await event.request.blob()
   ...event.request // doesn't work as expected
})
```

The other problem is that you would need some custom logic to avoid not adding the now uniquely handled body depending on the headers of the initial request. You still get a blob from a Request with n body, just an empty one. Meaning if you have to check if the blob is empty or check for the headers, otherwise you risk getting error:

```
TypeError: Failed to construct 'Request': Request with GET/HEAD method cannot have body.
```

--- 

So yeah, I'd way that there is no easy way to easy way to modify the url of a request in a browser compliant manner, it is actually incredible hard, verbose and frail to do…






-- 
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/671#issuecomment-2615992579
You are receiving this because you are subscribed to this thread.

Message ID: <whatwg/fetch/issues/671/2615992579@github.com>

Received on Monday, 27 January 2025 14:59:57 UTC