API 422 response truncates data on the client side
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.
c# asp.net-web-api .net-4.7.1
add a comment |
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.
c# asp.net-web-api .net-4.7.1
Can you use theCreate422Response<TData>
to returnContent((HttpStatusCode)200, response)
and see if that also truncates the data?
– JLe
Nov 23 '18 at 15:31
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
1
What does_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?
– Stijn
Nov 23 '18 at 16:00
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlyingHttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.
– Stijn
Nov 23 '18 at 19:54
add a comment |
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.
c# asp.net-web-api .net-4.7.1
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.
c# asp.net-web-api .net-4.7.1
c# asp.net-web-api .net-4.7.1
edited Nov 23 '18 at 19:48
Stijn
16k978126
16k978126
asked Nov 23 '18 at 15:24
M Kenyon II
1,59332156
1,59332156
Can you use theCreate422Response<TData>
to returnContent((HttpStatusCode)200, response)
and see if that also truncates the data?
– JLe
Nov 23 '18 at 15:31
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
1
What does_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?
– Stijn
Nov 23 '18 at 16:00
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlyingHttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.
– Stijn
Nov 23 '18 at 19:54
add a comment |
Can you use theCreate422Response<TData>
to returnContent((HttpStatusCode)200, response)
and see if that also truncates the data?
– JLe
Nov 23 '18 at 15:31
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
1
What does_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?
– Stijn
Nov 23 '18 at 16:00
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlyingHttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.
– Stijn
Nov 23 '18 at 19:54
Can you use the
Create422Response<TData>
to return Content((HttpStatusCode)200, response)
and see if that also truncates the data?– JLe
Nov 23 '18 at 15:31
Can you use the
Create422Response<TData>
to return Content((HttpStatusCode)200, response)
and see if that also truncates the data?– JLe
Nov 23 '18 at 15:31
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
1
1
What does
_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?– Stijn
Nov 23 '18 at 16:00
What does
_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?– Stijn
Nov 23 '18 at 16:00
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlying
HttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.– Stijn
Nov 23 '18 at 19:54
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlying
HttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.– Stijn
Nov 23 '18 at 19:54
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449278%2fapi-422-response-truncates-data-on-the-client-side%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53449278%2fapi-422-response-truncates-data-on-the-client-side%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you use the
Create422Response<TData>
to returnContent((HttpStatusCode)200, response)
and see if that also truncates the data?– JLe
Nov 23 '18 at 15:31
You could try this way to return Json with an UnprocessableEntity result code which I have used successfully with error messages, but not with long text.
– stuartd
Nov 23 '18 at 15:32
Interesting, changing to 200, the data is not truncated.
– M Kenyon II
Nov 23 '18 at 15:55
1
What does
_orderApiClient
look like, is it your own code or is it generated code? Have you tried making a request to the API with a program like Fiddler, to see if the content is also truncated there?– Stijn
Nov 23 '18 at 16:00
Is the response by any chance cut off at 64kb? If so, your question is a duplicate of this one. Summary: the underlying
HttpWebRequest.DefaultMaximumErrorResponseLength
defaults to 64kb.– Stijn
Nov 23 '18 at 19:54