API 422 response truncates data on the client side

358 Views Asked by At

I would like to return a 422 Response from my API which includes as much of the data model that I can. I've created a method in our BaseRepository where we pass in the data, and an optional message, to return as a 422.

protected IHttpActionResult Create422Response<TData>(
    TData data,
    string message = "Resource validation failed."
)
{
    var response = new ResponseObject<TData>(data)
    {
        Status = "Failure",
        Message = message
    };

    return Content((HttpStatusCode)422, response);
}

On the client side, I'm catching the response like so:

var response = await _orderApiClient.ShowOrderForApproval(id);

if (response.StatusCode == (HttpStatusCode)422)
{
    Logger.LogWarning(response.Content);
    var responseJson = JObject.Parse(response.Content);
    ProcessValidationErrorsFromResponse(response);
}

When I look at the value of response.Content, I see that the JSON is truncated.

If I pass the same data object through an OK response it works.

protected IHttpActionResult CreateOkResponse<TData>(TData data, string message = "Call was successful")
{
    var response = new ResponseObject<TData>(data)
    {
        Status = "Success",
        Message = message
    };
    return Ok(response);
}

Why would the 422 truncate the data? Could there be something else going on?

UPDATE: Here's what ShowOrderForApproval does:

    public async Task<IRestResponse> ShowOrderForApproval(int id)
    {
        var request = new RestRequest("/api/orders/{id}/approve/")
        {
            Method = Method.GET,
            RequestFormat = DataFormat.Json
        };
        request.AddUrlSegment("id", id.ToString());
        return await RsClient.SendRequestAsync(request, new HttpContextWrapper(HttpContext.Current));
    }

RsClient.SendRequestAsync is:

    public static Task<IRestResponse> SendRequestAsync(RestRequest request, HttpContextBase context)
    {
        if (_client == null)
        {
            _client = Connect();
        }

        //If null, this is coming from the CI API and we've stored the value in the originating request header
        if (context.Session == null)
        {
            var authHeaderValue = context.Request.Headers["Authorization"];
            request.AddHeader("Authorization", "Bearer " + authHeaderValue);
        }
        else
        {
            var sessionTokenName = ConfigurationManager.AppSettings["SessionTokenName"];
            request.AddHeader("Authorization", "Bearer " + context.Session[sessionTokenName]);
        }           

        return Task.FromResult(_client.Execute(request));
    }

Update 2: Okay, more updates. Using Postman I can get all the data with a 422. Our RsClient is using RestSharp.

0

There are 0 best solutions below