是否有最大HTTP状态代码消息长度? [英] Is there a maximum HTTP status code message length?

查看:95
本文介绍了是否有最大HTTP状态代码消息长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,我正在构建前端,而其他人正在构建API。我为所有请求提出了以下结构,以JSON格式发送:

I'm working on a project where I'm building the frontend and someone else is building an API. I was proposing the following structure for all requests, sent as JSON:

{
  "success": true, // true/false
  "message": null, // a string if success==false indicating the error
  "data": {} // The actual data in the response
}

他们更感兴趣的是让API更加RESTful,而不是他们提出的消息字段在HTTP标头中的状态代码消息中发回消息,例如:

They are more interested in making the API more RESTful, and instead of a "message" field they are proposing sending a message back in the status code message, in the HTTP headers, such as:

HTTP/1.1 401 Authentication Failed for john.smith@example.com. Please log in again.

并且前端会显示john.smith@example.com的身份验证失败。请再次登录。在弹出窗口或其他东西。

and the frontend would display "Authentication Failed for john.smith@example.com. Please log in again." in a popup or something.

我担心长度限制,但我找不到任何表示没有最大长度的东西。我们应该确保将这些消息保持在最小长度吗?有没有充分的理由不这样做,而是将其作为内容(JSON或纯文本)发回?

I'm worried about length restrictions, but I couldn't find anything indicating no maximum length. Should we ensure we keep those messages to a minimum length? Is there a good reason to not do this, and instead send it back as content (JSON or plain text)?

推荐答案

A很少进行测试会有很长的路要走,但你应该可以做到这一点,事实上 RFC 具体说明:

A little testing will go a long way, but you should be okay to do this and in fact the RFC says specifically:


此处列出的原因短语只是建议 - 它们可能被替换为本地等价物而不影响协议。

The reason phrases listed here are only recommendations -- they MAY be replaced by local equivalents without affecting the protocol.

唯一可能的问题是标题大小(某些服务器可能有限制,但我认为它们都相对较大)以及一些旧的浏览器可能会对此作出反应。坦率地说,我认为使用响应体更有意义,因为它更容易解释和清除,但是你的方法应该没有任何问题。

The only possible concern you may have is header size (some servers may have limitations, but I think they are all relatively large) and how some older browsers may react to this. Frankly I think it makes more sense to use the response body since it's easier to interpret and clear, but there shouldn't be anything wrong with your approach.

这篇关于是否有最大HTTP状态代码消息长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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