Search code examples
androidretrofit2

Retrofit modifying server response received


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.


Solution

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