我必须通过使用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失效。
答案 0 :(得分:1)
创建HttpEntity
请求时,应将标头作为第二个参数1传递:
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);