Request return empty

Hi,

I am trying to make a widget that show the exchange quote from USD to GBP.

This is the sample code I have written for testing. When I try the API in postman, it returns fine. However, when I try it in Scriptable, it gives me the following error:

Error: The data couldn’t be read because it isn’t in the correct format.

Anyone know what is the problem?


const w = new ListWidget()
const url = "https://api.sandbox.transferwise.tech/v2/quotes/"
const body = { "sourceCurrency": "USD","targetCurrency": "GBP","sourceAmount": 60000,"targetAmount": null }
const resp = await post(url,body)

Script.setWidget(w)
Script.complete()

async function post(url,body) {
  const request = new Request(url)
  request.method = "POST"
  request.body = body
  console.log(body)
  const result = await request.loadJSON()
  console.log(result)
  return result
}

Have you done any authentication?

This API doesn’t need any authentication. You can try yourself with the body above.

I did try it with the above in Postman and I got an unauthorised message, which was why I queried it. The documentation I saw on their site, albeit it was for v1 rather v2, suggested that an authentication token was required.

very strange in this case. I have just created a postman post call and got result, screenshots below.

In Transferwise API document, it also said that

Note that this endpoint does not require a token to create the resource, however, since it is just an example, the returned quote has no ID so can’t be used later to create a transfer.

No authorisation/token is provided in the call.

Okay, with the excerpt of the documentation I was able to find the content of what seems to be the only call (to the v2 end point) that does not require authentication.

I’m not sure why Postman was returning an authorisation error for me earlier, but I’m now off my PC and back on my iPad, and was able to get the following simplified version working to get a return. You should be able to translate this to your async function and widget version.

I think the key changes are the addition of the header to tell the API you are sending JSON, and the conversion of the body object attribute set to a string for transfer as part of the request string.

const url = "https://api.sandbox.transferwise.tech/v2/quotes/";
let body = {};
body.sourceCurrency = "USD";
body.targetCurrency = "GBP";
body.sourceAmount = 60000;
let request = new Request(url)
request.headers = {"Content-type" : "application/json"}
request.method = "POST"
request.body = JSON.stringify(body)
console.log(body)
let result = await request.loadString()
console.log(result)

Hope that gets it working for you.

thanks, it works!! I assumed there will be some default head…obviously, I am thinking too much.