fix: cors when content-type is set for simple requests

This commit is contained in:
liyasthomas
2021-08-22 22:44:07 +05:30
parent 5f27b59dc0
commit 294b1a5a7c
5 changed files with 43 additions and 20 deletions

View File

@@ -24,11 +24,20 @@
<template #trigger> <template #trigger>
<span class="select-wrapper"> <span class="select-wrapper">
<ButtonSecondary <ButtonSecondary
:label="contentType" :label="contentType || $t('state.none').toLowerCase()"
class="rounded-none ml-2 pr-8" class="rounded-none ml-2 pr-8"
/> />
</span> </span>
</template> </template>
<SmartItem
:label="$t('state.none').toLowerCase()"
:info-icon="contentType === null ? 'done' : ''"
:active-info-icon="contentType === null"
@click.native="
contentType = null
$refs.contentTypeOptions.tippy().hide()
"
/>
<SmartItem <SmartItem
v-for="(contentTypeItem, index) in validContentTypes" v-for="(contentTypeItem, index) in validContentTypes"
:key="`contentTypeItem-${index}`" :key="`contentTypeItem-${index}`"
@@ -44,7 +53,15 @@
</span> </span>
</div> </div>
<HttpBodyParameters v-if="contentType === 'multipart/form-data'" /> <HttpBodyParameters v-if="contentType === 'multipart/form-data'" />
<HttpRawBody v-else :content-type="contentType" /> <HttpRawBody v-else-if="contentType !== null" :content-type="contentType" />
<div
v-if="contentType == null"
class="flex flex-col text-secondaryLight p-4 items-center justify-center"
>
<span class="text-center pb-4">
{{ $t("empty.body") }}
</span>
</div>
</div> </div>
</template> </template>
@@ -59,11 +76,7 @@ export default defineComponent({
return { return {
validContentTypes: Object.keys(knownContentTypes), validContentTypes: Object.keys(knownContentTypes),
contentType: useStream( contentType: useStream(restContentType$, null, setRESTContentType),
restContentType$,
"application/json",
setRESTContentType
),
} }
}, },
}) })

View File

@@ -31,7 +31,7 @@
@click.native="clearContent('rawParams', $event)" @click.native="clearContent('rawParams', $event)"
/> />
<ButtonSecondary <ButtonSecondary
v-if="contentType.endsWith('json')" v-if="contentType && contentType.endsWith('json')"
ref="prettifyRequest" ref="prettifyRequest"
v-tippy="{ theme: 'tooltip' }" v-tippy="{ theme: 'tooltip' }"
:title="$t('action.prettify')" :title="$t('action.prettify')"

View File

@@ -31,6 +31,9 @@ export type HoppRESTReqBody =
body: string body: string
} }
| HoppRESTReqBodyFormData | HoppRESTReqBodyFormData
| {
contentType: null
}
export interface HoppRESTRequest { export interface HoppRESTRequest {
v: string v: string

View File

@@ -13,13 +13,17 @@ export interface EffectiveHoppRESTRequest extends HoppRESTRequest {
effectiveFinalURL: string effectiveFinalURL: string
effectiveFinalHeaders: { key: string; value: string }[] effectiveFinalHeaders: { key: string; value: string }[]
effectiveFinalParams: { key: string; value: string }[] effectiveFinalParams: { key: string; value: string }[]
effectiveFinalBody: FormData | string effectiveFinalBody: FormData | string | null
} }
function getFinalBodyFromRequest( function getFinalBodyFromRequest(
request: HoppRESTRequest, request: HoppRESTRequest,
env: Environment env: Environment
): FormData | string { ): FormData | string | null {
if (request.body.contentType === null) {
return null
}
if (request.body.contentType === "multipart/form-data") { if (request.body.contentType === "multipart/form-data") {
const formData = new FormData() const formData = new FormData()
@@ -93,11 +97,12 @@ export function getEffectiveRESTRequest(
} }
const effectiveFinalBody = getFinalBodyFromRequest(request, environment) const effectiveFinalBody = getFinalBodyFromRequest(request, environment)
effectiveFinalHeaders.push({ if (request.body.contentType)
active: true, effectiveFinalHeaders.push({
key: "content-type", active: true,
value: request.body.contentType, key: "content-type",
}) value: request.body.contentType,
})
return { return {
...request, ...request,

View File

@@ -37,8 +37,7 @@ export const defaultRESTRequest: HoppRESTRequest = {
preRequestScript: "", preRequestScript: "",
testScript: "", testScript: "",
body: { body: {
contentType: "application/json", contentType: null,
body: "",
}, },
} }
@@ -201,7 +200,7 @@ const dispatchers = defineDispatchers({
}, },
setContentType( setContentType(
curr: RESTSession, curr: RESTSession,
{ newContentType }: { newContentType: ValidContentTypes } { newContentType }: { newContentType: ValidContentTypes | null }
) { ) {
// TODO: persist body evenafter switching content typees // TODO: persist body evenafter switching content typees
if (curr.request.body.contentType !== "multipart/form-data") { if (curr.request.body.contentType !== "multipart/form-data") {
@@ -223,7 +222,10 @@ const dispatchers = defineDispatchers({
...curr.request, ...curr.request,
body: <HoppRESTReqBody>{ body: <HoppRESTReqBody>{
contentType: newContentType, contentType: newContentType,
body: curr.request.body.body, body:
newContentType === null
? undefined
: (curr.request.body as any).body,
}, },
}, },
} }
@@ -591,7 +593,7 @@ export function updateFormDataEntry(index: number, entry: FormDataKeyValue) {
}) })
} }
export function setRESTContentType(newContentType: ValidContentTypes) { export function setRESTContentType(newContentType: ValidContentTypes | null) {
restSessionStore.dispatch({ restSessionStore.dispatch({
dispatcher: "setContentType", dispatcher: "setContentType",
payload: { payload: {