Untitled
unknown
plain_text
2 years ago
1.6 kB
6
Indexable
Got it! In a non-Spring Boot project using NetBeans, you can still use `HttpURLConnection` to make API calls with a Bearer token. Here's a basic example:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class ApiCaller {
public static void main(String[] args) {
try {
// Replace these with your actual API endpoint and token
String apiUrl = "https://api.example.com/endpoint";
String bearerToken = "yourBearerToken";
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Set the request method
connection.setRequestMethod("GET"); // Replace with your HTTP method
connection.setRequestProperty("Authorization", "Bearer " + bearerToken);
// Read the response
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// Access the response
System.out.println("API Response: " + response.toString());
// Close the connection
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
Make sure to handle exceptions and customize the code based on your specific needs.Editor is loading...
Leave a Comment