0

Is this the best way to set querystring in using fetchAPI?

const params = new URLSearchParams();
params.set('id', 2);
params.set('word', 'hello');
fetch('https://example.com/?' + params.toString())

I don't think this is the best way because instantiation of URLSearchParams , append the '?' to the end of domain name and using toString method is dull. If I can I want to write as below.

fetch('https://example.com/').params([id:2, word: 'hello']);
mk-tool
  • 325
  • 1
  • 4
  • 10
  • 1
    seeing as fetch returns a promise, what you propose is ridiculous – Jaromanda X Apr 14 '18 at 07:40
  • @JaromandaX My example is wrong exactly. I want to say `fetch(url, { params:[id:2, word: 'hello']}`. But your pointing out is helpful for me. – mk-tool Apr 14 '18 at 17:44
  • @Andreas I think I will be able to write my fetching code more readably with your recommended other question. Thank you. – mk-tool Apr 14 '18 at 17:51

0 Answers0