From 764d001ef65815a6fabbf9e8ef706aaef286c488 Mon Sep 17 00:00:00 2001 From: Hydrophobefireman Date: Sun, 10 May 2020 20:44:51 +0530 Subject: [PATCH 1/3] produce valid output when showing/copying code Currently when copying a json request as fetch, it outputs a javascript object instead of a stringified version, which is not valid for a fetch request. --- pages/index.vue | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index a8e110c6c..65a3707fa 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1941,7 +1941,7 @@ export default { requestString.push( `xhr.setRequestHeader('Content-Type', '${this.contentType}; charset=utf-8')` ) - requestString.push(`xhr.send(${requestBody})`) + requestString.push(`xhr.send("${requestBody}")`) } else { requestString.push("xhr.send()") } @@ -1960,7 +1960,13 @@ export default { headers.push(` "Authorization": "Bearer ${this.bearerToken}",\n`) } if (["POST", "PUT", "PATCH"].includes(this.method)) { - const requestBody = this.rawInput ? this.rawParams : this.rawRequestBody + let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody + if (this.contentType.includes("json")) { + requestBody = `JSON.stringify("${requestBody}")`; + } else if (this.contentType.includes("x-www-form-urlencoded")) { + requestBody = `"${requestBody}"`; + } + requestString.push(` body: ${requestBody},\n`) headers.push(` "Content-Length": ${requestBody.length},\n`) headers.push(` "Content-Type": "${this.contentType}; charset=utf-8",\n`) From fec3c309be459c1dc812ea99697cd72fd45ed9c2 Mon Sep 17 00:00:00 2001 From: Hydrophobefireman Date: Sun, 10 May 2020 20:48:12 +0530 Subject: [PATCH 2/3] fix for xhr and keep code style intact --- pages/index.vue | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 65a3707fa..2e9ddb23c 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1936,12 +1936,17 @@ export default { }) } if (["POST", "PUT", "PATCH"].includes(this.method)) { - const requestBody = this.rawInput ? this.rawParams : this.rawRequestBody + let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody + if (this.contentType.includes("json")) { + requestBody = `JSON.stringify("${requestBody}")` + } else if (this.contentType.includes("x-www-form-urlencoded")) { + requestBody = `"${requestBody}"` + } requestString.push(`xhr.setRequestHeader('Content-Length', ${requestBody.length})`) requestString.push( `xhr.setRequestHeader('Content-Type', '${this.contentType}; charset=utf-8')` ) - requestString.push(`xhr.send("${requestBody}")`) + requestString.push(`xhr.send(${requestBody})`) } else { requestString.push("xhr.send()") } @@ -1962,9 +1967,9 @@ export default { if (["POST", "PUT", "PATCH"].includes(this.method)) { let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody if (this.contentType.includes("json")) { - requestBody = `JSON.stringify("${requestBody}")`; + requestBody = `JSON.stringify("${requestBody}")` } else if (this.contentType.includes("x-www-form-urlencoded")) { - requestBody = `"${requestBody}"`; + requestBody = `"${requestBody}"` } requestString.push(` body: ${requestBody},\n`) From e0aede3fc418e8bb38fbac2113ee99bc8cee5cd4 Mon Sep 17 00:00:00 2001 From: Hydrophobefireman Date: Sun, 10 May 2020 21:26:44 +0530 Subject: [PATCH 3/3] Update index.vue --- pages/index.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/index.vue b/pages/index.vue index 2e9ddb23c..8843c8ee8 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -1938,7 +1938,7 @@ export default { if (["POST", "PUT", "PATCH"].includes(this.method)) { let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody if (this.contentType.includes("json")) { - requestBody = `JSON.stringify("${requestBody}")` + requestBody = `JSON.stringify(${requestBody})` } else if (this.contentType.includes("x-www-form-urlencoded")) { requestBody = `"${requestBody}"` } @@ -1967,7 +1967,7 @@ export default { if (["POST", "PUT", "PATCH"].includes(this.method)) { let requestBody = this.rawInput ? this.rawParams : this.rawRequestBody if (this.contentType.includes("json")) { - requestBody = `JSON.stringify("${requestBody}")` + requestBody = `JSON.stringify(${requestBody})` } else if (this.contentType.includes("x-www-form-urlencoded")) { requestBody = `"${requestBody}"` }