B4J Question HTTPJob; Get body when there is an error.

keirS

Well-Known Member
Licensed User
Longtime User
Can't seem to work out how to do this. For a REST server I am connecting to the error information is returned in the body as JSON. Neither ErrorMessage or GetString return the body.
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
Try ErrorMessage (the assumption here is that the job's Success flag is False). That works for me when I return 500 codes in my modified jRDC2 code.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Try ErrorMessage (the assumption here is that the job's Success flag is False). That works for me when I return 500 codes in my modified jRDC2 code.

Sorry meant to say ErrorMessage and GetString not . ErrorMessage does not return the JSON string which is in the body of the message.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
So on a Success = False, ErrorMessage is blank/has no content?
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
It has content; for example ErrorMessage returns "Internal Server Error". Doing the same REST API call using Postman I can see this in the Body:

B4X:
{"code":"INTERNAL_SERVER_ERROR","message":"An internal server error occurred"}

I need the full JSON not just the code.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
If you have logging on, the OkHTTPUtils, on an error (a non 200 return code from the WebServer) will perform
B4X:
Log($"ResponseError. Reason: ${Reason}, Response: ${Response.ErrorResponse}"$)

On my jRDC2 server, I have the following error code when calling a sql query that is not set up
B4X:
errMsg = $"jRDC ERROR: Command not found: ${cmd.Name}"$
resp.SendError(500, errMsg)

This will log the following (via the above Log() call):
ResponseError. Reason: jRDC ERROR: Command not found: drop_students2, Response: <html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 500 jRDC ERROR: Command not found: drop_students2</title>
</head>
<body><h2>HTTP ERROR 500</h2>
<p>Problem accessing /rdc. Reason:
<pre> jRDC ERROR: Command not found: drop_students2</pre></p><hr><a href="http://eclipse.org/jetty">Powered by Jetty:// 9.4.z-SNAPSHOT</a><hr/>
</body>
</html>

If you notice, my error message is shown plainly as ${Reason}, but it is also embedded in ${Response.ErrorResponse}. Do you get something similar? Please note, this is only if the server returns a non-200 code and therefore Success = False.
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
I
If you notice, my error message is shown plainly as ${Reason}, but it is also embedded in ${Response.ErrorResponse}. Do you get something similar? Please note, this is only if the server returns a non-200 code and therefore Success = False.

Yes the log shows the same as Postman:
B4X:
{"code":"INTERNAL_SERVER_ERROR","message":"An internal server error occurred"}

It's a 500 error.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Can you post the exact log posting?
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Can you post the exact log posting?

Hmm. I think I have sorted this. I had both OkHTTP and JHTTP ticked in the libraries and JHTTPUtils2 and jOkHTTPUtils ticked! I am converting an old project to OkHTTP and implementing Wait For. Thanks for the help.
 
Upvote 0
Top