Reputation: 179
I'm trying to convert the following curl into an axios
GET request for use in React Native.
Here's what I want to convert:
$ 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"
Output of above:
{"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":{}}
Here's what I did and it just returns a 401 error:
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)));
Also tried with the following but got a 401 error:
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)));
Should I be converting client_id:client_key
into a base64encoded
string? Also, how can I include the query params in the request without just appending to the URL?
Upvotes: 5
Views: 14354
Reputation: 69
Paste your curl command into https://curlconverter.com/node-axios/ and it will convert it to
const axios = require('axios');
const response = await axios.get('https://api.shutterstock.com/v2/images/search', {
params: {
'query': 'donkey',
'page': '2',
'per_page': '1'
},
auth: {
username: 'client_id',
password: 'client_secret'
}
});
Upvotes: 6
Reputation: 179
I figured it out anyway.
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));
Upvotes: 10