shodan-python icon indicating copy to clipboard operation
shodan-python copied to clipboard

Return response body for errors

Open praseodym opened this issue 2 years ago • 3 comments

When the Shodan HTTP API returns an error status code, such as 403 Forbidden, this library will only return a APIError object.

However, the HTTP response will usually also include data in the response body, such as {"error": "Requires membership or higher to access"}. This data is currently ignored and not available through the library.

It would be useful if this response body is also made available by the library, for example to show a more informative error message to the end user.

praseodym avatar Jul 11 '23 15:07 praseodym

You should be able to access the specific error message from the APIException.value property

achillean avatar Jul 15 '23 00:07 achillean

The contents of the APIException.value property are hardcoded in the library, so e.g. for status code 403 the error message will always be Access denied (403 Forbidden): https://github.com/achillean/shodan-python/blob/9ed74964c1447f6781afd254a087563cd5ebee20/shodan/client.py#L376-L379

For some status codes, the Shodan response body will contain more detailed information that is useful to have and return to the end user.

praseodym avatar Jul 17 '23 13:07 praseodym

I think that's because for some of those responses the body isn't JSON but you're right, we should use the JSON error message if available as that is typically more precise.

achillean avatar Jul 18 '23 20:07 achillean