user47589
user47589

Reputation:

Restful API - What to return when no error occurred, but a resource wasn't created?

For reference, I am referencing http://www.restapitutorial.com/httpstatuscodes.html while working on this.

I have a Web API 2.2 service that allows a user to add "print requests" with AddPrintRequest(...), then batch those requests using a call to BatchRequests(...).

The BatchRequests method validates each request, and batches the valid ones. If there is at least one valid request, the batch is created and we return a HTTP 201 (Created). But what should we return if no batch is created? We don't consider this an error, but want to signal to the client why no batch was created. What is the correct status code?

Upvotes: 1

Views: 44

Answers (2)

abatishchev
abatishchev

Reputation: 100248

The status 204 No Content may be suitable here.

Upvotes: 4

William Xifaras
William Xifaras

Reputation: 5312

You can use Conflict and then throw an exception with a message.

Equivalent to HTTP status 409. Conflict indicates that the request could not be carried out because of a conflict on the server.

var message = "your message here";    
throw new HttpResponseException(
            Request.CreateErrorResponse(HttpStatusCode.Conflict, message));

https://msdn.microsoft.com/en-us/library/system.net.httpstatuscode(v=vs.110).aspx

Upvotes: 0

Related Questions