将 multipart/form-data 请求调整为在同一请求中包含图像和 json 的curl
我需要重写从浏览器中提取的这个curl请求以从bash执行它,它是一个multipart/form-data,它将图像的上传与同一请求中的json结合在一起,我已经进行了几次测试来替换原始数据两个 -F 的数据,但服务器响应错误。 正确的方法是什么? 我也无法理解 WebKitFormBoundary 旁边的键是一个看起来随机的值,它只是一个分隔符吗?我可以使用任何东西吗?
我删除了我认为不相关的标题,
curl 'https://localhost' \
-H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXXXXXXXXXXXXXXXX'
-H 'Accept: */*' \
--data-raw $'------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="image"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="item"; filename="blob"\r\nContent-Type: application/json\r\n\r\n{"item1":"xxxx","item2":"yyyy"}\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX--\r\n' \
谢谢
I need to rewrite this curl request extracted from the browser to execute it from bash, it is a multipart/form-data that combines the upload of an image together with a json in the same request, I have carried out several tests substituting the raw data for two -F but the server responds with error.
What would be the correct way?
I also fail to understand the key that is next to WebKitFormBoundary is a value that seems random, is it just a separator? can i use anything?
I have removed the headers that I considered irrelevant
curl 'https://localhost' \
-H 'Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryXXXXXXXXXXXXXXXX'
-H 'Accept: */*' \
--data-raw
Thanks
------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="image"; filename="image.jpg"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX\r\nContent-Disposition: form-data; name="item"; filename="blob"\r\nContent-Type: application/json\r\n\r\n{"item1":"xxxx","item2":"yyyy"}\r\n------WebKitFormBoundaryXXXXXXXXXXXXXXXX--\r\n' \
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,是的,实际上说是的。它应该是一个随机保证的唯一字符串,用作分隔符来表示表单的开始和结束。如果您事先知道上传的内容都不包含字符串
stackoverflow
,您可以使用内容类型:多部分/表单数据;例如,boundary=stackoverflow
(但如果您上传实际包含字符串stackoverflow
的文件,这可能会中断!),但您应该让curl 自动生成该字符串,而不是自己指定它, curl 将自动生成一个实际上保证唯一的字符串。...如果您猜错了,服务器可能会因为缺少必需的标头而给您一个错误。假设您在删除不相关标头时没有犯任何错误,但我认为
该命令生成的请求是错误的:
如您在上面看到的,
接受:*/* 无论如何都是curl默认的accept-header,所以不需要指定它,curl会自动生成边界
------------------------ --7d0262831ce8f248所以也无需手动指定边界。
yes and yes and practically speaking yes. it's supposed to be a random-guaranteed-unique string used as separator to signal start and end of forms.. if you know beforehand that none of your uploads contains the string
stackoverflow
, you could useContent-Type: multipart/form-data; boundary=stackoverflow
for example (but this may break if you upload a file actually containing the stringstackoverflow
!), but you should let curl generate that string automatically, rather than specifying it yourself, curl will auto-generate a string that is practically guaranteed to be unique.... if you guessed wrong, the server may give you an error for missing a required header. assuimg that you didn't make any mistakes in removing
irrelevant headers
though, i think it'sthe request generated by that command is roghly:
as you can see above,
Accept: */*
is curl's default accept-header anyway, so there's no need to specify it, and curl auto-generated the boundary------------------------7d0262831ce8f248
so there's no need to manually specify a boundary either.