如何将文件和JSON数据与Curl一起发布? [英] How Can I Post Files and JSON Data Together With Curl?

查看:488
本文介绍了如何将文件和JSON数据与Curl一起发布?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  curl -i -F file = @。/ File .xlsm -F name = file -X POST http://example.com/new_file/ 

现在我想发送一些关于文件的信息(如JSON)以及文件。

  curl -i -HContent-类型:application / json-d{metadata:{comment:提交一个新的数据集,current:false},sheet:1,row:7}'-F file = @。/ File.xlsm -F name = file http://example.com/new_file/ 



< curl对于以这种完全不正确的方式使用而言是非常暴躁的,在这种情况下,它说你只能选择一个HTTP请求!好吧,那么我怎么把文件附件和这些POST变量放到一个单一的curl HTTP请求中去?解析方案

可以添加另一个表单域:

$ $ p $ curl -X POST http:// someurl / someresource -F upload = @ / path / to / some / file -F data ={\test \:\test\}

注意:由于内容类型的原因,这并不等同于将json发送到web服务。


I've been posting a file with this curl command:

curl -i -F file=@./File.xlsm -F name=file -X POST http://example.com/new_file/

Now I want to send some information about the file (as JSON) along with the file.

curl -i -H "Content-Type: application/json" -d '{"metadata": {"comment": "Submitting a new data set.", "current": false }, "sheet": 1, "row": 7 }' -F file=@./File.xlsm -F name=file http://example.com/new_file/

Curl is very grumpy about being used in this completely incorrect way, and in this case it says "You can only select one HTTP request!" OK, fair enough, so how do I get the file attachment and those POST variables into a single curl HTTP request?

解决方案

You could just add another form field:

curl -X POST http://someurl/someresource -F upload=@/path/to/some/file -F data="{\"test\":\"test\"}"

Note: due to the content type, this does not really equate to sending json to the web service.

这篇关于如何将文件和JSON数据与Curl一起发布?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆