-
-
Save salacoste/d06b5eb67961df57c9c584716d895b9d to your computer and use it in GitHub Desktop.
request module for Scriptable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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