Benjamin B.
Benjamin B.

Reputation: 483

FosRestBundle + AngularJS : Post request is empty

I'm working on an API with FosRestBundle and AngularJS for the front part.

GET requests working but i have problems with POST requests. In the API controller the request object is empty..

Here is the front part

 $http.post(Route.api + '/leads', {lead: "test"}).success(function(data) {

 });

And here is the FosRestBundle controller

public function postAction(Request $request) {
    var_dump($request->request->all()); // empty ? :(
}

Someone already have the same problem ?

Thanks ;)

Upvotes: 0

Views: 959

Answers (1)

Vahe Shadunts
Vahe Shadunts

Reputation: 1966

Yes I had such problem, and I've solve this problem passing Content-Type header to Symfony, here is my code example

return $http({
            method: 'POST',
            url: Route.api + '/leads',
            data: dataYouWantToSend, //see the note regarding this parameter
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })

NOTE : If you pass data parameter with object, you will receive in in the POST your data by JSON with key of your JSON's root.

If you want to receive it like form data, you have to convert it to querystring (e.g. ?deal=test&ok=1), for example if you're using jQuery too, you can convert it like this

var dataYouWantToSendQueryString = $.param(dataYouWantToSend);

hope it will help you too.

P.S. I've researched and haven't found how to convert object to query string in angular, so if Angular has such functionality, I will glad to know about it, if someone know, please comment.

Upvotes: 1

Related Questions