在curl中,如何强制将标头内容类型强制为application / json?

时间:2018-07-21 08:48:37

标签: curl

您好,我正在尝试遵循此页面上的教程:

https://www.elastic.co/guide/en/elasticsearch/reference/current/_index_and_query_a_document.html

在这一部分中,我们将添加一个客户。但是,当我按照说明进行操作(将其复制为curl并粘贴到cmd中)时,出现错误406。

复制的curl指令完全按照以下步骤粘贴到cmd中:

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'

以下情况导致每行被单独处理(不确定正确的术语): screen-cap of cmd showing each line being processed. ugh.

哪个会出现错误源?

如果我将所有内容复制到一行中,它看起来像这样:

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H'Content-Type:json' -d '{  "name": "John Doe"}'

重做不是很重,但是仍然给我一个错误:

{
  "error" : "Content-Type header [application/x-www-form-urlencoded] is not supported",
  "status" : 406
}
curl: (6) Could not resolve host: name
curl: (3) [globbing] unmatched close brace/bracket in column 9

这很奇怪,因为我没有plinkin将内容类型设置为该类型,很明显我将其设置为json。(对不起,我在这个周末浪费了一半的时间,厌倦了。)

谁能告诉我我做错了什么?还是开始寻找的好地方?我知道有很多配置可能是错误的,但是到目前为止一切都已经完成了;前一页上的PUT和GET请求有效,所以我不知道为什么突然之间不起作用。

1 个答案:

答案 0 :(得分:0)

这里有几个问题,

Collection<Child> filteredChildren = 
    parents.stream()
           .flatMap(p -> p.getChildren()
                          .stream()
                          .filter(c -> c.getType().equals("A")))
           .collect(Collectors.toList());

您显然正在使用的Microsoft终端仿真器cmd不支持带有curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d' { "name": "John Doe" } ' 的多行方案。等效的cmd命令为:

'

但是,我强烈建议您安装一个更好的终端,例如Cygwin Terminal,它支持curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d' ^ { ^ "name": "John Doe" ^ } ^ ' (和'的linux终端换行方案,而不是为此使用cmd,顺便说一句。)-很明显,您正在阅读的api文档已经假设您正在使用linux风格的终端,因此,您应该将linux格式的命令转换为Windows cmd格式(显然您不符合资格),而不是而是安装一个Linux风格的终端(例如Cygwin。)

另一个问题:当API标头为\时,它将以Content-Type:json响应

  • 这是目标API中的错误,您应该将错误报告发送给API开发人员,它应该已响应:Content-Type header [application/x-www-form-urlencoded] is not supported

另一个问题:Content-Type header [Content-Type:json] is not supported

  • 您复制错了,最后以If I copy it all into one line, it looks like this:结尾,应该是-H'Content-Type:json'
相关问题