我试图用养蜂场(apiblueprint)创建我的api但是当我将一个帖子数据发送到我的终端时,我总是得到201响应。但是端点需要一些参数,所以当我发送空帖时我应该得到一些错误信息。我怎么能这样做?
### Create a New Question [POST]
+ question `Some text message` (string, required)
+ Request (application/json)
{
"question": "Favourite programming language?",
"choices": [
"Swift",
"Python",
"Objective-C",
"Ruby"
]
}
+ Response 201 (application/json)
+ Headers
Location: /questions/2
+ Body
{
"question": "Faavourite programming language?",
"published_at": "2015-08-05T08:40:51.620Z",
"choices": [
{
"choice": "Swift",
"votes": 0
}, {
"choice": "Python",
"votes": 0
}, {
"choice": "Objective-C",
"votes": 10
}, {
"choice": "Ruby",
"votes": 0
}
]
}
答案 0 :(得分:1)
如果您要将POST请求发送到Apiary的模拟服务器,您将在API蓝图中找回为该端点指定的第一个响应。这意味着即使与您的数据结构不匹配,您的示例中也总会得到201.
然而,Apiary将检查您的请求并确保它与您的数据结构和模式匹配,让您知道它的无效位置。
您的请求在JSON中包含一个问题参数,Apiary会在发出请求时确保它在那里。但是,它会将此行视为您的操作的描述,而不是在检查时使用它:
+ question `Some text message` (string, required)
定义数据结构的推荐方法是使用MSON,如下所示(注意我没有将响应转换为MSON,只是请求):
### Create a New Question [POST]
+ Request (application/json)
+ Attributes
+ question (string, required)
+ choices (array, optional)
- Swift
- Python
- Objective-C
- Ruby
+ Response 201 (application/json)
+ Headers
Location: /questions/2
+ Body
{
"question": "Favourite programming language?",
"published_at": "2015-08-05T08:40:51.620Z",
"choices": [
{
"choice": "Swift",
"votes": 0
}, {
"choice": "Python",
"votes": 0
}, {
"choice": "Objective-C",
"votes": 0
}, {
"choice": "Ruby",
"votes": 0
}
]
}
如果您使用此示例,请在忽略question
属性的同时向模拟服务器发出请求,然后查看" Inspector"选项卡,您会看到question
属性被遗漏的说明。
虽然MSON是我们推荐的定义数据结构的方法,但您也可以使用JSON Schema来定义语义。