简体   繁体   中英

How to implement the HTTP POST Request using Spring RestTemplate

I have to do this request through using resttemplate. key is valid, and other (GET) request ive successfully implemmented. But with this i have a problem

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();

I've trying, but i always get 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);

Where is mistake?
UPDATED This request doesnt works in SPRING 5, in spring 4 its works.

创建HttpEntity请求时,应将标头作为第二个参数1传递:

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM