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 badge1 Answer
Reset to default 0It 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
版权声明:本文标题:httpclient - Use dynamically generated filename in IntelliJ HTTP Client multipart request? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741761882a2396455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论