Home > Blockchain >  API does not return right data in Google Apps Script
API does not return right data in Google Apps Script

Time:10-04

I'm trying to fetch Wise API to save the exchange rate to Google Sheets, but for some reason the data returned is not correct.

It works in Postman but when I make same request in Google Sheets via Google Apps Script data just don't match.

curl example from documentation

  https://api.transferwise.com/v3/quotes/ \
  -H "Authorization: Bearer <your client credentials token>"
  -H 'Content-type: application/json' \
  -d '{
    "sourceCurrency": "GBP",
    "targetCurrency": "USD",
    "sourceAmount": null,
    "targetAmount": 110 }'

Response in Postman:

enter image description here

Request in Google Apps Script:

const url = "https://api.transferwise.com/v3/quotes/";
    const response = UrlFetchApp.fetch(url, {
        "method": "POST",
        "headers": {
            "Authorization": "Bearer   mytoken",
            "Content-Type": "application/json"
        },
        "muteHttpExceptions": true,
        "followRedirects": true,
        "validateHttpsCertificates": true,
        "contentType": "application/json",
        "payload": JSON.stringify({"\\\"sourceCurrency\\\"":"\\\"EUR\\\"","\\\"targetCurrency\\\"":"\\\"USD\\\"","\\\"sourceAmount\\\"":"null","\\\"targetAmount\\\"":"1000"})
    });

  const data = JSON.parse(response.getContentText())

    Logger.log("Response code is %s", response.getResponseCode());
    Logger.log(data.rate);
} 

Response on console:

enter image description here

API is returning "1.0176" instead of "0.98015" and I can't discovery what I'm doing wrong.

CodePudding user response:

Change your payload to:

 "payload": `{
  "sourceCurrency": "EUR",
  "targetCurrency": "USD",
  "souceAmount": 1000,
  "targetAmount": null 
 }`
  • Related