Do not work ParamConverter with "fos_rest.request_body" converter
Asked Answered
A

1

8

I'm trying to use FOSRestBundle's request body converter but my current implementation doesn't seem to work.

I am using Symfony2, Propel, AngularJS (it sends data to server)

What am i doing wrong?

config.yml:

fos_rest:
    routing_loader:
    default_format: json
    include_format:       false     
view:
    view_response_listener: force
body_listener: true
param_fetcher_listener: true
body_converter:
    enabled: true

sensio_framework_extra:
    view:    { annotations: false }
    router:  { annotations: true }
    request: { converters: true }

Method:

/**
 * @View()
 * @Put("/documenttypes")
 * @ParamConverter("documentType", converter="fos_rest.request_body")
 */
public function putAction(DocumentType $documentType)
{
    print_r($documentType);
    return $documentType;
}

In result I have empty model object:

Backend\SettingsBundle\Model\DocumentType Object
(
  [id:protected] => 
  [name:protected] => 
  ....
)

To check that data comes to server, I modify method:

public function putAction(DocumentType $documentType)
  {
    $content = $this->get("request")->getContent();
    $documentType->fromArray(json_decode($content, true));

    print_r($documentType);
    return $documentType;
  }

This method works and fills model field:

Backend\SettingsBundle\Model\DocumentType Object
(
  [id:protected] => 2
  [name:protected] => 'Oferta'
  ....
)
Achromaticity answered 2/2, 2014 at 15:2 Comment(0)
C
3

Symfony is complaining that You need to enable fos_rest.converter.request_body service.

In your fos_rest configuration section within config.yml you need to enable body_converter feature:

fos_rest:
  body_converter:
    enabled: true

Now if you list your services, you will notice that service fos_rest.converter.request_body has appeared:

user@host:~/symfony-project$ php app/console debug:container | grep fos_rest.converter
 fos_rest.converter.request_body       FOS\RestBundle\Request\RequestBodyParamConverter        
Costa answered 28/11, 2016 at 21:17 Comment(2)
Thank you for you answer, but I'm not work with Symfony anymore, so I can't confirm you answer. Sorry.Achromaticity
It returns No converter named "fos_rest.converter.request_body" found for conversion of parameter XXX.Discordance

© 2022 - 2024 — McMap. All rights reserved.