0

I'm building a RESTful API with Laravel 5.2 and I have an AngularJS 1.5 front end. I am successfully writing services to get information but I am having troubble putting or posting anything to the database when I pass it to the API. I've tried doing some searching and but I just don't understand how to actually save data I would send the API. Here is my attempt so far:

-Service from the Factory-

addReceipt: function(request) { 
            return $http.post(api_url + "/rewards/receipts/add", request).then(function(results) {
              console.log(results);
              return results.data;
            });
        }

-From the Controller

$scope.submitReceipt = function() {
                rewardsFactory.addReceipt($scope.model).then(function() {
                    console.log($scope.model);
                    toaster.pop({ type: 'success', title: 'Claim Submitted!', body: "Thanks! We'll take a look at your claim shortly.", showCloseButton: true });
                });
            };

-From Laravel API routes

Route::post('rewards/receipts/add', 'Rewards\RewardsController@addReceipt');

-From Laravel Controller

public function addReceipt(Request $request)
    {
        //Add the Receipt
        DB::table('receipts')->insert(
            ['transaction_id' => $request->input('transactionID'), 
             'client_id' => $request->input('client_id'), 
             'location_id' => $request->input('location_id') ]
        );
    }

My Current Cors setup seems to be working out well enough for at least some traffic so I don't think that is the problem but I'm just still not sure what I'm doing wrong.

4
  • Do you know where the breakdown is? When in doubt, log, log, log. Commented Feb 18, 2016 at 19:09
  • Does your frontend have the Laravel security token? That borked my last Laravel API for quite a while. Commented Feb 18, 2016 at 20:00
  • \Log::info($request) in the controller to see if your request data gets to the controller. Remember to clear your logs before Commented Feb 18, 2016 at 20:22
  • note that $http does not send form encoded data by default, it sends application/json in request body so $request->input is probably empty. See what json_decode($request->getContent()) does for you Commented Feb 18, 2016 at 20:25

1 Answer 1

1

Note that $http does not send form encoded data by default, it sends application/json in request body.

I don't do any work with laravel but if you check $_POST you will see it is empty so $request->input is probably empty also.

In php you can access the response body using :

json_decode(file_get_contents('php://input')[,true/*optional to convert to array*/])

I believe that json_decode($request->getContent()) will do the same in laravel

The alternative is to use the following $http set up taken from the docs to send form encoded data

.controller(function($http, $httpParamSerializerJQLike) {
  //...

  $http({
    url: myUrl,
    method: 'POST',
    data: $httpParamSerializerJQLike(myData),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });

});

You can also set $http.defaults in a run block so all post or put are sent as x-www-form-urlencoded and not have to add the config to each use

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.