使用绝对或相对路径在 curl 请求中发送 json 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16615900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending json files in curl requests with absolute or relative paths
提问by Classified
Just wondering how I can send a curl command with the -d option specifying a file with its path and not a file in the current directory.
只是想知道如何发送带有 -d 选项的 curl 命令,指定文件及其路径而不是当前目录中的文件。
This is what I'm getting when I try to test my app with the json file in the local dir. Both the app and myself are happy:
这就是我尝试使用本地目录中的 json 文件测试我的应用程序时得到的结果。应用程序和我自己都很高兴:
curl -XPOST -H 'Content-Type:application/json' -d @all_fields.json http://testcomp.lab.net:8080/stats -v -s
* About to connect() to testcomp.lab.net port 8080
* Trying 10.93.2.197... connected
* Connected to testcomp.lab.net (10.93.2.197) port 8080
> POST /stats HTTP/1.1
> User-Agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
> Host: testcomp.lab.net:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 2882
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
HTTP/1.1 200 OK
< Content-Length: 0
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0
This is what I'm getting when I specify a json file that's in another directory
这就是我在指定另一个目录中的 json 文件时得到的结果
curl -XPOST -H 'Content-Type:application/json' -d @json/all_fields.json http://testcomp.lab.net:8080/stats -v -s
"Invalid json for Java type interface java.util.List"
Warning: Couldn't read data from file "json/all_fields.json", this makes an
Warning: empty POST.
<snip snip>
<snip snip>
< HTTP/1.1 400 Bad Request
< Content-Type: application/json
< Transfer-Encoding: chunked
* Connection #0 to host testcomp.lab.net left intact
* Closing connection #0
I didn't see anything in the man page for curl for specifying directories for files passed in as data. Am I unfortunately limited to files in the local directory or is there a special way to specify files in different directories? Thanks in advance for your help.
我在 curl 的手册页中没有看到任何用于为作为数据传入的文件指定目录的内容。不幸的是,我是否仅限于本地目录中的文件,还是有一种特殊的方法可以指定不同目录中的文件?在此先感谢您的帮助。
回答by Perception
The -d @command option accepts any resolvable file path, as long as the path actually exists. So you could use:
该-d @命令选项接受任何解析文件的路径,只要路径确实存在。所以你可以使用:
- a path relative to the current directory
- a fully qualified path
- a path with soft-links in it
- and so on
- 相对于当前目录的路径
- 完全合格的路径
- 包含软链接的路径
- 等等
To wit, just the same as hundreds of other *Nix style commands. One quick note, the -doption will attempt to url encode your data, which from what you describe isn't actually what you want. You should use the --data-binaryoption instead. Something like this:
也就是说,就像数百个其他 *Nix 风格的命令一样。一个快速说明,该-d选项将尝试对您的数据进行 url 编码,根据您的描述,这实际上并不是您想要的。您应该改用该--data-binary选项。像这样的东西:
curl -XPOST
-H 'Content-Type:application/json'
-H 'Accept: application/json'
--data-binary @/full/path/to/test.json
http://localhost:8080/easy/eservices/echo -v -s

