![](/img/trans.png)
[英]Spring RestTemplate HTTP Post with parameters cause 400 bad request error
[英]How to implement the HTTP POST Request using Spring RestTemplate
我必須通過使用resttemplate來執行此請求。 鍵是有效的,並且其他(GET)請求已成功隱含。 但是有了這個我有一個問題
response = Unirest.post("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0")
.header("X-RapidAPI-Host", "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com")
.header("X-RapidAPI-Key", "somekey")
.header("Content-Type", "application/x-www-form-urlencoded")
.field("country", "US")
.field("currency", "USD")
.field("locale", "en-US")
.field("originPlace", "MSQ-sky")
.field("destinationPlace", "DME-sky")
.field("outboundDate", "2019-05-01")
.field("adults", 1)
.asJson();
response.getHeaders();
我正在嘗試,但我總是得到401
HttpHeaders headers = new HttpHeaders();
headers.set("X-RapidAPI-Host", "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com");
headers.set("X-RapidAPI-Key", RAPID_API_KEY);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("country", "US");
map.add("currency", "USD");
map.add("locale", "en-US");
map.add("originPlace", "MSQ-sky");
map.add("destinationPlace", "DME-sky");
map.add("outboundDate", "2019-05-01");
map.add("adults", "1");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<ObjectNode> resp = restTemplate.exchange(url, HttpMethod.POST, request, ObjectNode.class);
錯誤在哪里?
更新此請求在春天5的春天5它的作品中不起作用。
創建HttpEntity
請求時,應將標頭作為第二個參數1傳遞:
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.