Hi I'm working on a project which involves API calls. I was able to perform the Post api successfully but I'm getting 400 error for the GET request. I have tried so many times but still i'm getting the same error. Any solutions?
My Code
String targetURL = 'https://graph.microsoft.com/v1.0/users/pradeep@avaMigration.onmicrosoft.com'
try {
URL restServiceURL = new URL(targetURL);
HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();
httpConnection.setRequestMethod("GET");
//httpConnection.setRequestProperty("Accept", "application/json");
httpConnection.setRequestProperty("Authorization", "Bearer " + "the token);
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("HTTP GET Request Failed with Error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(
(httpConnection.getInputStream())));
String output;
log.debug("Output: \n");
while ((output = responseBuffer.readLine()) != null) {
return output;
}
httpConnection.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This line is plain wrong:
httpConnection.setRequestProperty("Authorization", "Bearer " + "the token);
Do you have the token as the variable "token"? If so, then it should be something like:
httpConnection.setRequestProperty("Authorization", "Bearer " + token);
Hi @Pradeep A
From the code you have shared, I noticed an error, i.e.:-
httpConnection.setRequestProperty("Authorization", "Bearer " + "the token);
You have missed out on a closed quotation for your String parameter. It should be:-
httpConnection.setRequestProperty("Authorization", "Bearer " + "the token");
I hope this helps to solve your question. :)
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Did you catch the news at Team ‘25? With Loom, Confluence, Atlassian Intelligence, & even Jira 👀, you won’t have to worry about taking meeting notes again… unless you want to. Join us to explore the beta & discover a new way to boost meeting productivity.
Register today!Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.