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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料