AnWeber / vscode-httpyac

Quickly and easily send REST, Soap, GraphQL, GRPC, MQTT and WebSocket requests directly within Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=anweber.vscode-httpyac
MIT License
222 stars 20 forks source link

Variable replacement doesn't work when file is injected in multipart/form-data request #258

Closed wiadam closed 5 months ago

wiadam commented 5 months ago

Problem description

When a multipart/form-data request is sent, variables are not replaced in body payload of the request if a file is injected in one part of the form.

It is expected to have variables replaced regardless of what other part is injected in request body. BTW, I find awesome this VSCode extension :)

Steps to reproduce

Non functional case

For following request (with variable AND file injection):

# Import variables
# @import ../workdir/variables.http

###
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebKitFormBoundary

--WebKitFormBoundary
Content-Disposition: form-data; name="text"

{{projectName}}
--WebKitFormBoundary
Content-Disposition: form-data; name="invoice"; filename="invoice.pdf"
Content-Type: application/pdf

< ./dummy.pdf
--WebKitFormBoundary--

Response is following

{
  "args": {},
  "data": "",
  "files": {
    "invoice": "1"
  },
  "form": {
    "text": "{{projectName}}"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Length": "238",
    "Content-Type": "multipart/form-data; boundary=WebKitFormBoundary",
    "Host": "httpbin.org",
    "User-Agent": "httpyac",
    "X-Amzn-Trace-Id": "Root=1-65bc352a-09deb72a513ef6ff60dfffb6"
  },
  "json": null,
  "origin": "***REDACTED***",
  "url": "https://httpbin.org/post"
}

Functional case

If request doesn't have file injection, variables replacement work fine. For request (with only variable):

# Import variables
# @import ../workdir/variables.http

###
POST https://httpbin.org/post
Content-Type: multipart/form-data; boundary=WebKitFormBoundary

--WebKitFormBoundary
Content-Disposition: form-data; name="text"

{{projectName}}
--WebKitFormBoundary--

Response is:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "text": "test"
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate, br",
    "Content-Length": "97",
    "Content-Type": "multipart/form-data; boundary=WebKitFormBoundary",
    "Host": "httpbin.org",
    "User-Agent": "httpyac",
    "X-Amzn-Trace-Id": "Root=1-65bc357b-7107433c0fd312492c17692b"
  },
  "json": null,
  "origin": "***REDACTED***",
  "url": "https://httpbin.org/post"
}

Variables file

Content of ./workdir/variables.http is following:

@projectName=test
AnWeber commented 5 months ago

Need to change order of these two lines: https://github.com/AnWeber/httpyac/blob/main/src/plugins/core/registerCorePlugin.ts#L28-L29

AnWeber commented 5 months ago

@wiadam thx for your contribution. I released a new version containing the fix.