Fixing some issues discovered with POSTing JSON.

This commit is contained in:
mike12345567 2021-06-23 19:28:33 +01:00
parent e25c6d4f3f
commit c4cac4a0b1
1 changed files with 20 additions and 3 deletions

View File

@ -77,14 +77,31 @@ module.exports.run = async function ({ inputs }) {
requestBody.length !== 0 &&
BODY_REQUESTS.indexOf(requestMethod) !== -1
) {
request.body = JSON.parse(requestBody)
request.body = requestBody
request.headers = {
"Content-Type": "application/json",
}
}
try {
// do a quick JSON parse if there is a body, to generate an error if its invalid
if (request.body) {
JSON.parse(request.body)
}
const response = await fetch(url, request)
const contentType = response.headers.get("content-type")
const success = response.status === 200
let resp
if (!success) {
resp = response.statusText
} else if (contentType && contentType.indexOf("application/json") !== -1) {
resp = await response.json()
} else {
resp = await response.text()
}
return {
response: await response.json(),
success: response.status === 200,
response: resp,
success: success,
}
} catch (err) {
/* istanbul ignore next */