GET with query string with Fetch in React Native

前端 未结 7 1762
情话喂你
情话喂你 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:14

    I did a small riff on Mark Amery's answer that will pass Airbnb's eslint definitions since many teams seem to have that requirement these days.

    function objToQueryString(obj) {
      const keyValuePairs = [];
      for (let i = 0; i < Object.keys(obj).length; i += 1) {
        keyValuePairs.push(`${encodeURIComponent(Object.keys(obj)[i])}=${encodeURIComponent(Object.values(obj)[i])}`);
      }
      return keyValuePairs.join('&');
    }
    

提交回复
热议问题