简体   繁体   English

REST API传递错误消息的索引参数

[英]REST API to pass indexed parameters for error messages

I'm designing a client and server system exchanging information using REST API. 我正在设计使用REST API交换信息的客户端和服务器系统。 For i18n, the client to localized while server is always in English. 对于i18n,客户端本地化,而服务器始终使用英语。 In order to do that, only error code is sent from server to client and client will pick up the correct version of localized error message to display to the user. 为此,仅将错误代码从服务器发送到客户端,客户端将获取正确版本的本地化错误消息以显示给用户。

Some error messages require indexed parameters. 一些错误消息需要索引参数。 Eg error message is "Can't find {0} on server {1}". 例如,错误消息为“在服务器{1}上找不到{0}”。 I'm wondering what's the best practice to pass these indexed parameters (ie {0}, {1}) along with error code to the client so that the client can create the error message. 我想知道将这些索引参数(即{0},{1})和错误代码传递给客户端,以便客户端可以创建错误消息的最佳实践是什么。

Can't you simply pass the errorcode + parameters in a single JSON object (or xml)? 您不能简单地在单个JSON对象(或xml)中传递errorcode +参数吗?

{
   "errorCode": "123",
   "parameters": ["param0", "param1"]
}

Or am I missing something here? 还是我在这里想念什么?

Follow the suggestions from Nottingham & Wilde . 遵循Nottingham&Wilde的建议。

"This document defines a "problem detail" as a way to carry machine-readable details of errors in a HTTP response, to avoid the need to invent new error response formats for HTTP APIs." “本文档定义了“问题详细信息”,作为在HTTP响应中携带机器可读错误的详细信息的方式,以避免需要为HTTP API发明新的错误响应格式。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM