Bad Request 400 when making API call to Microsoft Graph
Asked Answered
K

3

6

The error I'm getting from Microsoft Graph API is this

error = {
  "code": "BadRequest",
  "innerError": {
    "date": "2017-04-10T19:37:08",
    "request-id": "973641dd-b150-4406-9f3b-fbcf6f7e5aa1"
  },
  "message": "The MIME type 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2' requires a '/' character between type and subtype, such as 'text/plain'."
}

I'm able to successfully get from the endpoint https://graph.microsoft.com/v1.0/me/contacts while using POSTman however going the the scribe-java library OAuthRequest I receive this error.
My request is going to the url https://graph.microsoft.com/v1.0/me/contacts with 2 headers. Authorization -> Bearer [Token]
Content-Type -> application/json

Kiwi answered 10/4, 2017 at 20:16 Comment(6)
Match the request headers you send with postman and your application. There are obviously differences.Somato
Hi Mark, Currently my POSTman and my request using the java library match. i.imgur.com/Pab6ejb.png this is what my post man request looks like.Kiwi
Hi John, right click in your browser, and inspect the raw wire transfer. The server may also be expecting a User-Agent or something else.Somato
I don't see this in the request that postman makes. The only extra header that is included in postman is a "Postman-Token" -> "", but this should not make a different in making an API call to microsoft graph.Kiwi
Maybe replace * with */* in your request's MIME type.Sumba
Could you post the code where you add the headers to your application request? I'm wondering if they're getting escaped or something.Rivalee
K
0

It turns out the scribe-java library doesn't setup the request correctly. Switching to apache HttpClient and HttpGet resolved the issue for me.

Kiwi answered 26/4, 2017 at 20:59 Comment(1)
It's actually OpenJDK's HttpURLConnection class that sets up this invalid accept string. See bugs.openjdk.java.net/browse/JDK-8163921.Kasten
C
15

Try setting the "Accept" header explicitly in your request. POSTman may be defaulting you to [Accept=text/html, image/gif, image/jpeg, *; q=.2, /; q=.2]

Cristalcristate answered 19/4, 2017 at 19:24 Comment(1)
Buddy you're genius, I spent past 3 hours with this and never thought of the Accept. Thank you.Forelady
C
5

Hi I was getting same error when I tried to create a new user on AzureAD with graph api. I solved my issue that I changed my accept header to "Accept":"text/html, application/json". For your information

Carbolated answered 5/5, 2020 at 10:23 Comment(0)
K
0

It turns out the scribe-java library doesn't setup the request correctly. Switching to apache HttpClient and HttpGet resolved the issue for me.

Kiwi answered 26/4, 2017 at 20:59 Comment(1)
It's actually OpenJDK's HttpURLConnection class that sets up this invalid accept string. See bugs.openjdk.java.net/browse/JDK-8163921.Kasten

© 2022 - 2024 — McMap. All rights reserved.