休息json数据的API响应头?

时间:2017-02-24 15:49:27

标签: json rest http curl

这可能是重复但我无法以一种像我一样的方式来表达我的查询。感谢您回答或完善问题的任何帮助。

我在java / apache中实现了一个简单的REST api。当我使用 浏览器 中的GET请求调用端点时,我得到了我期望的响应:

从我的浏览器:

GET http://localhost:8080/app/pipeline?text=hello {"input":"hello"}

请求标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Host:localhost:8080
Pragma:no-cache
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

响应标题:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Access-Control-Allow-Headers:Origin, Accept, X-Requested-With, Content-Type, 
Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods:GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
Access-Control-Allow-Origin:*
Access-Control-Max-Age:3600
Connection:keep-alive
Content-Length:31
Content-Type:application/json
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36

来自Curl:

现在当我通过curl点击同一个端点时出现问题:

$ curl -i 'http://localhost:8080/app/pipeline?text=hi' 
HTTP/1.1 200 OK
Content-Length: 42
Accept: */*
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods:
GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
text: hi
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Content-Type: application/json
Connection: keep-alive

"eyJpbnB1dCI6ImhpIiwibWV0YWRhdGEiOnt9fQ=="

我上面得到了这个奇怪的编码响应。我正在学习Accept和Encoding类型标题,所以我设置了accept和content-encoding标头,就像我的chrome请求一样:

$ curl -i -sH 'Accept: application/json' -sH 'Content-Encoding: gzip, deflate, sdch, br' 'http://localhost:8080/app/pipeline?text=hi' 
HTTP/1.1 200 OK
Content-Length: 42
Accept: application/json
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type,     Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Content-Encoding: gzip, deflate, sdch, br
text: hi
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Content-Type: application/json
Connection: keep-alive

"eyJpbnB1dCI6ImhpIiwibWV0YWRhdGEiOnt9fQ=="

到底是什么?事实证明,除非我在chrome请求中确切地指定了Accept标头,否则我会得到这个奇怪的输出。使用该标头集,我得到了所需的响应:

$ curl -i -sH 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -sH 'Content-Encoding: gzip, deflate, sdch, br' 'http://localhost:8080/app/pipeline?text=hi' 
HTTP/1.1 200 OK
Content-Length: 28
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Access-Control-Allow-Headers: Origin, Accept, X-Requested-With, Content-Type,     Access-Control-Request-Method, Access-Control-Request-Headers
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 3600
Content-Encoding: gzip, deflate, sdch, br
text: hi
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
Content-Type: application/json
Connection: keep-alive

{"input":"hi","metadata":{}}

问题

我正在从我的服务返回application / json,那么为什么我的请求不起作用,除非我指定这样的标题:Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8?我假设*/*包含所有内容,但如果我提供标题中的*/*,或者只是提供应用程序/ JSON。

非常感谢任何正确理解方向的线索。

0 个答案:

没有答案