[whatwg/fetch] Clarify whether fetch method and Request accept URL instances (#452)

In `fetch(url)` and `new Request(url)` invocations, can `url` be an instance of `URL`? The spec is not clear.

[This example shows](https://fetch.spec.whatwg.org/#fetch-api) that `fetch()` should likely accept URL:

```js
var url = new URL("https://geo.example.org/api"),
    params = {lat:35.696233, long:139.570431}
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
fetch(url).then(/* … */)
```

However, when I look at the spec itself, the first argument for Request constructor is [defined as "RequestInfo"](https://fetch.spec.whatwg.org/#requestinfo), and [later on it states](https://fetch.spec.whatwg.org/#dom-request):

> 5\. **If input is a string**, then run these substeps: …
> 6\. **Otherwise (input is a `Request` object**), run these substeps: …

This suggests that the argument may only be "USVString" or Request, and that behavior concerning other types is undefined, or—as I interpret it—that any non-string type will be treated as Request and that an exception will be thrown if the type doesn't look like a Request.

The spec also suggests that the behavior of the `fetch()` method is identical to `new Request()` because the arguments to `fetch()` are [forwarded to Request constructor](https://fetch.spec.whatwg.org/#dom-global-fetch). It follows that `fetch()` too only accepts string or Request as argument.

Google Chrome's implementation, however, seems to treat any non-Request argument as a url by converting it to a string. Example:

```js
new Request({ toString: () => "foo" }).url
// => "https://github.com/whatwg/fetch/issues/foo"
```

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

Received on Friday, 13 January 2017 12:45:03 UTC