我可以通过执行以下命令成功创建一个位置:

$ curl -vX POST https://server/api/v1/places.json -d "
  auth_token=B8dsbz4HExMskqUa6Qhn& \
  place[name]=Fuelstation Central& \
  place[city]=Grossbeeren& \
  place[address]=Buschweg 1& \
  place[latitude]=52.3601& \
  place[longitude]=13.3332& \
  place[washing]=true& \
  place[founded_at_year]=2000& \
  place[products][]=diesel& \
  place[products][]=benzin \
"

The server returns HTTP/1.1 201 Created.
Now I want to store the payload in a JSON file which looks like this:

// testplace.json
{
  "auth_token" : "B8dsbz4HExMskqUa6Qhn",
  "name" : "Fuelstation Central",
  "city" : "Grossbeeren",
  "address" : "Buschweg 1",
  "latitude" : 52.3601,
  "longitude" : 13.3332,
  "washing" : true,
  "founded_at_year" : 2000,
  "products" : ["diesel","benzin"]
}

So I modify the command to be executed like this:

$ curl -vX POST http://server/api/v1/places.json -d @testplace.json

返回HTTP/1.1 401 Unauthorized次失败.为什么?

推荐答案

curl发送默认内容类型为application/x-www-form-urlencoded的POST请求.如果要发送JSON请求,则必须指定正确的内容类型标头:

$ curl -vX POST http://server/api/v1/places.json -d @testplace.json \
--header "Content-Type: application/json"

但是,只有在服务器接受json输入的情况下,这才能起作用.URL末尾的.json可以仅指示output是JSON,这并不一定意味着它也将处理JSON input.API文档应该会给您一个提示,告诉您它是否这样做.

The reason you get a 401 and not some other error is probably because the server can't extract the auth_token from your request.

Json相关问答推荐

SSIS Kingswaysoft Json源动态 node 名称

如何在JMESPath中区分空和假?

JQ如何获取特定子元素的所有父母

如何从字符串中创建一个逗号分隔的列表,由 API 中的 JSON 对象内的编号空格分隔?

判断golang中解析的json响应中是否存在所需的json键(不是值)

如何从条带订阅响应对象中正确获取 struct 项?

流导入错误:重新上传时不存在布局释放 UUID

通过sql查询读取嵌套Json

Go - JSON 验证抛出错误,除非我在 struct 中使用指针.为什么?

在 Flutter 中将对象转换为可编码对象失败

在 Apache Spark 中读取多行 JSON

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

在 JSON 字符串中react i18n 换行符

Jackson Scala 模块的小例子?

json和空数组

没有默认构造函数的杰克逊第 3 方类

使用杰克逊创建一个 json 对象

使用 Python 3 读取 JSON 文件

如何安装 json gem - 无法构建 gem 原生扩展(mac 10.10)

添加json文件注释