Android Question Web Service response

luke2012

Well-Known Member
Licensed User
B4X:
    Dim j1 As HttpJob : j1.Initialize("j1", Me)
    j1.Username = "user"
    j1.Password = "psw"
    j1.PostString("http://hostname.domain.it/apiname", "var1=test1&var2=test2")
    Wait For (j1) JobDone(j1 As HttpJob)
    If j1.Success Then
        Log(j1.GetString2("UTF8"))
    end if

Hi All,
with the above code I can get the text messages returned by the web service (program) like "error: VAT is invalid..." but I can't see the HTTP status code like "200 OK".

When there are no errors and the web service run successfully the "j1.GetString2("UTF8")" return an empty string.
I need to understand hot to intercept the "200 OK" (Standard response for successful HTTP requests) status code...

Thanks in advance for your response.
 

OliverA

Expert
Licensed User
I need to understand hot to intercept the "200 OK" (Standard response for successful HTTP requests) status code...

I'm pretty sure if j1.Success is true, then that means 200 OK. That status code is embedded in the header of the return, not the body (therefore status 200 can be set, but the body, and therefore GetString2, can be empty). Any non 2xx code will trigger j1.Success to be false (?).
 
Upvote 0

OliverA

Expert
Licensed User
Your parameters are good for GET-calls. You are using a POST call
He's using PostString. He's embedding the variables in the body of the post. This way they do not show up in any logging (and the other side probably expects them in the body instead of the URL). One should remember that PostString does not encode the data (unlike PostBytes/PostFile). This is fine as long as the receiving end is not expecting an encoded string.
 
Upvote 0

OliverA

Expert
Licensed User
So if success = true (status code = 2xx) then I'm sure that the web service run successfully
Most likely yes. Please note that it's up to the service what it uses to indicate a successful run. A service could return a 2xx code, but have as a response body with an the error message. It's also up to the service to have extra information in the body or not when throwing non 2xx codes.

Ninja'd by @Erel
 
Upvote 0

luke2012

Well-Known Member
Licensed User
The Web Service developer has implement for me a JSON response (j1.GetString2("UTF8")) with the StatusCode (ex. 200, 500, etc) and the description to display to the user.

W3.org says:

10.2 Successful 2xx
This class of status code indicates that the client's request was successfully received, understood, and accepted.

10.2.1 200 OK
The request has succeeded. The information returned with the response is dependent on the method used in the request, for example:

GET an entity corresponding to the requested resource is sent in the response;

HEAD the entity-header fields corresponding to the requested resource are sent in the response without any message-body;

POST an entity describing or containing the result of the action;

TRACE an entity containing the request message as received by the end server.
 
Upvote 0
Top