admin管理员组

文章数量:1305065

I am using the IntelliJ HTTP Client to send a multipart POST request and would like to dynamically generate a filename in the pre-request script and pass it to the Content-Disposition header inside the request body.

I have a working request with a hardcoded filename:

#@name Upload file 
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="file.pdf"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

Now, I am trying to replace the static filename with the dynamically generated one by referencing the variable in the request body:

#@name Upload file - with variable
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="{{myFileName}}"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

This approach does not work, and the filename is not replaced with the expected value from request.variables.get("myFileName").

How can I correctly use a dynamically generated filename inside the Content-Disposition header of a multipart request in IntelliJ HTTP Client?

Is there a proper way to reference variables inside the multipart request body, or is this not supported?

I expected the variable {{myFileName}} to be replaced with the value set in the pre-request script (file414143.pdf) when executing the request. The expected result was a Content-Disposition header like this:

Content-Disposition: form-data; name="file"; filename="file414143.pdf"

However, instead of the expected filename, it appears that the variable is not resolved correctly, and the request fails. The filename remains as {{myFileName}} in the request payload rather than being substituted with its actual value.

I am using the IntelliJ HTTP Client to send a multipart POST request and would like to dynamically generate a filename in the pre-request script and pass it to the Content-Disposition header inside the request body.

I have a working request with a hardcoded filename:

#@name Upload file 
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="file.pdf"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

Now, I am trying to replace the static filename with the dynamically generated one by referencing the variable in the request body:

#@name Upload file - with variable
< {%
let myFileName = "file414143.pdf"; 
request.variables.set("myFileName", myFileName);
%}
POST {{url}}/files 
Authorization: Basic {{credentials}}
Content-Type: multipart/form-data; boundary=WebAppBoundary

--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="{{myFileName}}"
Content-Type: application/pdf

< ../myfile.pdf
--WebAppBoundary--

This approach does not work, and the filename is not replaced with the expected value from request.variables.get("myFileName").

How can I correctly use a dynamically generated filename inside the Content-Disposition header of a multipart request in IntelliJ HTTP Client?

Is there a proper way to reference variables inside the multipart request body, or is this not supported?

I expected the variable {{myFileName}} to be replaced with the value set in the pre-request script (file414143.pdf) when executing the request. The expected result was a Content-Disposition header like this:

Content-Disposition: form-data; name="file"; filename="file414143.pdf"

However, instead of the expected filename, it appears that the variable is not resolved correctly, and the request fails. The filename remains as {{myFileName}} in the request payload rather than being substituted with its actual value.

Share Improve this question asked Feb 4 at 13:25 MoritzMoritz 211 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

It works for me when I specify the filename without quotes. Instead of

Content-Disposition: form-data; name="file"; filename="{{myFileName}}";

use

Content-Disposition: form-data; name="file"; filename={{myFileName}};

According to MDN you may not need the quotes:

The quotes around the file name are optional, but are necessary if you use special characters in the file name, such as spaces.

If you want to preserve the quotes, you could put the whole header value in a variable:

< {%
    let myFileName = "file414143.pdf";
    request.variables.set("contentDisposition", "form-data; name=\"file\"; filename=\"" + myFileName + "\";");
%}
...
Content-Disposition: {{contentDisposition}}

I'd still file a bug report with Jetbrains, though.

本文标签: httpclientUse dynamically generated filename in IntelliJ HTTP Client multipart requestStack Overflow