用户提交的无效数据的HTTP响应是什么?

时间:2023-01-28 07:35:45

I'm experimenting with JSON and http response codes. I'm submitting a form via an AJAX request and I obviously need to validate the data on the server-side.

我正在尝试使用JSON和http响应代码。我通过AJAX请求提交表单,显然我需要验证服务器端的数据。

My idea is to respond with a "200 OK" response (with a confirmation message as the body) if the post is successful. I don't know what to respond with if the data that the user sends is invalid.

如果帖子成功,我的想法是回复“200 OK”响应(以身份确认消息)。如果用户发送的数据无效,我不知道该怎么回应。

7 个解决方案

#1


7  

Just implement a standard protocol like JSON-RPC. It has error handling, parameter passing, etc.

只需实现像JSON-RPC这样的标准协议。它有错误处理,参数传递等。

Request:

请求:

{"method": "postMessage", "params": ["Hello all!"], "id": 99}

Response:

响应:

{"result": 1, "error": null, "id": 99}

And on error:

并且出错:

{"result": null, "error": "Duplicate Message", "id": 99}

It's quite flexible, and is standard...

它非常灵活,是标准的......

#2


10  

You could send a 400: Bad Request header. If that's not your cup of tea, maybe check through the W3C's Status Code Definitions?

您可以发送400:错误请求标头。如果那不是你的一杯茶,也许可以查看W3C的状态代码定义?

#3


7  

Send back a JSON object:

发回一个JSON对象:

$message = array(
   'error' => true,
   'code' => 'some error number relevant to you',
   'message' => 'A nice human-readable+relevant error message'
);

echo json_encode($message);

I prefer signaling errors with a service in this way. Fiddling with HTTP status codes doesn't seem right, as EVERYTHING about the actual HTTP request itself worked fine - it's just that the request didn't conform to the service's expectations.

我更喜欢这种方式的服务信号错误。摆弄HTTP状态代码似乎不对,因为关于实际HTTP请求本身的一切工作正常 - 只是请求不符合服务的期望。

#4


1  

Depends on the purpose of API. If it's yours (private) then answer with HTTP status 400 as Nightfirecat suggested. If it's a public API send a meaningful error message to aid developers.

取决于API的目的。如果它是你的(私人),那么回答HTTP状态400,如Nightfirecat所示。如果是公共API,则发送有意义的错误消息以帮助开发人员。

#5


0  

Here's the complete list of HTTP status codes. The first one that springs to mind for your situation is 400 Bad Request, but that's usually used to indicate an error in the HTTP syntax rather than an error in the body content. Still, without more information I'd go with that one.

这是HTTP状态代码的完整列表。第一个考虑到你的情况的是400 Bad Request,但这通常用于指示HTTP语法中的错误而不是正文内容中的错误。但是,如果没有更多信息,我会选择那个。

In specific cases, depending on the exact nature of the data you're receiving, I could see any of 403, 404, 410, 413, or perhaps others being the appropriate response.

在特定情况下,根据您收到的数据的确切性质,我可以看到403,404,410,413中的任何一个,或者其他可能是适当的响应。

#6


0  

Either 400 (generic), or if you want to be more specific: http://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422

要么400(通用),要么更具体:http://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422

#7


0  

The way I typically see this done is that you let the HTTP response codes be used for things related to the HTTP protocol, not for your application errors. Then, from your HTTP response, you return JSON that indicates success or failure and any desired returned data. For example, you could return this:

我通常看到这样做的方式是让HTTP响应代码用于与HTTP协议相关的事情,而不是用于应用程序错误。然后,从HTTP响应中,返回指示成功或失败的JSON以及任何所需的返回数据。例如,你可以返回这个:

{
    statusCode: 0,     // 0 = successful, negative value = error code
    statusMsg: "success",   // you can put any human readable status msg here
    data: {
        confirmationMsg: "Data sent successfully."
    }
}

Then, your code that looks at the response looks first at the statusCode and depending upon whether that is successful or not, it looks for other data.

然后,查看响应的代码首先查看statusCode,并根据是否成功,查找其他数据。

If you imagine how this response will be used by the caller, I find it valuable to have different code paths for application level errors vs. transport level errors. If you think about a jQuery.ajax() call. The success handler will be called when the http status code is a successful code. The error handler will be called when the http status code is not successful. Doing it this way, you can have generic error handlers for all transport level errors with one ajax error handler and then you can put your specific application-level error handling in the success handler by examining the application level status code in the returned JSON. If you go with the error 400, then you have to code up a whole different error handler for each ajax call to deal with both the transport errors and the application level errors.

如果您想象调用者将如何使用此响应,我发现应用程序级错误与传输级错误的代码路径不同是很有价值的。如果你考虑一个jQuery.ajax()调用。当http状态代码是成功的代码时,将调用成功处理程序。当http状态代码不成功时,将调用错误处理程序。这样做,您可以使用一个ajax错误处理程序为所有传输级错误提供通用错误处理程序,然后您可以通过检查返回的JSON中的应用程序级状态代码,将您的特定应用程序级错误处理放在成功处理程序中。如果你使用错误400,那么你必须为每个ajax调用编写一个完整的不同错误处理程序来处理传输错误和应用程序级错误。

#1


7  

Just implement a standard protocol like JSON-RPC. It has error handling, parameter passing, etc.

只需实现像JSON-RPC这样的标准协议。它有错误处理,参数传递等。

Request:

请求:

{"method": "postMessage", "params": ["Hello all!"], "id": 99}

Response:

响应:

{"result": 1, "error": null, "id": 99}

And on error:

并且出错:

{"result": null, "error": "Duplicate Message", "id": 99}

It's quite flexible, and is standard...

它非常灵活,是标准的......

#2


10  

You could send a 400: Bad Request header. If that's not your cup of tea, maybe check through the W3C's Status Code Definitions?

您可以发送400:错误请求标头。如果那不是你的一杯茶,也许可以查看W3C的状态代码定义?

#3


7  

Send back a JSON object:

发回一个JSON对象:

$message = array(
   'error' => true,
   'code' => 'some error number relevant to you',
   'message' => 'A nice human-readable+relevant error message'
);

echo json_encode($message);

I prefer signaling errors with a service in this way. Fiddling with HTTP status codes doesn't seem right, as EVERYTHING about the actual HTTP request itself worked fine - it's just that the request didn't conform to the service's expectations.

我更喜欢这种方式的服务信号错误。摆弄HTTP状态代码似乎不对,因为关于实际HTTP请求本身的一切工作正常 - 只是请求不符合服务的期望。

#4


1  

Depends on the purpose of API. If it's yours (private) then answer with HTTP status 400 as Nightfirecat suggested. If it's a public API send a meaningful error message to aid developers.

取决于API的目的。如果它是你的(私人),那么回答HTTP状态400,如Nightfirecat所示。如果是公共API,则发送有意义的错误消息以帮助开发人员。

#5


0  

Here's the complete list of HTTP status codes. The first one that springs to mind for your situation is 400 Bad Request, but that's usually used to indicate an error in the HTTP syntax rather than an error in the body content. Still, without more information I'd go with that one.

这是HTTP状态代码的完整列表。第一个考虑到你的情况的是400 Bad Request,但这通常用于指示HTTP语法中的错误而不是正文内容中的错误。但是,如果没有更多信息,我会选择那个。

In specific cases, depending on the exact nature of the data you're receiving, I could see any of 403, 404, 410, 413, or perhaps others being the appropriate response.

在特定情况下,根据您收到的数据的确切性质,我可以看到403,404,410,413中的任何一个,或者其他可能是适当的响应。

#6


0  

Either 400 (generic), or if you want to be more specific: http://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422

要么400(通用),要么更具体:http://greenbytes.de/tech/webdav/rfc4918.html#STATUS_422

#7


0  

The way I typically see this done is that you let the HTTP response codes be used for things related to the HTTP protocol, not for your application errors. Then, from your HTTP response, you return JSON that indicates success or failure and any desired returned data. For example, you could return this:

我通常看到这样做的方式是让HTTP响应代码用于与HTTP协议相关的事情,而不是用于应用程序错误。然后,从HTTP响应中,返回指示成功或失败的JSON以及任何所需的返回数据。例如,你可以返回这个:

{
    statusCode: 0,     // 0 = successful, negative value = error code
    statusMsg: "success",   // you can put any human readable status msg here
    data: {
        confirmationMsg: "Data sent successfully."
    }
}

Then, your code that looks at the response looks first at the statusCode and depending upon whether that is successful or not, it looks for other data.

然后,查看响应的代码首先查看statusCode,并根据是否成功,查找其他数据。

If you imagine how this response will be used by the caller, I find it valuable to have different code paths for application level errors vs. transport level errors. If you think about a jQuery.ajax() call. The success handler will be called when the http status code is a successful code. The error handler will be called when the http status code is not successful. Doing it this way, you can have generic error handlers for all transport level errors with one ajax error handler and then you can put your specific application-level error handling in the success handler by examining the application level status code in the returned JSON. If you go with the error 400, then you have to code up a whole different error handler for each ajax call to deal with both the transport errors and the application level errors.

如果您想象调用者将如何使用此响应,我发现应用程序级错误与传输级错误的代码路径不同是很有价值的。如果你考虑一个jQuery.ajax()调用。当http状态代码是成功的代码时,将调用成功处理程序。当http状态代码不成功时,将调用错误处理程序。这样做,您可以使用一个ajax错误处理程序为所有传输级错误提供通用错误处理程序,然后您可以通过检查返回的JSON中的应用程序级状态代码,将您的特定应用程序级错误处理放在成功处理程序中。如果你使用错误400,那么你必须为每个ajax调用编写一个完整的不同错误处理程序来处理传输错误和应用程序级错误。