Dingo API 表单验证错误 App 无法处理?

表单验证跑出的 422 状态码的响应会进到 ios 常用的网络请求组件的 error 里,且 ios 程序员跟我说 error 里拿不到 body 里返回的信息,有人有经验这样怎么处理吗?

他用的应该是 AFNetworking 这个 ios 网络库

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 5
liyu001989

照这个逻辑,那么 http 422 的错误信息应该放在哪?

如果要用 github 的 api,做个 app,岂不是完蛋了?

file

8年前 评论

@liyu001989 所为我在想如果 ios 不能处理,框架肯定会告诉大家应该怎么去写 api,但是他说他拿不到我返回的信息,最好有个 demo 说服他

8年前 评论

你们 ios 是个菜 不就是一个 json 字符串?

8年前 评论

@YuxiangDong 422 错误码的含义是请求的内容无法识别,但是在 AFNetworking 里面把这个当作错误响应了。看看这个问题:
https://github.com/AFNetworking/AFNetworki...

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.

所以两种方法咯,要不让你的同事重写一个 response serializer 来处理这个响应,要不你就返回一个 AFNetworking 可以获取 body 的响应了,如果要修改 laravel 的表单验证失败响应码,只有重写 response 方法,貌似没其他的设置什么的。。。

public function response(array $errors)
    {
        if ($this->ajax() || $this->wantsJson()) {
            return new JsonResponse($errors, 422);
        }

        return $this->redirector->to($this->getRedirectUrl())
                                        ->withInput($this->except($this->dontFlash))
                                        ->withErrors($errors, $this->errorBag);
    }
8年前 评论