Fetch: GET with parameters not just in the URL

Created on 24 Jun 2015  路  6Comments  路  Source: github/fetch

In some http clients, e.g. jQuery.ajax, it's possible to not just put the params in the URL but an object. The client will mix/concat the params into the URL later. Is that possible to give params independently like

fetch("foo.json" {
  method: "get",
  params: {
    param1: "value1",
    param2: "value2",
  }
}).then(response => response.json())

to come up with a URL /foo.json&param1=value1&param2=value2?

Also, sometimes the params may be longer than the length limitation, so that some APIs accept GET requests with a body of parameters. So I don't understand the reason fetch couldn't _GET_ with the body like

fetch("foo.json" {
  method: "get",
  body: {
    param1: "value1",
    param2: "value2",
  }
}).then(response => response.json())

Will these be behaviors allowed in the future? Maybe I can submit a pull request for these.

Most helpful comment

Query parameters may be encoded natively with encodeURIComponent or with jQuery.param.

All 6 comments

It's best to ask for this support at the https://github.com/whatwg/fetch specification repository.

URLSearchParams is accepted as a body value, but it's only supported by Firefox right now.

@dgraham That is, if we create a polyfill for URLSearchParams it should work with this fetch polyfill?

@dgraham not exactly accepted. fetch polyfill is still complaining TypeError: Body not allowed for GET or HEAD requests

screen shot 2015-06-24 at 2 27 47 pm

How is this solved today?

Query parameters may be encoded natively with encodeURIComponent or with jQuery.param.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gkatsanos picture gkatsanos  路  4Comments

javan picture javan  路  3Comments

karladler picture karladler  路  4Comments

poppinlp picture poppinlp  路  4Comments

fczuardi picture fczuardi  路  3Comments