I'm trying to access the FlightRadar24 API using Java and Spring's RestTemplate, but I'm receiving a 403 Forbidden error from Cloudflare.
What I'm trying to achieve:
According to the FlightRadar24 API documentation, I want to retrieve flight data by:
- Using the base URL https://fr24api.flightradar24.com/api.
- Sending my API token in the Authorization header as a Bearer token.
- Setting the API version in the Accept-Version header to v1.
- Making a GET request to the appropriate endpoint.
My Code:
package ch.ais.www.services;
import java.util.Collections;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
public class Fr24Service {
private final RestTemplate restTemplate = new RestTemplate();
private final String apiToken = "your_api_token_here";
private final String baseUrl = "https://fr24api.flightradar24.com/api";
public String getFlightData(String flightId) {
String url = String.format("%s/flights/%s", baseUrl, flightId);
// Set headers
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(apiToken); // Set Authorization header
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.add("Accept-Version", "v1");
headers.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
// Create request entity
HttpEntity<String> entity = new HttpEntity<>(headers);
// Send GET request
ResponseEntity<String> response;
try {
response = restTemplate.exchange(
url, HttpMethod.GET, entity, String.class);
} catch (HttpClientErrorException e) {
// Handle HTTP errors
System.err.println("HTTP Status Code: " + e.getStatusCode());
System.err.println("Response Body: " + e.getResponseBodyAsString());
throw e;
}
if (response.getStatusCode() == HttpStatus.OK) {
return response.getBody();
} else {
// Error handling
throw new RuntimeException("Error fetching flight data: " + response.getStatusCode());
}
}
}
The Error I'm Receiving:
When I run my application, I get the following exception:
Exception in thread "main" org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: "<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js ie6 oldie" lang="en-US"> <![endif]-->
<!--[if IE 7]> <html class="no-js ie7 oldie" lang="en-US"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en-US"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en-US"> <!--<![endif]-->
<head>
<title>Attention Required! | Cloudflare</title>
...
It appears that Cloudflare is blocking my request, and instead of the expected JSON response, I receive an HTML page.
Additional Information:
I'm using Java 21 and Spring's RestTemplate. It seems that Cloudflare is detecting and blocking my request as automated traffic. The API documentation specifies that the API token should be sent in the Authorization header and that the API version should be specified in the Accept-Version header. My Question:
How can I successfully send a request to the FlightRadar24 API from my Java application without being blocked by Cloudflare? Is there a way to configure RestTemplate (or another HTTP client) to access the API despite Cloudflare's protection mechanisms, given that I have legitimate API credentials?
Any help or suggestions would be greatly appreciated.
Note:
- I've omitted my actual API token from the code for security reasons.
- I understand that Cloudflare can block automated requests, but since I have valid API access, there must be a way to properly configure my client.
What I've Tried So Far:
- Verified that my apiToken is correct.
- Ensured that I'm setting the Authorization header with the Bearer token.
- Added the Accept-Version header with the value v1.
- Included a User-Agent header to simulate a common browser.
- Tested the API request using tools like Postman and curl—it works there.
- Confirmed that the endpoint URL is correct.
- Attempted to use WebClient from Spring WebFlux, but I get the same error.
- Enabled logging to inspect the HTTP requests and responses.