为什么 RAML 响应中需要多个状态码?

Why multiple status codes are required in RAML response?

在定义 RAML 时,在响应主体中,我们将根据状态代码(如 200、400 等)定义响应。 为什么需要单独定义?

 sample RAML
responses: 
        200:
            body:
                type: User
        400:
            body:
                type: ErrorMessage

我可以从控制器发送赞

  response = Request.CreateResponse(HttpStatusCode.Ambiguous, error);

这会将响应创建为 300 状态。

在从 Postman 中点击 api 时,我可以获得状态为 300 的响应。

所以真的需要定义不同状态码的响应吗?

我认为你误解了 RAML 是什么。

RAML 正在记录您的 API,它不会实现您的 API。所以是的,你可以有一个不同于你的 RAML 指定的实现。

Is it really needed to define the response for different status codes?

不,在 RAML 中整个 responses is optional。所以你可以忽略它。

但我们的想法是,您指定的 RAML 尽可能接近您的实现,因此它对您和其他人都有用。

While defining RAML, in response body we will define response based on status codes like 200, 400, etc. Why that seperate defination is required?

不同的状态码通常意味着不同的响应数据。

例如,如果您通过 id (/dogs/1) 得到一只狗,那么如果找到该狗,您将 return 状态码 200 正文将包含有关狗的数据。例如{ name: "foo", colour: "brown", ...}

另一方面,如果没有 id 为 1 的狗,那么您将 return 状态代码 404 并且在正文中是这样的:{ message: "a dog with id 1 was not found" }

Is it really needed to define the response for different status codes?

RAML 是一种生成文档和客户端的方法。当然,您可以记录您要 return 一组 X,实际上 return 一组 Y,但这只会惹恼消费者。

因此您可以return对所有响应代码使用相同的响应类型,或者记录响应类型因响应代码而异的时间。