0

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.

4
  • 1
    Can you format your code before asking for help ? Commented Apr 23, 2019 at 16:36
  • 1
    sorry, its first question this, I thought it would be formatted Commented Apr 23, 2019 at 16:38
  • Is your key (RAPID_API_KEY) valid? Commented Apr 23, 2019 at 16:45
  • sure, the key is valid and works with other requests Commented Apr 23, 2019 at 20:12

1 Answer 1

1

When you create a HttpEntity request, you should pass headers as a second argument 1:

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
Sign up to request clarification or add additional context in comments.

3 Comments

ive changed, and now im getting 403
The 403 Forbidden error is an HTTP status code which means that accessing the page or resource you were trying to reach is absolutely forbidden for some reason.
i understand what does mean 403. look at first example(Unirest) its works. how i can do that by resttemplate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.