我正在使用github api使用curl命令創建存儲庫,如下所示,並且工作正常。將帶有JSON數據的授權curl -u請求轉換爲RestTemplate等效
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
現在我需要通過HttpClient
執行相同的上述網址,我在我的項目中使用RestTemplate
。
我與RestTemplate
工作之前,我知道如何執行簡單的URL,但不知道如何使用發佈上述JSON數據到我的網址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);
任何人都可以提供我將如何執行的例子通過發佈JSON到上面的URL?
這個URL沒有什麼複雜的。你什麼意思?你具體遇到什麼問題? – 2015-02-08 04:49:56
@SotiriosDelimanolis我不知道我會把所有其他的參數,除了網址。我只是對這個部分感到困惑。否則,我很清楚我需要做什麼。 URL將成爲curl命令的結尾部分。 – john 2015-02-08 04:54:03