Skip to content

Instantly share code, notes, and snippets.

@salacoste
Forked from yoav-lavi/requests.js
Created January 17, 2023 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salacoste/d06b5eb67961df57c9c584716d895b9d to your computer and use it in GitHub Desktop.
Save salacoste/d06b5eb67961df57c9c584716d895b9d to your computer and use it in GitHub Desktop.
request module for Scriptable
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: green; icon-glyph: file-code;
module.exports = {
post: async ({ url, body, headers = {} }) => {
const request = new Request(url);
request.body = JSON.stringify(body);
request.method = methods.post;
request.headers = {
...defaultHeaders,
...headers
};
return await request.loadJSON();
},
put: async ({ url, body, headers = {} }) => {
const request = new Request(url);
request.body = JSON.stringify(body);
request.method = methods.put;
request.headers = {
...defaultHeaders,
...headers
};
return await request.loadJSON();
},
get: async ({ url, headers = {} }) => {
const request = new Request(url);
request.method = methods.get;
request.headers = {
...defaultHeaders,
...headers
};
return await request.loadJSON();
}
};
const defaultHeaders = {
Accept: "application/json",
"Content-Type": "application/json"
}
const methods = {
get: "GET",
post: "POST",
put: "PUT"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment