If HTTP POST done with error Response is null

Jan 18, 2013 at 10:10 PM
Edited Jan 18, 2013 at 10:13 PM

Hello!

I`m using your library on WP7.1.

My app works with RESTful service. So, if any request error happens, service returns json with detailed error description. But I can`t get that json because HttpResponse.Response is null if request is not succesful.

For example, if app make request with wrong parameters, service returns header "bad request" and json with description what is exactly wrong.

The question is: is there any posibility to receive json in HttpResponse.Response (or everywere else) if request finished successfuly, but server returns header with error code (for example 400)?

Thanks!

Coordinator
Jan 20, 2013 at 6:37 PM

hi vbilenko

I just changed the http source code. The response should be available if there is an error.

Download the newest source code on codeplex or using subversion and please report me if it is working for you...

If everything is ok, i will release a new NuGet package (new version).

Regards

Rico

Jan 21, 2013 at 9:22 PM

Hi rsuter!

Thanks for the answer. 

The code has been downloaded and rebuilt, but I don`t see any changes. There is no body of responce in HttpResponse.Response on any error.

Also I found another issue: it not depends on server error, but HttpResponse.Exception contains - [System.Net.WebException] {"The remote server returned an error: NotFound."} and Status - "UnknownError". In the same time HttpResponse.HttpStatusCode is correct and contains System.Net.HttpStatusCode.BadRequest.

Also, I`m using custom error messages, something like, "HTTP/1.1 400 My custom bad request message" - this is very useful technique and often used in web services. It would be great to have possibility to read custom error messages.

I would be very thankful if you fix there problems :)

Coordinator
Jan 21, 2013 at 9:29 PM

Do you have a sample http url I can test the class with?

Jan 21, 2013 at 9:36 PM

Of course.

Not logged in:

GET http://watcher.mobi/api/login

Bad request:

POST http://watcher.mobi/api/login?login=&password=

Not found:

POST http://watcher.mobi/api/login?login=wrong_login&password=wrong_password

 

I`m using Fiddler Web Debugger to test requests.

 

Coordinator
Jan 21, 2013 at 10:40 PM

I updated the code.

Usage (WinRT/WP8, async):

try
{
    var result = await Http.PostAsync("http://watcher.mobi/api/login?login=&password=");
    Debug.WriteLine(result.Response);
}
catch (HttpStatusException e)
{
    // your status != 200 handler
    Debug.WriteLine(e.Result.HttpStatusCode);
    Debug.WriteLine(e.Result.Response);
}
catch (OperationCanceledException e)
{
    // TODO add your cancellation logic
}
catch (Exception e)
{
    // TODO add your exception handling logic
}

Usage (WP7, callback):

Http.Post("http://watcher.mobi/api/login?login=&password=", result =>
{
    if (result.Successful)
    {
        Debug.WriteLine(result.Response);
    }
    else if (!result.Canceled)
    {
        if (result.Exception is HttpStatusException)
        {
            var e = (HttpStatusException)result.Exception;

            // your status != 200 handler
            Debug.WriteLine(e.Result.HttpStatusCode);
            Debug.WriteLine(e.Result.Response);
        }
        else
            Debug.WriteLine(result.Exception.Message); // url not found, etc...
    }
});

Jan 22, 2013 at 4:01 AM

Great!

It works now, I can get the body of response.

Thank you very much.

Coordinator
Feb 5, 2013 at 12:22 AM
The feature is now available in the newest NuGet version!