donglvchu9143 2017-10-15 12:41
浏览 45
已采纳

在Laravel中的休息调用中返回错误消息

I have to receive a POST request from a client to my REST app in laravel 5. I'm follow the documentation related to validation, it said that when is an AJAX request laravel do not generate a redirect response, instead it will generate a JSON response with errors(https://laravel.com/docs/5.5/validation#a-note-on-optional-fields). But when I make the call from a REST client I'm getting this response:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url=http://laraveltest.com:8080/test/public" />

        <title>Redirecting to http://laraveltest.com:8080/test/public</title>
    </head>
    <body>
        Redirecting to <a href="http://laraveltest.com:8080/test/public">http://laraveltest.com:8080/test/public</a>.
    </body>
</html>

AdultoPost class *********************

class AdultoPost extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'Profesion' => "max:50",
            "EstadoCivil" => "required",
            "FamiliaReconstituida" => "required",
            "SolteroConHijo" => "required",
            "Viudo" => "required",
            "NoHijos" => "required"
        ];
    }
}

AdultoController **************

 public function post(AdultoPost $request, Adulto $adulto, Paciente $paciente, Persona $persona)
        {
            $persona = Persona::create($request->all());
            $adulto = Adulto::create($request->all());
            $paciente = Paciente::create($request->all());
return $adulto;
    }

If I change AdultoPost class for Request and put the validation rules inside the post function and catch a ValidationException I get this error response "The given data was invalid". Which is not what I want. I would like to send to the client which fields were invalid and why.

I'm making the request from REST API plugin of VSCODE, and from REST API TESTING extension from chrome, and now I'm installing POSTMAN to continue testing

POST http://laraveltest.com:8080/test/public/api/adulto
Content-Type: application/json
Accept: application/json

{
    "FechaInicio":"2017-09-10",
    "MotivoConsulta":"un motivo real",
    "ComoConocio":"como conocio"
}
  • 写回答

1条回答 默认 最新

  • dshm8998473 2017-10-15 12:52
    关注

    How do you make request from a client. Be sure to set correct headers, so it's a valid JSON request:

    Content-Type: application/json
    Accept: application/json
    

    Curl example:

    curl -H "Content-Type: application/json" -H "Accept: application/json"  -X POST -d '{"username":"xyz","password":"xyz"}' http://localhost/api
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误