Reputation: 2022
I have a an API available as a Django rest framework. I would like to make it available as a google sheet using Google script as less tech guys are more used to Exel. This a sample body with the input data I'm using with postman to do a post request:
{
"Group": {
"Names": [
"Peter",
"John",
"Marry"
],
"weightsColumns": [
"Peter",
"John",
"Marry"
],
"weightsIndex": [
0,
1,
2
],
"weightsValues": [
[
0.848324434,
0.00129090761,
0.00000549100261
],
[
0.68104794,
0.00000594297639,
0.00000477060816
],
[
0.496659891,
0.00000495070405,
0.00000227036433
],
[
0.326632792,
0.0000148350248,
0.00000133351815
]
]
},
"DataColumns": [
"Scale1",
"Scale2",
"Scale3"
],
"DataValues": [
[
0,
10,
100
]
],
"DataIndex": [
0,
1,
2
]
}
Now how can I do such an API call with Google script. My attempt so far following the official documentation
var data = {
"Group": {
"Names": [
"Peter",
"John",
"Marry"
],
"weightsColumns": [
"Peter",
"John",
"Marry"
],
"weightsIndex": [
0,
1,
2
],
"weightsValues": [
[
0.848324434,
0.00129090761,
0.00000549100261
],
[
0.68104794,
0.00000594297639,
0.00000477060816
],
[
0.496659891,
0.00000495070405,
0.00000227036433
],
[
0.326632792,
0.0000148350248,
0.00000133351815
]
]
},
"DataColumns": [
"Scale1",
"Scale2",
"Scale3"
],
"DataValues": [
[
0,
10,
100
]
],
"DataIndex": [
0,
1,
2
]
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'Authorization': 'ApiKey name:XXXXXX',
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('sampleurl', options);
However with that I get an error with a bad request. But the data is working with postman so that I believe I handle the request wrongly with google script. Any help would be appreciated
Upvotes: 2
Views: 264
Reputation: 4808
try doing as so:
var options = {
"headers": {
'Authorization': 'Bearer ' + token,
"Content-type": "application/json",
},
"method": "POST",
"payload": JSON.stringify(data)
}
use the headers
key for Authorization
and Content-type
. Not sure if you need Bearer
with your api call
Upvotes: 2