响应中的有效 JSON [英] Valid JSON in Response

查看:26
本文介绍了响应中的有效 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发客户端和服务器应用程序,但遇到了一个有趣的问题.我们正在创建一个宁静的 API 并通过 JSON 响应与客户端进行通信.执行 DELETE 操作时,我们将返回 200 OK 和空白响应.我们的客户得到 200 OK,但解析 JSON 失败.

I am working on a client and server application and came across an interesting problem. We are creating a restful API and communicating to the clients with JSON responses. When doing a DELETE, we are returning a 200 OK with a blank response. Our clients are getting the 200 OK, but failing on parsing the JSON.

我们完全理解失败的解析(没有什么可解析的),但我们有一个更大的问题.发送空白响应是有效的 JSON,还是我们应该返回 {} 或类似的东西?谢谢.

We completely understand the failed parse (there is nothing to parse), but we had a bigger question. Is sending a blank response valid JSON, or should we be returning {} or something to that effect? Thanks.

推荐答案

来自 HTTP/1.1 定义:

如果响应包含一个成功的响应应该是 200 (OK)描述状态的实体,如果操作尚未完成,则为 202(已接受)已颁布,或 204(无内容),如果该操作已颁布但响应不包含实体.

A successful response SHOULD be 200 (OK) if the response includes an entity describing the status, 202 (Accepted) if the action has not yet been enacted, or 204 (No Content) if the action has been enacted but the response does not include an entity.

因此,如果您的服务器立即删除该项目,则建议的响应为:

So if your server is deleting the item immediately, then the suggested responses would be:

  • 200 OK - 返回某种状态代码(如布尔值 true,或表示成功"的字符串)
  • 204 No Content - 不返回任何内容,让您的客户端注意此响应而不尝试解析 JSON,同时仍然知道请求已成功

这篇关于响应中的有效 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆