Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It is possible to return complex object content in http CreateErrorResponse?

So we have a Web Api that for some 3rd party automation, the 3rd party will make a call to our API try Update some items.

Our Web Api will check if the items exists and some dependencies items (complex object in xaml/json) that need also be update before do the actual update process.

So the problem here is, if we found some dependencies that client haven't provided, I want to be able to response a Error Response message to the client with the exception and dependencies that need to be provided.

So the client can do a call to Api try update, if client got this error response with this error code and the content is these dependencies, the client can do automation with these dependencies.

Is it possible to do it?

Because I tried to use CreateErrorResponse, but it doesn't let me supply a content like CreateResponse(statusCode, complexObject)....

Or what is the best practice to work with this?

like image 266
King Chan Avatar asked May 20 '26 03:05

King Chan


1 Answers

You're not limited to use CreateErrorResponse. It's just there to ease the job done by using already provided HttpError class:

http://msdn.microsoft.com/en-us/library/system.web.http.httperror%28v=vs.108%29.aspx

This class can contain additional set of key/value pairs that can be sent in response body.

However, you can always define your own error class (i.e. ApiError) that will contain exact data that you need, and then return that class by calling, i.e.:

Request.CreateResponse(HttpStatusCode.BadRequest, error);

Where error is instance of your ApiError class.

like image 156
Admir Tuzović Avatar answered May 23 '26 10:05

Admir Tuzović