Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing the right HTTP response code for incorrect POST data

I suspect this is a very trivial question. I'm writing a PHP script to respond to an AJAX query. The query should include some XML data, which the PHP script processes and then returns a response to. There are two error cases I want to consider:

  1. No POST data in the request; or
  2. Bad data in the XML (either not valid or well-formed XML, or fails some schema checks)

In such cases I believe I should be returning a 4xx response code. Is there anything more appropriate than 400?

More Details

To illustrate the problem further: The client Javascript application is a diagram editor for educational purposes. The user is required to create a diagram that correctly models a given situation. The student can then submit the diagram, whereby an XML serialization of the diagram is POSTed via an AJAX call to the server. A PHP script analyses the diagram XML and constructs an XML report that is sent as the AJAX response to the client. The two situations I originally described (no XML POST data or invalid XML therein) should not happen when requested by the client, but I think it prudent to correctly capture and deal with these situations. Hence my belief that a 4xx response code is appropriate. The XML report structure doesn't cater for these situations, and an empty report would amount to a perfect diagram, which clearly is not appropriate,

like image 383
beldaz Avatar asked Feb 20 '12 03:02

beldaz


People also ask

What is HTTP response code for POST request with incorrect parameters?

HTTP status codes the server can generate in response to HTTP requests: 200 OK : Successful request. 400 Bad Request : Invalid argument (invalid request payload). 403 Forbidden : Permission denied (e.g. invalid API key).

Should POST return 200 or 201?

The 200 status code is by far the most common returned. It means, simply, that the request was received and understood and is being processed. A 201 status code indicates that a request was successful and as a result, a resource has been created (for example a new page).

What is the best HTTP response code for successful POST request?

2xx Status Codes [Success] Indicates that the request has succeeded. Indicates that the request has succeeded and a new resource has been created as a result. Indicates that the request has been received but not completed yet. It is typically used in log running requests and batch processing.

What is the HTTP code 402 indicates?

The HTTP 402 Payment Required is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.


1 Answers

Based upon the meanings of the codes in the TCP/IP Guide it seems like 400 is your best choice. Nothing there seems to meet your example.

like image 100
GPollice Avatar answered Nov 15 '22 06:11

GPollice