BetterRequest - A better version of the Request API

I’ve seen lately that some of you struggle to get the Request API to send what they want like I did some time ago. Therefore I made this wrapper of the Request API to help with the encoding and setting the correct mime type.

It does that for these types:

  • json (application/json)
  • forms (application/x-www-form-urlencoded)
  • Data (the Scriptable API) (application/octet-stream)
  • strings (text/plain)

You can do everything what you can do with the original Request API. Even if it gets new properties, they will also be available on this wrapper.

This also fixes ignored properties when assigning them with the . operator:

let req = new Request(url);
req.header = {}; = "bar";
log(req.header); // prints "{}"

Use it like the normal Request API, but use the properties json and form for the first two content types listed above and body for all other

// import this script
let BetterRequest = importModule("BetterRequest");

// use it
let req = new BetterRequest("");
req.method = "post";
req.json = {
	foo: "bar"
let res = await req.loadJSON();

I’ve tested it a little and I’ve also written some tests at the bottom of the script. To run them, simply run this script on its own and it will print the results in the console.


Thanks for sharing. It’s great that you’re turning this into modules! :clap: :smile:

I’ve updated the script!

  • Changed example in the comment at the top to not suggest that Request.body = {} works for sending JSON
  • Fixed missing content type for text/plain
1 Like