简体   繁体   English

在Spring中添加Accept头

[英]Adding Accept header in Spring

I'm trying to consume a JSON object from http://www.foaas.com/ in my Spring RESTful app. 我正在尝试在我的Spring RESTful应用程序中使用来自http://www.foaas.com/的JSON对象。 However they require content negotiation with an Accept header. 但是,它们需要使用Accept标头进行内容协商。

I have tried this method found here on SO: 我已经尝试过在这里找到这种方法:

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");

HttpEntity entity = new HttpEntity(headers);

HttpEntity response = restTemplate.exchange(foaasURI, HttpMethod.GET, entity, String.class);

I'm getting 403 forbidden at every attempt. 每次尝试我都禁止403。

Using Postman for adding an Accept header works fine so I know everything is correct on foaas end. 使用Postman添加Accept标头可以很好地工作,因此我知道在foaas端一切都正确。 How do I proceed? 我该如何进行?

The problem is NOT with the "application/json" header. 问题不在于"application/json"标头。 Your current code is successfully adding the "application/json" header. 您当前的代码已成功添加"application/json"标头。

The server is rejecting your request because of some authentication issues. 由于某些身份验证问题,服务器拒绝了您的请求。

A 403 response generally indicates one of two conditions: 403响应通常指示以下两种情况之一:

Authentication was provided, but the authenticated user is not permitted to perform the requested operation. 提供了身份验证,但是不允许经过身份验证的用户执行请求的操作。

Please refer this link 请参考此链接

使用HttpHeaders#setAccept

headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));

It seems that you should specify a user agent header in the request for foass.com. 似乎您应该在foass.com的请求中指定用户代理标头。

headers.set("User-Agent", "eltabo");

RestTemplate default requestfactory (SimpleClientHttpRequestFactory) does not specify any user-agent for its requets. RestTemplate的默认requestfactory(SimpleClientHttpRequestFactory)未指定任何用户代理对其进行记录。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM