首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我如何告诉Akka HTTP上的spray“假设application/json"?

我如何告诉Akka HTTP上的spray“假设application/json"?
EN

Stack Overflow用户
提问于 2017-03-18 04:39:27
回答 1查看 207关注 0票数 0

spray-json服务器上使用Akka HTTP;当未提供请求时,我如何“假设请求为Content-type: application/json"?”

问题

代码语言:javascript
复制
curl localhost:12345/request -d'{"sample":"json"}'

不起作用除非我加上

代码语言:javascript
复制
-H "Content-type: application/json"

明文规定。

我为什么问

  • 不适合在README.md上显示琥珀示例
  • 调试时总是手动添加标头很烦人
EN

回答 1

Stack Overflow用户

发布于 2017-03-18 05:01:42

您应该将API定义为只接受内容类型json,这就是它不接受其他内容类型的原因。

只做curl -XPOST -d 'content' URL就会将内容作为application/x-www-form-urlencoded (默认行为)发布。

参见--冗长的卷发文章,参见下面示例中的Content-Type

代码语言:javascript
复制
$ curl -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
*   Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Length: 22
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:54:41 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 51
< Connection: keep-alive
< Set-Cookie: __cfduid=d91d0ebbaa5f7b40a998d327ffce2b5671489812881; expires=Sun, 18-Mar-18 04:54:41 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"33-cwlRczABGtsoiZUP8lw9WSIt684"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415986bb9f50c35-SEA
< 
{
  "{\"test\" : \"testValue\"}": "",
  "id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}

您必须更改客户端头以匹配API服务器接受的内容。

代码语言:javascript
复制
$ curl -H "Content-type: application/json" -XPOST -d '{"test" : "testValue"}' "https://jsonplaceholder.typicode.com/posts" -v
*   Trying 104.31.87.157...
* Connected to jsonplaceholder.typicode.com (104.31.87.157) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
* Server certificate: sni233425.cloudflaressl.com
* Server certificate: COMODO ECC Domain Validation Secure Server CA 2
* Server certificate: COMODO ECC Certification Authority
* Server certificate: AddTrust External CA Root
> POST /posts HTTP/1.1
> Host: jsonplaceholder.typicode.com
> User-Agent: curl/7.43.0
> Accept: */*
> Content-type: application/json
> Content-Length: 22
> 
* upload completely sent off: 22 out of 22 bytes
< HTTP/1.1 201 Created
< Date: Sat, 18 Mar 2017 04:53:22 GMT
< Content-Type: application/json; charset=utf-8
< Content-Length: 38
< Connection: keep-alive
< Set-Cookie: __cfduid=d8e90de27fc1fa87fb42b763a4199ba7d1489812801; expires=Sun, 18-Mar-18 04:53:21 GMT; path=/; domain=.typicode.com; HttpOnly
< X-Powered-By: Express
< Vary: Origin, X-HTTP-Method-Override, Accept-Encoding
< Access-Control-Allow-Credentials: true
< Cache-Control: no-cache
< Pragma: no-cache
< Expires: -1
< X-Content-Type-Options: nosniff
< Etag: W/"26-Zx6VPi+rfbM5YFlBzT2pzGEHcgg"
< Via: 1.1 vegur
< Server: cloudflare-nginx
< CF-RAY: 3415967b2af12a19-SEA
< 
{
  "test": "testValue",
  "id": 101
* Connection #0 to host jsonplaceholder.typicode.com left intact
}

您可以更改服务器上的内容类型,并自行将收到的任何内容更改为JSON (这可能不是一个好主意)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42870522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档