【问题标题】:Symfony Post data receive with form but not with Rest clientSymfony Post 数据通过表单接收,但不通过 Rest 客户端接收
【发布时间】:2017-11-19 09:27:44
【问题描述】:

你能帮帮我吗?

我使用高级 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>

我检查 url 是否正确,在这两种情况下我进入控制器。 我不认为我的 .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>

我不明白,因为表单工作而不是 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
                      "

感谢您的帮助。

【问题讨论】:

    标签: rest api symfony-2.1


    【解决方案1】:

    我找到了解决方案,问题是标题。为了解决问题,我必须在标题中形成数据,并且没有 x-www-form-urlencoded 哪里是 $_POST 无法获取的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2020-08-13
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      相关资源
      最近更新 更多