HTTP Request: JSON Body

If I’m reading the documentation right, it’s currently only possible to include one string or number in the body of a request. It would be great if JSON is allowed too. Or any of the following options:

// Create request
let flow = new Request('https://some.api.site/somecontroller/');
flow.method = "POST";

// Set Authorization key 
flow.headers = {"Some_Way_of_Autorization" : "the key"};

// Set body (option 1)
flow.birthdate = "1996-01-25"; 
flow.name = "Raimond van Mouche"; 

// Set body (option 2)
flow.body = {"birthdate" : "1996-01-25", "name" : "Raimond van Mouche"};

// Set body (option 3)
flow.body.birthdate = "1996-01-25"; 
flow.body.name = "Raimond van Mouche"; 

let json = await flow.loadJSON();

console.log(json);

With this ability, scriptable could be make complete API-usage through Siri and itself possible. Which would make my life quite a bit easier :wink:

1 Like

Hey,

Thanks for your feedback :smiley:

I’m not totally sure I understand what you want to do. Strictly speaking, when you are sending JSON in the body of an HTTP request you are sending a string. Wouldn’t something like this work in your case?

let body = { "birthdate": "1996-01-25", "name": "Raimond van Mouche" }
let req = new Request("https://api.example.com")
req.headers = { "Authorization": "Token ..." }
req.body = JSON.stringify(body)
let json = await req.loadJSON()
2 Likes

Hey Simon,

Thanks for your reply. After reading it I realised the problem is on the API-side of things. It uses php to look for “birthday” in POST, instead of checking for JSON and then decoding it.

So I’ll just build a small server script to convert it and include JSON support in the planned refactoring. :slight_smile:

Thank you so much for posting this!

I have been failing to put a proper body in a PUT request for at least half an hour, but now I can (use Scriptable to let IFTTT generate push notifications - to test them)

:smile:

I need to have an object named .json but it doesn’t work.
I tried to use .body but I really need the .json.
Anyone know how to get around this?
This is how it looks in python
“response = requests.post(url, headers=headers,json=payload)”
payload is {“OneTimePassword”: “”}

Same as the example above but you need to set req.method = ‘POST’

thanks for your replay but I tried that and it doesn’t work.
This is the code

const apiUr = `https://xxx.yyy`;
const reques = new Request(apiUr);
reques.headers = {'authorization': `Basic zzzzzz` };
reques.method = "POST";
reques.json = {"OneTimePassword":""} 
log(reques)

the log look like this

{"url":"https://xxx.yyy","method":"POST","headers":{"authorization":"Basic zzz"},"timeoutInterval":60,"response":null,"allowInsecureRequest":false}

it seems that json is not added?
anyway I get error when doing this, I have a code in Python that works fine so I know it should work

You need to do this:

reques.body = JSON.stringify({"OneTimePassword":""})

And set the content type:

reques.header = {
  "authorization": "Basic zzzzzz",
  "content-type": "application/json",
}