Reputation: 1
I've been trying to convert the following line of curl code to Google Apps Script and have had no luck.
The curl code:
curl -X POST "https://api.textspaced.com/market/commodities/" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "token=###"
I've tried different styles of headers and different ways of labeling the token and payloads, but I always get the error that No API token has been provided.
Google Apps Script code:
function preshfinder() {
var message = 'token=###' ;
var payloads = JSON.stringify(message);
var headers = {'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
};
var requestOptions = {
"ContentType" : "application/json",
headers: headers,
method: "POST",
payload: payloads,
followRedirects : true,
};
var fetcher = UrlFetchApp.fetch('https://api.textspaced.com/market/commodities', requestOptions)
Logger.log(fetcher.getContentText());
}
Upvotes: 0
Views: 293
Reputation: 201603
You want to convert the following curl command to Google Apps Script.
curl -X POST \
"https://api.textspaced.com/market/commodities/" \
-H "accept: application/json" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "token=###"
You have already confirmed that the above curl command worked.
If my understanding is correct, how about this answer?
application/x-www-form-urlencoded
. And the data is sent as form.followRedirects
is true
.When above points are reflected to your script, how about the following modification?
function preshfinder() {
var payloads = {token: "###"}; // Please set your token here.
var headers = {
'accept': 'application/json'
};
var requestOptions = {
contentType: 'application/x-www-form-urlencoded',
headers: headers,
method: "POST",
payload: payloads,
};
var fetcher = UrlFetchApp.fetch('https://api.textspaced.com/market/commodities', requestOptions)
Logger.log(fetcher.getContentText());
}
The request of above modified script is the same with the curl command. But I cannot test this. So if this didn't resolve your issue, I apologize.
Upvotes: 1