首页 > 解决方案 > curl:以称为文件的多部分形式发布一个字段?

问题描述

我想替换这个:

curl -F 'file=@a.html' http://localhost:8080/convert

curl -F 'file=<html><body>Inline!</body></html>' http://localhost:8080/convert

但这只是让我:curl: (26) couldn't open file "html><body>a</body></html>"

使用 curl -d 不起作用(大概是因为它不会生成多部分主体?)

如何在 curl 参数中嵌入内容,而不是依赖对实际文件的引用?

标签: curl

解决方案


使用--form-string参数而不是 -F,例如

curl --form-string 'file=<html><body>Inline!</body></html>' http://localhost:8080/convert

产生

POST /convert HTTP/1.1
Host: localhost:8080
User-Agent: curl/7.59.0
Accept: */*
Content-Length: 172
Content-Type: multipart/form-data; boundary=------------------------2e4ba7fc50e9eec8

--------------------------2e4ba7fc50e9eec8
Content-Disposition: form-data; name="file"

<html><body>Inline!</body></html>
--------------------------2e4ba7fc50e9eec8--

推荐阅读