使用restTemplate发送带有身份验证标头的GET请求 [英] Sending GET request with Authentication headers using restTemplate

查看:1018
本文介绍了使用restTemplate发送带有身份验证标头的GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过使用RestTemplate发送带有一些Authorization标头的GET请求来从我的服务器检索资源。

I need to retrieve a resources from my server by sending a GET request with the some Authorization headers using RestTemplate.

在浏览 docs 我注意到没有一个GET方法接受头作为参数,发送Headers(如accept和Authorization)的唯一方法是使用交换方法。

After going over the docs I noticed that none of the GET methods accept headers as a parameter, and the only way to send Headers such as accept and Authorization is by using the exchange method.

因为这是一个非常基本的动作,我想知道我是否遗漏了一些东西,还有另一种更简单的方法吗?

Since it is a very basic action I am wondering if I am missing something and there another, easier way to do it?

推荐答案

你没有遗漏任何东西。 RestTemplate#exchange(..)是用于设置请求标头的适当方法。

You're not missing anything. RestTemplate#exchange(..) is the appropriate method to use to set request headers.

这是一个例子(使用POST,但只需将其改为GET并使用实体你想)。

Here's an example (with POST, but just change that to GET and use the entity you want).

这是另一个例子。

请注意,使用GET,您的请求实体不必包含任何内容(除非您的API期望它,但那会违背HTTP规范)。它可以是一个空字符串。

Note that with a GET, your request entity doesn't have to contain anything (unless your API expects it, but that would go against the HTTP spec). It can be an empty String.

这篇关于使用restTemplate发送带有身份验证标头的GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆