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 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题