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 'https://openapi.growatt.com/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 RefAPI 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.com/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.

1 Answer 1

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',
    },
  };

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.