php没有后置的api,php-如果无法执行该操作,API应该返回什么?

我正在用PHP为移动应用程序编写API.端点之一create-user.php用于将新记录添加到User表.如果应用程序试图创建副本怎么办?我可以捕获此错误并返回错误消息(顺便说一下,这是?).问题是,我是否还应该返回具有以下结构的JSON:

{

"status": "The email already exists"

}

…向客户提供有关出了什么问题的更多信息?还是我应该只使用错误代码而已?

解决方法:

返回http状态422和错误消息

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

page statistics

具体来说:

422 status code feels much more appropriate. The server understands what you’re trying to do; and it understands the data that you’re submitting; it simply won’t let that data be processed.

此外,Laravel框架在“请求表单”类中还使用“失败规则”(例如,电子邮件已经存在)的http状态422.查看此:http://laravel.com/docs/master/validation

标签:mobile,api,backend,json,php

来源: https://codeday.me/bug/20191119/2039413.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值