相当于curl命令的axios

woo*_*ymj 1 axios

我正在尝试将以下curl转换为axiosGET请求以在React Native中使用。

这是我要转换的内容:

$ curl --get --user ${client_id}:${client_secret} \
 https://api.shutterstock.com/v2/images/search \
 --data-urlencode "query=donkey" \
 --data-urlencode "page=2" \
 --data-urlencode "per_page=1"
Run Code Online (Sandbox Code Playgroud)

以上输出:

{"page":2,"per_page":1,"total_count":42019,"search_id":"g3gruNqeXTnAOggCv9s9hA","data":[{"id":"384331075","aspect":1.1607,"assets":{"preview":{"height":387,"url":"https://image.shutterstock.com/display_pic_with_logo/3969413/384331075/stock-photo-laughing-donkey-good-and-funny-donkey-the-portrait-the-best-photo-of-donkey-in-the-world-384331075.jpg","width":450},"small_thumb":{"height":86,"url":"https://thumb7.shutterstock.com/thumb_small/3969413/384331075/stock-photo-laughing-donkey-good-and-funny-donkey-the-portrait-the-best-photo-of-donkey-in-the-world-384331075.jpg","width":100},"large_thumb":{"height":129,"url":"https://thumb7.shutterstock.com/thumb_large/3969413/384331075/stock-photo-laughing-donkey-good-and-funny-donkey-the-portrait-the-best-photo-of-donkey-in-the-world-384331075.jpg","width":150},"huge_thumb":{"height":260,"url":"https://image.shutterstock.com/image-photo/laughing-donkey-good-funny-portrait-260nw-384331075.jpg","width":302}},"contributor":{"id":"3969413"},"description":"Laughing donkey - good and funny donkey. The Portrait. The best photo of donkey in the world. Northern Cyprus. Karpasia. Karpaz. Dipkarpaz. Rizokarpaso. Apostolos Andreas cape donkey","image_type":"photo","media_type":"image"}],"spellcheck_info":{}}
Run Code Online (Sandbox Code Playgroud)

这是我所做的,仅返回401错误:

axios.get("https://api.shutterstock.com/v2/images/search?query=donkey&page=2&per_page=1",{ headers: {
      'Authorization': 'Basic client_id:client_secret'
    }}).then(res => console.log((res)));
Run Code Online (Sandbox Code Playgroud)

还尝试了以下操作,但出现401错误:

axios.get("https://api.shutterstock.com/v2/images/search?query=donkey&page=1&per_page=1",{ headers: {
  user: client_id:client_secret
}}).then(res => console.log((res)));

axios.get("https://api.shutterstock.com/v2/images/search?query=donkey&page=1&per_page=1",{ headers: {
  user: client_id, pass: client_secret
}}).then(res => console.log((res)));
Run Code Online (Sandbox Code Playgroud)

我应该转换client_id:client_keybase64encoded字符串吗?另外,如何在不附加到URL的情况下将查询参数包括在请求中?

woo*_*ymj 5

我还是想通了。

axios({
      baseURL: 'https://api.shutterstock.com/v2/images/search?query=donkey&page=1&per_page=1',
      auth: { username: 'CLIENT_ID', password: 'CLIENT_SECRET'},
      params: { query: 'donkey', page: '1', per_page: '1' }  
}).then(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)


小智 5

试试这个curl 转换器,只需发布你的 curl cmd。