Fixing some issues discovered with POSTing JSON.

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

View File

@ -77,14 +77,31 @@ module.exports.run = async function ({ inputs }) {
requestBody.length !== 0 && requestBody.length !== 0 &&
BODY_REQUESTS.indexOf(requestMethod) !== -1 BODY_REQUESTS.indexOf(requestMethod) !== -1
) { ) {
request.body = JSON.parse(requestBody) request.body = requestBody
request.headers = {
"Content-Type": "application/json",
}
} }
try { 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 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 { return {
response: await response.json(), response: resp,
success: response.status === 200, success: success,
} }
} catch (err) { } catch (err) {
/* istanbul ignore next */ /* istanbul ignore next */