curl:以称为file的多部分形式发布字段?

curl:以称为file的多部分形式发布字段?,curl,Curl,我想替换这个: curl -F 'file=@a.html' http://localhost:8080/convert 与 curl-F'file=Inline!'http://localhost:8080/convert 但这让我明白了:curl:(26)无法打开文件“html>a” 使用curl-d不起作用(可能是因为它不生成多部分体?) 如何在curl参数中嵌入内容,而不依赖对实际文件的引用?这个答案如何?我认为可能有几种解决办法。所以,请把这看作是其中之一。为了使用多部分/表单数据

我想替换这个:

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

curl-F'file=Inline!'http://localhost:8080/convert
但这让我明白了:
curl:(26)无法打开文件“html>a”

使用curl-d不起作用(可能是因为它不生成多部分体?)


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

这个答案如何?我认为可能有几种解决办法。所以,请把这看作是其中之一。为了使用多部分/表单数据发送
a.html
的内容,需要创建并发送请求正文。作为一个例子,这个答案假设如下

--boundaryboundary
Content-Disposition: form-data; name="file"; filename="a.html"
Content-Type: text/html

<html><body>Inline!</body></html>
--boundaryboundary--
  • a.html
    内联的
  • 在bash上使用curl
请求正文如下

--boundaryboundary
Content-Disposition: form-data; name="file"; filename="a.html"
Content-Type: text/html

<html><body>Inline!</body></html>
--boundaryboundary--
修改的卷曲样本: 修改后的curl示例如下所示<代码>内容类型
多部分/表单数据;边界=边界边界边界

curl -X POST \
  -H 'Content-Type: multipart/form-data; boundary=boundaryboundary' \
  -d $'--boundaryboundary\r\nContent-Disposition: form-data; name="file"; filename="a.html"\r\nContent-Type: text/html\r\n\r\n<html><body>Inline!</body></html>\r\n--boundaryboundary--' \
  'http://localhost:8080/convert'
curl-X POST\
-H'内容类型:多部分/表格数据;边界=边界边界'\
-d$'--boundaryboundary\r\n内容处理:表单数据;name=“file”;filename=“a.html”\r\n内容类型:text/html\r\n\r\n行\r\n--边界边界--'\
'http://localhost:8080/convert'
参考:

如果这不是你想要的,我很抱歉。

这个答案怎么样?我认为可能有几种解决办法。所以,请把这看作是其中之一。为了使用多部分/表单数据发送
a.html
的内容,需要创建并发送请求正文。作为一个例子,这个答案假设如下

--boundaryboundary
Content-Disposition: form-data; name="file"; filename="a.html"
Content-Type: text/html

<html><body>Inline!</body></html>
--boundaryboundary--
  • a.html
    内联的
  • 在bash上使用curl
请求正文如下

--boundaryboundary
Content-Disposition: form-data; name="file"; filename="a.html"
Content-Type: text/html

<html><body>Inline!</body></html>
--boundaryboundary--
修改的卷曲样本: 修改后的curl示例如下所示<代码>内容类型
多部分/表单数据;边界=边界边界边界

curl -X POST \
  -H 'Content-Type: multipart/form-data; boundary=boundaryboundary' \
  -d $'--boundaryboundary\r\nContent-Disposition: form-data; name="file"; filename="a.html"\r\nContent-Type: text/html\r\n\r\n<html><body>Inline!</body></html>\r\n--boundaryboundary--' \
  'http://localhost:8080/convert'
curl-X POST\
-H'内容类型:多部分/表格数据;边界=边界边界'\
-d$'--boundaryboundary\r\n内容处理:表单数据;name=“file”;filename=“a.html”\r\n内容类型:text/html\r\n\r\n行\r\n--边界边界--'\
'http://localhost:8080/convert'
参考:
如果这不是您想要的,我很抱歉。

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

curl--formstring'file=Inline!'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--
POST/convert HTTP/1.1
主机:本地主机:8080
用户代理:curl/7.59.0
接受:*/*
内容长度:172
内容类型:多部分/表单数据;边界=---------------------------2e4ba7fc50e9eec8
--------------------------2e4ba7fc50e9eec8
内容配置:表单数据;name=“文件”
内联!
--------------------------2e4ba7fc50e9eec8--
使用
--form string
参数代替-F,例如

curl--formstring'file=Inline!'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--
POST/convert HTTP/1.1
主机:本地主机:8080
用户代理:curl/7.59.0
接受:*/*
内容长度:172
内容类型:多部分/表单数据;边界=---------------------------2e4ba7fc50e9eec8
--------------------------2e4ba7fc50e9eec8
内容配置:表单数据;name=“文件”
内联!
--------------------------2e4ba7fc50e9eec8--

非常好。是的,这是可行的,而且似乎很容易处理。我希望不必手动构造表单数据,但事实上,只要知道换行符和boundaries@Chris卡罗尔:谢谢你的回复。我必须向你道歉。现在我注意到另一个答案。我认为hanshenrik的答案更简单,也更适合你的问题。所以请接受hanshenrik的回答。对于我无知的回答,我真的很抱歉。我同意表单字符串是最简单的东西,但由于您的回答既有效又演示了其他东西,因此它仍然是一个有价值的答案。@Chris F Carroll感谢您的回答和关注。太好了。是的,这是可行的,而且似乎很容易处理。我希望不必手动构造表单数据,但事实上,只要知道换行符和boundaries@Chris卡罗尔:谢谢你的回复。我必须向你道歉。现在我注意到另一个答案。我认为hanshenrik的答案更简单,也更适合你的问题。所以请接受hanshenrik的回答。对于我无知的回答,我真的很抱歉。我同意表单字符串是最简单的东西,但由于你的回答既有效又演示了其他东西,因此它仍然是一个有价值的答案。@Chris F Carroll感谢你的回答和关心。我认为你的回答更简单,更适合作为OP问题的答案。我可以从你的答案中学习。非常感谢。我真的很抱歉我无知的回答。虽然我不能接受,但我想投赞成票。我认为你的答案比OP问题的答案更简单、更合适。我可以从你的答案中学习。非常感谢。我真的很抱歉我无知的回答。虽然我不能接受,但我想向上投票。我在错误消息和手册页中都没有注意到的是,前导“我在错误消息和手册页中都没有注意到的是,前导”