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:

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: