2

I'm trying to make a post with google Apps.

function makePost(url,page,status,token) {
var form = {
    "page" : 1,
    "statuses[]": status.toString().toLowerCase(),    
}
var options = {
    'method': 'post',
    'headers' : {"Authorization" : "BASIC "+token},
    'contentType': 'application/x-www-form-urlencoded',
    'payload' : form,
};
var response = UrlFetchApp.fetch(url, options);
var jsonResponse = JSON.parse(response.getContentText());
return jsonResponse;}

The problem seems to be that page is being past as 1.0 what makes the destination url return an error when tries to cast.

Already tried the answer in How to convert a string to number using Google Apps Script .

I cant pass it as a String , because in that case the API returns the result with an strange behaviour.

Is it possible to pass this as Integer without being change it to 1.0 ?

Any suggestion or help will be helpfull, thanks

UPDATE: So , i couldnt find a way to accomplish this. UrlFetchApp changes it every time. Lucky for me we could solve it by order the response in the API , and the behaviour passing a string stoped being erratic.

Thanks for the help and suggestions!

  • what will happen if you pass 1 as a string as "page" : "1" ? – Chris Chen Mar 12 '18 at 03:30
  • no , they are not asynchonous. I debug and see that the request is being send with 1.0 – Rodrigo Lopez Guerra Mar 12 '18 at 03:31
  • If i passed as "1" , it works but is not what API expected so doesnt give me the rigth answer. With curl works ok with -d "page=1" and incorrectly with -d "page='1'" @ChrisChen – Rodrigo Lopez Guerra Mar 12 '18 at 03:32
  • 1
    I would argue that this is an issue with your form not recognizing that 1.0 is the same as 1. In [JavaScript, all numbers are `float`s](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type). You cannot have a number that does not have the decimal portion. – tehhowch Mar 12 '18 at 04:09
  • @RodrigoLopezGuerra assuming you do not have control over the server end reciving this posts? Then how about 'payload' : JSON.stringify(form) ? – Chris Chen Mar 12 '18 at 04:38
  • @ChrisChen i tried , but when i stringify the data , parameters are ignored in destination API – Rodrigo Lopez Guerra Mar 12 '18 at 12:33

1 Answers1

1

Same here connecting with Stripe REST which enforces a form-based body. It must be a bug of UrlFetchApp.

When I use

payload: {
  amount: 1000,
  currency: 'USD',
}

UrlFetchApp always encoded it into, note the decimal .0

"payload":"amount=1000.0&currency=USD"

But I resolved it by converting it to a String.

payload: {
  amount: String(1000),
  currency: 'USD',
}

Oddly, the payload will now be encoded correctly into

"payload":"amount=1000&currency=USD"

since the form-urlencoded doesn't specify the type of value, in this case, either a numeric String or an Integer. It's the server's implementation for interpreting the value.

Community
  • 1
  • 1
jasonleakey
  • 1,876
  • 1
  • 12
  • 5
  • Did you miss the comment above? **All** numbers in JavaScript are floating point. There is no `int`. – tehhowch Mar 05 '19 at 12:15