最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

rest - cURL API call in Google App Script with Header & Body data - Stack Overflow

programmeradmin3浏览0评论

I'm a bit of a novice with Google App Script and looking for a bit of help making an API call. The data in my call is working fine when used in Postman.

Postman shows me the cURL call is as follows:

curl --location '' \
--header 'token: xxMyTokenxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: SERVERCORSID=b91729c6d809fa10b5c855916352a94d|1739535788|1739535788; SERVERID=b91729c6d809fa10b5c855916352a94d|1739535788|1739535788' \
--data-urlencode 'deviceType=sph' \
--data-urlencode 'deviceSn=xxMySNxx'

The "cookie" part i have no idea about... that just appeared in Postman. The Growatt API documentation just tells me i need 4 things (which i have... these 4 things work just fine in Postman as mentioned):

The URL, The API Key, Device Type & Device Serial Num.

API Ref

I've tried several variants of the following app script code... but just get Access Denied returned by the Growatt server...

const bodyData = {
    deviceType: 'sph',
    deviceSn: 'xxMySNxx',
  };

  const options = {
    method: 'POST',
    token: 'xxMyTokenxx',
    payload: JSON.stringify(bodyData),
  };

  let url = '';
  
  let response = UrlFetchApp.fetch(url, options);

I'm pretty sure i'm just not putting the options together properly... double quotes, single quotes, no quotes, stringify or not?! I left out content-type as i believe it defaults to the value i need?

Anyway, i'd be really grateful if someone could point me in the right direction.

I'm a bit of a novice with Google App Script and looking for a bit of help making an API call. The data in my call is working fine when used in Postman.

Postman shows me the cURL call is as follows:

curl --location 'https://openapi.growatt/v4/new-api/queryLastData' \
--header 'token: xxMyTokenxx' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: SERVERCORSID=b91729c6d809fa10b5c855916352a94d|1739535788|1739535788; SERVERID=b91729c6d809fa10b5c855916352a94d|1739535788|1739535788' \
--data-urlencode 'deviceType=sph' \
--data-urlencode 'deviceSn=xxMySNxx'

The "cookie" part i have no idea about... that just appeared in Postman. The Growatt API documentation just tells me i need 4 things (which i have... these 4 things work just fine in Postman as mentioned):

The URL, The API Key, Device Type & Device Serial Num.

API Ref

I've tried several variants of the following app script code... but just get Access Denied returned by the Growatt server...

const bodyData = {
    deviceType: 'sph',
    deviceSn: 'xxMySNxx',
  };

  const options = {
    method: 'POST',
    token: 'xxMyTokenxx',
    payload: JSON.stringify(bodyData),
  };

  let url = 'https://openapi.growatt/v4/new-api/queryLastData';
  
  let response = UrlFetchApp.fetch(url, options);

I'm pretty sure i'm just not putting the options together properly... double quotes, single quotes, no quotes, stringify or not?! I left out content-type as i believe it defaults to the value i need?

Anyway, i'd be really grateful if someone could point me in the right direction.

Share Improve this question edited Feb 15 at 5:16 TheMaster 50.6k7 gold badges69 silver badges98 bronze badges asked Feb 14 at 14:57 MikeBoxMikeBox 11 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

Add in headers as a object and don't stringify the the payload to url encode it:

  const options = {
    method: 'POST',
    headers: {token: 'xxMyTokenxx'},
    payload: {
     deviceType: 'sph',
     deviceSn: 'xxMySNxx',
    },
  };
发布评论

评论列表(0)

  1. 暂无评论