无法使用标头在Spring RestTemplate中重新创建cURL命令

时间:2016-09-28 20:42:35

标签: spring curl resttemplate

这是我试图重新创建的curl命令:

curl https://bannana-pajamas.com/services/data/v20.0/query/?q=BananaQuery -H 'Authorization: Bearer Banana_Token'

这个curl命令在我的终端中运行正常,并返回一个json文件。

我尝试在Spring Rest Template中重新创建它:

//Create a rest template
        RestTemplate rest = new RestTemplate();

        System.out.println("This here is the token "+Banana_Token);


        //Create the headers
        HttpHeaders headers = new HttpHeaders();
        headers.set("Authorization","Bearer "+Banana_Token);

        HttpEntity<String> entity = new HttpEntity<>(headers);

        //Create the url
        String url = "https://bannana-pajamas.com/services/data/v20.0/query/?q=BananaQuery";

        String response;

        try{
            response = rest.exchange(url, HttpMethod.GET, entity, String.class).toString();
        }catch(Exception error){
            System.out.println("This still ain't working bro. Imma make this null till you get stuff right");
            response = null;
        }


        return response;

当我运行它时,我收到以下错误:

org.springframework.web.client.HttpClientErrorException: 400 Bad Request

我已经在这个网站上尝试了几个类似的答案,但我一直在收到这个错误。我可能遗漏了一些非常小的东西。

有人有什么想法吗?

谢谢!

0 个答案:

没有答案