将具有JSON数据的授权curl -u发布请求转换为等效的RestTemplate [英] Translate authorized curl -u post request with JSON data to RestTemplate equivalent

查看:1982
本文介绍了将具有JSON数据的授权curl -u发布请求转换为等效的RestTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用github api来创建使用curl命令的存储库,如下所示,它工作正常。


$ b

  curl -i -u用户名:密码-d'{name:TestSystem,auto_init: true,private:true,gitignore_template:nanoc}'https://github.host.com/api/v3/orgs/Tester/repos 

现在我需要通过 HttpClient 来执行上面的url,并且使用 RestTemplate 在我的项目中。



之前我曾与 RestTemplate 执行简单的URL但不知道如何使用 RestTemplate -

将上述JSON数据发布到我的网址。  RestTemplate restTemplate = new RestTemplate(); 

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

//创建一个multimap来保存命名参数
MultiValueMap< String,String> parameters = new LinkedMultiValueMap< String,String>();
parameters.add(username,username);
parameters.add(password,password);

//为请求创建http实体
HttpEntity< MultiValueMap< String,String>> entity =
new HttpEntity< MultiValueMap< String,String>>(parameters,headers);

ResponseEntity< String> response = restTemplate.exchange(url,HttpMethod.POST,entity,String.class);

任何人都可以提供一个例子,我如何通过发布JSON来执行上述URL?

解决方案

我还没有时间测试代码,但我相信这应该可以做到。当我们使用 curl -u 时,要传递凭证,必须将其编码并与授权标头一起传递,如 http://curl.haxx.se/docs/manpage.html#--basic 。 json数据只是作为HttpEntity传递。

 字符串编码= Base64Encoder.encode(username:password); 
HttpHeaders headers = new HttpHeaders();
headers.set(Authorization,Basic+ encoding);
headers.setContentType(MediaType.APPLICATION_JSON); //可选

String data ={\name \:\TestSystem \,\auto_init\:true,\private\: true,\gitignore_template\:\nanoc\};
String url =https://github.host.com/api/v3/orgs/Tester/repos;

HttpEntity< String> entity = new HttpEntity< String>(data,headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity< String> response = restTemplate.exchange(url,HttpMethod.POST,entity,String.class);


I am using github api to create repositories using curl command as shown below and it works fine.

curl -i -u "username:password" -d '{ "name": "TestSystem", "auto_init": true, "private": true, "gitignore_template": "nanoc" }' https://github.host.com/api/v3/orgs/Tester/repos

Now I need to execute the same above url through HttpClient and I am using RestTemplate in my project.

I have worked with RestTemplate before and I know how to execute simple url but not sure how to post the above JSON data to my url using RestTemplate -

RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Create a multimap to hold the named parameters
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
parameters.add("username", username);
parameters.add("password", password);

// Create the http entity for the request
HttpEntity<MultiValueMap<String, String>> entity =
            new HttpEntity<MultiValueMap<String, String>>(parameters, headers);

ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

Can anyone provide an example how would I execute the above URL by posting JSON to it?

解决方案

I have not had the time to test the code, but I believe this should do the trick. When we are using curl -u, to pass the credentials, it has to be encoded and passed along with the Authorization header, as noted here http://curl.haxx.se/docs/manpage.html#--basic. The json data, is simply passed as a HttpEntity.

String encoding = Base64Encoder.encode("username:password");
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic " + encoding);
headers.setContentType(MediaType.APPLICATION_JSON); // optional

String data = "{ \"name\": \"TestSystem\", \"auto_init\": true, \"private\": true, \"gitignore_template\": \"nanoc\" }";
String url = "https://github.host.com/api/v3/orgs/Tester/repos";

HttpEntity<String> entity = new HttpEntity<String>(data, headers);    
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, entity , String.class);

这篇关于将具有JSON数据的授权curl -u发布请求转换为等效的RestTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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