0

I am using retrofit on newsApi. I am trying to hit endpoint

https://newsapi.org/v2/sources?apiKey=xxxxx

In case of wrong api key server is returning:

{"status":"error","code":"apiKeyInvalid","message":"Your API key is invalid or incorrect. Check your key, or go to https://newsapi.org to create a free API key."}

and in case of no api key server is returning:

{“status”:“error”,“code”:“apiKeyMissing”,“message”:“Your API key is missing. Append this to the URL with the apiKey param, or use the x-api-key HTTP header.”}

but in my application retrofit is returning following for both the cases:

Response{protocol=http/1.1, code=401, message=Unauthorized, url=https://newsapi.org/v2/sources}

just the url is changed for both cases but message and code is same.

Why is retrofit modifying server response and how can I get the original server message or response?

Thanks in advance.

  • 1
    Actually, it's not modifying the response, it's just showing the "header" (protocol, code, message (of the code), and url). But the response you are expecting is in the body. You need to get the json body from the response and you will see what you call the "original server message" – Héctor Jan 10 '18 at 10:26
  • I had no idea retrofit did that. Thank you very much. – Tarun Trikha Jan 10 '18 at 11:15

1 Answers1

0

Not having an api key makes you unauthorized, so Retrofit is not wrong to access the error body check out this previous answer

Demilade
  • 513
  • 2
  • 7