3

My webapp has Laravel as backend framework which provides a Restful API and in the fronend Angularjs is running. I send different requests through the api and receive the responses and based on the code of response and data included, appropriate messages are shown to user.

Recently when I send requests using PUT method or POST method, when the data has problem in validation process and Laravel should respond with a 422 code in JSON format, instead I receive a text/html response with code 200. and then everything goes wrong.

This does not happen on my local machine, Only when I test the app in production environment this happens.

I also tested UnAuthorized response which is sent with 403 code, and it works flawlessly.

I tested both the automatic validation error for Laravel (as described in documentation: When using the validate method during an AJAX request, Laravel will not generate a redirect response. Instead, Laravel generates a JSON response containing all of the validation errors. This JSON response will be sent with a 422 HTTP status code.) and also using the following method:

return response()->json(compact('errors'),422);

I should mention that I use following methods to send AJAX requests:

    function save(data, url) {
        return $http({
            method: 'POST',
            url: url,
            headers: {'Content-Type': 'application/json'},
            data: angular.toJson(data)
        });
    }
    function update(data, url) {
        return $http({
            method: 'PUT',
            url: url + data.id,
            headers: {'Content-Type': 'application/json'},
            data: angular.toJson(data)
        });
    }

needless to say I became totally confused!


UPDATE: It seems to be a problem with Laravel validation process. when the validation runs, request become erroneous. see the following piece of code:

public function altUpdate(Request $request){
    $this->authorize('editCustomer', $this->store);
    if (!$request->has('customer')){
        return response()->json(["message"=>"Problem in received data"],422);
    }
    $id = $request->customer['id'];
    $rules = [
        'name' => 'required',
        'mobile' => "required|digits:11|unique:customers,mobile,$id,id,store_id,$this->store_id",
        'phone' => 'digits_between:8,11',
        'email' => "email|max:255|unique:customers,email,$id,id,store_id,$this->store_id",
    ];
    //return response()->json(["problem in data"],422); //this one works properly if uncommented
    $validator = Validator::make($request->customer,$rules);
    if ($validator->fails()){
        $errors = $validator->errors()->all();
        Log::info($errors);
        return response()->json(["problem in data"],422);//this one is received in client side as a text/html response with code 200
    }

    $customer = Customer::find($id);
    $customer->update(wrapInputs($request->all()));

    if ($request->tags) {
        $this->syncTags($request->tags, $customer);
    }
    $message = "Customer updated successfully!";
    return response()->json(compact('message'));
}

I still don't know what's the problem of validation process. this code is working on my local machine without any problems but on the production server problem occurs.

2
  • Anything in the error logs? Normally a 200 can happen from a request that fails but does not throw a proper error code. It can be good to set http_response_code(500) as the first line in the response code so it will default to 500 instead of 200. Commented Nov 15, 2016 at 21:04
  • No, nothing in logs. where I should use the set_http_response(500)? before validation? @shylor Commented Nov 15, 2016 at 21:06

1 Answer 1

3

I finally got that. I had added a language file and the file was encoded in UTF-8-BOM, when I converted that file to UTF-8 without BOM things become correct.

the file was resources/lang/[the language]/validation.php and because of the encoding problem the headers were being sent while processing this file.

This question also helped me to find the problem: Laravel redirect::route is showing a message between page loads

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.