简体   繁体   English

FOSRestBundle ParamFetcher POST方法

[英]FOSRestBundle ParamFetcher POST method

I'm creating restful API with FOSRestBundle. 我正在使用FOSRestBundle创建Restful API。 When I send data via ajax with using GET method, everything works ok. 当我使用GET方法通过Ajax发送数据时,一切正常。 But I have to send data by POST, I try to get values from POST with ParamFetcherListener, but it returns null values. 但是我必须通过POST发送数据,我尝试使用ParamFetcherListener从POST获取值,但是它返回空值。 When I change the request method to GET, it works. 当我将请求方法更改为GET时,它可以工作。 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: 和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 如果您想将ParamFetcher与发布请求一起使用,则需要使用

@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: 我的错误,当然是POST请求中的数据在正文中发送,设置了正文侦听器解码器,因此这是正确的解决方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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