1

I am an experienced programmer but new to WebApi (We're using MS asp.net mvc framework).

A customer requested that all return values be wrapped in an object of this sort:

{
  success: [boolean],
  errorLog: [error message, if failed],
  referer: [api call details],
  payload: {
    // results, if success
}

I saw that the standard already called for returning an HttpResponseMessage object, which has this information. So, rather than duplicating, I just return an object of this type (either directly, or via an IHttpActionResult, e.g.

return Ok(object);

However, I cannot see how to use this object from the client side. When I make an api call from the browser, all I see is the content (even using the debugger). When I return an error, I see:

 <Error>
    <Message>foo bar</Message>
 </Error>

but no sign of the other info (StatusCode, ReasonPhrase, etc.). The same applies if I return Json format.

Is this object 'stripped' somewhere along the line, and if so by who? How can I allow this object to arrive to the api caller so s/he can use all the associated fields?

Thanks

Edit: As requested by comment, I'm posting my server-side code, though there isn't much to it. My attempt at seeing the HttpResponseMessage object was to create this controller:

public HttpResponseMessage Get() {
            HttpResponseMessage response = Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "foo bar");
            response.ReasonPhrase = "There is no reason.";
            return response;
        }

This is the code which results in the xml posted above.

Pete
  • 51
  • 1
  • 7

1 Answers1

0

Is this object 'stripped' somewhere along the line, and if so by who? The object will be stripped by HttpResponseMessage under the hood when you return IHttpActionResult a call to ExecuteAync to create an HttpResponseMessage. So in order to see all fields you should use a tools like fiddler in raw mode or extension like HttpHeader for chrome. How can I allow this object to arrive to the api caller so s/he can use all the associated fields
One way to send the expected answer is to create a POCO like this

 public class Payload
    {
    }

    public class CustomResponse
    {
        public IList<bool> Success { get; set; }
        public IList<string> ErrorLog { get; set; }
        public IList<string> Referer { get; set; }
        public Payload Payload { get; set; }
    }

and send the answer like this

 return OK(new CustomObject(){Payload=yourObject});
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47