Search code examples
angularjsapisymfonyfosrestbundle

Call a GET in a Rest Api and send POST to give informations to the Rest Api


I'm currently using Symfony with FosRestBundle to create an Api.

In symfony I would like to catch a request from the client to give back some informations. (So a get methode).

This is my code who permit the communication between the api and the client.

Api Side :

public function getPropertiesAction(Request $request){

    $properties = $this->getDoctrine()->getManager()->getRepository('ApiBundle:Achats')->findAll();

    if(null == $properties){
        throw $this->createNotFoundException();
    }

    $data =  findPropertiesInside($properties, $request);

    return $data;
}

Client side :

angular.module('googleMapApp')
    .service('PropertiesData', function ($http) {
        this.fetch = function(url, polygone, callback, errorCallback) {
            $http.post(url,{data: polygone}).then(callback,errorCallback);
        };
});

So when i do that the api return error 405 (Method Not Allowed)

Exception from Symfomy is No route found for "POST /api/properties": Method Not Allowed (Allow: GET, HEAD)

Is there a way to send post json informations from the client to the api and the api return the good properties?


Solution

  • You cannot pass a request body when using HTTP GET. You should pass the data in the url, either using the path or the query string.

    Change your client side to $http.get(urlWithData).then...