Symfony Post数据以表单形式接收,但不与Rest客户端接收

时间:2017-06-16 14:39:41

标签: rest api symfony-2.1

你能帮我吗?

我使用高级Rest客户端chrome扩展来测试我的api symfony。我的问题是当我发送帖子数据时,变量$ _POST和$ request无效。 但我在我的wampserver中有一个表格供测试。

<form action="url" method="post">
<input name="access_token"  value="my token"/>
<input name="json" value='here my json file' />
<input type="submit" class="btn btn-primary" value="send" name="send" />
</form>

我检查网址是对的,在这两种情况下我进入控制器。 我不认为问题出在我的.htaccess:

    <IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

我不理解beacause表单工作,但不了解Rest客户端chrome的数据。

我收到的数据是

Request {#8 ▼
  +attributes: ParameterBag {#11 ▶}
  +request: ParameterBag {#9 ▼
    #parameters: array:3 [▶]
  }
  +query: ParameterBag {#10 ▶}
  +server: ServerBag {#14 ▶}
  +files: FileBag {#13 ▶}
  +cookies: ParameterBag {#12 ▶}
  +headers: HeaderBag {#15 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/api/v2/imports/initials.json"
  #requestUri: "url"
  #baseUrl: "url"
  #basePath: null
  #method: "POST"
  #format: "xml"
  #session: Session {#413 ▶}
  #locale: null
  #defaultLocale: "fr"
  -isForwardedValid: true

和休息后请求

SyncTwoController.php on line 47:
Request {#8
  +attributes: ParameterBag {#11
    #parameters: array:6 [
      "media_type" => "application/json"
      "_controller" => "AppBundle\Controller\Rest\SyncTwoController::postImportInitialAction"
      "_format" => "json"
      "_route" => "post_import_initial"
      "_route_params" => array:1 [
        "_format" => "json"
      ]
      "paramFetcher" => ParamFetcher {#441
        -container: appDevDebugProjectContainer {#370 …12}
        -parameterBag: ParameterBag {#443
          -paramReader: ParamReader {#442
            -annotationReader: CachedReader {#337
              -delegate: AnnotationReader {#336
                -parser: DocParser {#335
                  -lexer: DocLexer {#334
                    #noCase: array:9 [
                      "@" => 101
                      "," => 104
                      "(" => 109
                      ")" => 103
                      "{" => 108
                      "}" => 102
                      "=" => 105
                      ":" => 112
                      "\" => 107
                    ]
                    #withCase: array:3 [
                      "true" => 110
                      "false" => 106
                      "null" => 111
                    ]
                    -input: null
                    -tokens: []
                    -position: 0
                    -peek: 0
                    +lookahead: null
                    +token: null
                  }
                  -target: null
                  -isNestedAnnotation: false
                  -imports: []
                  -classExists: []
                  -ignoreNotImportedAnnotations: false
                  -namespaces: []
                  -ignoredAnnotationNames: []
                  -ignoredAnnotationNamespaces: []
                  -context: ""
                }
                -preParser: DocParser {#333
                  -lexer: DocLexer {#332
                    #noCase: array:9 [
                      "@" => 101
                      "," => 104
                      "(" => 109
                      "

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,问题是标题。为了解决我必须在标题中形成数据而没有x-www-form-urlencoded,其中信息无法通过$ _POST获得。