Fix form post data parsing (#1248)

Co-authored-by: Qing Ye <ye.qing@go-jek.com>
This commit is contained in:
YE Qing
2020-10-07 11:21:03 +08:00
committed by GitHub
parent 2ccd053b0a
commit ce5fb78bcd

View File

@@ -36,16 +36,25 @@ export const NodeJsRequestCodegen = {
} }
if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) { if (["POST", "PUT", "PATCH", "DELETE"].includes(method)) {
let requestBody = rawInput ? rawParams : rawRequestBody let requestBody = rawInput ? rawParams : rawRequestBody
let reqBodyType = 'formData' let reqBodyType = "formData"
if (isJSONContentType(contentType)) { if (isJSONContentType(contentType)) {
requestBody = `JSON.stringify(${requestBody})` requestBody = `JSON.stringify(${requestBody})`
reqBodyType = 'body' reqBodyType = "body"
} else if (contentType.includes("x-www-form-urlencoded")) { } else if (contentType.includes("x-www-form-urlencoded")) {
if (requestBody.indexOf('=') > -1) { const formData = []
const rq = requestBody.split('=') if (requestBody.indexOf("=") > -1) {
requestBody = `{"${rq[0]}": "${rq[1]}"}` requestBody.split("&").forEach((rq) => {
const [key, val] = rq.split("=")
formData.push(`"${key}": "${val}"`)
})
} }
reqBodyType = 'form' if (formData.length) {
requestBody = `{${formData.join(", ")}}`
}
reqBodyType = "form"
} else if (contentType.includes("application/xml")) {
requestBody = `\`${requestBody}\``
reqBodyType = "body"
} }
if (contentType) { if (contentType) {
genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`) genHeaders.push(` "Content-Type": "${contentType}; charset=utf-8",\n`)