验证请求是否应该返回true或false,还是应该返回状态代码200为true和状态代码400为false?

时间:2019-04-25 08:09:10

标签: backend

我正在创建一个REST api,该api可以验证两因素身份验证代码。我使用了一个库进行验证,它返回true或false。

至于我的回答,应该是正确,推荐还是标准的方式?

A。。返回状态码200,并且响应数据为true或false。

B。返回状态码200是否为真,返回状态码400(或者应该为什么正确的错误代码)?

我们正在使用JavaScript进行开发,如果我使用 A 实现,它将类似于以下内容:

// Not using async await
http.post(url).then(isValid => {
  // If Else
});

// Using async await
const isValid = await http.post(url);
  // If Else

使用 B 实现时:

// Not using async await
http.post(url).then(valid => {
  // We expect valid to be always true
}).catch(err => {
  // Check if error refers to invalid two fa code or other errors
});

// Using async await
try {
  // We expect valid to be always true
  const valid = await http.post(url);
} catch (err) {
  // Check if error refers to invalid two fa code or other errors
}

如您所见,在使用 B 实现时,我将需要使用catch并检查该错误是否涉及无效的两个fa代码或其他错误。与 A 实现不同,它是对还是错。

0 个答案:

没有答案
相关问题