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.

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:

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

这就是我在另一个目录中指定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

在curl的手册页中,我没有看到为作为数据传入的文件指定目录的任何内容.不幸的是,我只限于本地目录中的文件,还是有特殊的方法来指定不同目录中的文件?提前感谢你的帮助.

推荐答案

-d @命令选项接受任何可解析的文件路径,只要该路径实际存在.所以你可以用:

  • 相对于当前目录的路径
  • 完全限定的路径
  • 一条有软链接的路径
  • and so on

也就是说,就像数百个其他*Nix样式的命令一样.需要注意的是,-d选项将try 对您的数据进行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

Json相关问答推荐

基于两个条件替换扁平化的SON中的值

在T—SQL中将STR_AGG与JSON_ARRAY结合起来

将json数组反序列化为选项 struct

由于无效的UTF-8开始字节0xa0,JSON被拒绝,但编码似乎有效

褐煤面积图中的分选问题

VBA json按特定属性名称提取所有数据

在Snowflake中查询JSON时,属性名称是否支持绑定参数?

递归解码嵌套列表(具有任意深度的列表列表)

如何使NiFi将数据库单列中的多个值转化为Solr索引中的数组?

我需要在 mongodb compass 中检索索引(编号 1)信息

提交后使用 Rails 7 结合 JSON 标签进行标记

Golang / Go - 如果 struct 没有字段,如何将其编组为空?

如何在 Node.js 中解析包含NaN的 JSON 字符串

UTF-8 字符编码之战 json_encode()

未捕获的类型错误:无法读取 null 的属性props

如何在 Eclipse 中安装和使用 JSON 编辑器?

Newtonsoft Json 将值 {null} 转换为类型System.Int32时出错

PHP json_encode json_decode UTF-8

在 .NET 中缩小缩进的 JSON 字符串

我如何反序列化以杰克逊为单位的时间戳?