V
V
Vladimir Zuev2020-06-01 08:59:00
API
Vladimir Zuev, 2020-06-01 08:59:00

How to create a request for Nova Poshta from google script?

Hello.
I'm trying to get a list of cities in Nova Poshta from a google script.

async function novaPoshta () {
 
 var headers = {
'Content-Type': 'application/json',
'apiKey': '***************'
};
 
var options = {
'method' : 'POST',
'headers': headers,
'modelName': 'Address',
'calledMethod': 'getCities',
  }
var result = await UrlFetchApp.fetch('https://api.novaposhta.ua/v2.0/json/getCities', options);
let orderList = JSON.parse(result.getContentText());
Logger.log(orderList);    
    
  };


I get this answer
{data=[], info=[], errorCodes=[], warningCodes=[], warnings=[], messageCodes=[], success=false, infoCodes=[], errors=[Data is invalid]}

What's my mistake? Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Stoyanov, 2020-06-01
@vladd56

Do this:

function getCities () {
  const formData = {
    // Ваш ключ API 2.0
    "apiKey": apiKey,
    
    "modelName": "Address",
    
    "calledMethod": "getCities"
  };
  
  const options = {
    "method": "POST",
    "headers": {
      "Content-Type": "application/json"
    },
    "payload": JSON.stringify(formData)
  };
  
  const url = "https://api.novaposhta.ua/v2.0/json/getCities";
  
  const response = UrlFetchApp.fetch(url, options);
  
  Logger.log( response.getContentText() );
  
  return response.getContentText();
};

---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question