lomboboo
lomboboo

Reputation: 1233

Symfony 3(FOSrestBundle) + Angular 1 POST request is empty

Here is my Angular service :

restFactory.getCalcPrice = function (data) {
    return $http({
        url:"/api/calc",
        method:"POST",
        data:{"data": data},
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    })
};

FOSRestBundle config:

fos_rest:
param_fetcher_listener: true
body_listener: true
body_converter:
    enabled: true
routing_loader:
    default_format: json
view:
    view_response_listener: 'force'
    mime_types:
        json: ['application/json', 'application/json;version=1.0', 'application/json;version=1.1']
format_listener:
    rules:
        - { path: ^/api, priorities: [ json ], fallback_format: json, prefer_extension: true }
        - { path: ^/, priorities: [ 'html', '*/*'], fallback_format: ~, prefer_extension: true }

And Symfony 3 contoller:

    /**
   * @Post("/calc")
   * @View()
   * @return array
   */
  public function getCalcPrice(Request $request)
  {
    $req = $request->request->all();
    $data = $req['data'];
    $data = array(
      "data" => $data // in Angular recieving null
    );

    return $data;
  }

So the problem is when I posting some data from Angular I can't get it in Symfony. GET request working fine. Any suggestions? :)

This is screenshot from Chrome debug: enter image description here

Upvotes: 1

Views: 647

Answers (1)

martin
martin

Reputation: 96891

According to the API doc for Request class property $request->request gives you access to all $_POST params but you're sending your data as JSON in payload:

So you should call method getContent() and decode it manually:

$content = $request->getContent();
$json = json_decode($content, true);
$data = $json['data'];

Upvotes: 2

Related Questions