GET with query string with Fetch in React Native

前端 未结 7 1699
情话喂你
情话喂你 2020-12-29 01:51

I am making a request like this:

fetch(\"https://api.parse.com/1/users\", {
  method: \"GET\",
  headers: headers,   
  body: body
})

How d

7条回答
  •  时光说笑
    2020-12-29 02:20

    Yes you should, there are a few classes in JS, that can help you a handy one is https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

    e.g. if you had the params in a javascript object say

    let params = {one: 'one', two: 'two'}
    

    you could say this function

    let queryString = new URLSearchParams()
    for(let key in params){
      if(!params.hasOwnkey())continue
      queryString.append(key, params[key])
    }
    

    then you can get your nicely formatted query string by saying

    queryString.toString()
    

提交回复
热议问题