简体   繁体   中英

FOSRestBundle ParamFetcher POST method

I'm creating restful API with FOSRestBundle. When I send data via ajax with using GET method, everything works ok. But I have to send data by POST, I try to get values from POST with ParamFetcherListener, but it returns null values. When I change the request method to GET, it works. What am I doing wrong?

My code:

/**
 * @Rest\View(statusCode=201)
 * @QueryParam(name="test", description="test")
 */
public function createAction(Request $request, ParamFetcherInterface $paramFetcher)
{
    $test = $paramFetcher->get('test'); // it's null
}

And config.yml:

fos_rest:
    param_fetcher_listener: true
    body_listener:
        decoders:
            json: fos_rest.decoder.json
    view:
        view_response_listener: true
        formats:
            xml: true
            json: true
    routing_loader:
        default_format: json
    format_listener:
        rules:
            - { path: '^/api/', priorities: ['json', 'xml'], fallback_format: 'html', prefer_extension: false }    

Routing:

  _wdt                       ANY      ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY      ANY      ANY    /_profiler/                        
  _profiler_search           ANY      ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY      ANY      ANY    /_profiler/search_bar              
  _profiler_info             ANY      ANY      ANY    /_profiler/info/{about}            
  _profiler_phpinfo          ANY      ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY      ANY      ANY    /_profiler/{token}/search/results  
  _profiler                  ANY      ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY      ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY      ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY      ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test           ANY      ANY      ANY    /_error/{code}.{_format}           
  object_all                 GET      ANY      ANY    /api/objects                       
  saved_object_all           GET      ANY      ANY    /api/saved_objects                 
  saved_object_get           GET      ANY      ANY    /api/saved_objects/{id}            
  saved_object_new           POST     ANY      ANY    /api/saved_objects                 
  saved_object_delete        DELETE   ANY      ANY    /api/saved_objects/{id}            
  object_test                ANY      ANY      ANY    /api/test  

Thanks in advance.

The accepted solution reverts to using the standard request object.

If you want to use ParamFetcher with a post request you need to use

@RequestParam

instead of

@QueryParam

in the annotation of your method.

/**
 * @Rest\View(statusCode=201)
 * @RequestParam(name="test", description="test")
 */
public function createAction(Request $request, ParamFetcherInterface $paramFetcher)
{
    $test = $paramFetcher->get('test'); // it's null
}

My mistake, of course data in POST requests is sent in body, body listener decoder is set, so this is the correct solution:

/**
 * @Rest\View(statusCode=201)
 */
public function newAction(Request $request)
{
    $test = $request->request->get('test'); 
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM