Guzzle HTTP request transforms from POST to GET

不打扰是莪最后的温柔 提交于 2019-12-11 11:19:59

问题


I have this very weird thing going on when trying to make post to an external API, I try to make a POST request to the URL but Guzzle make a GET request instead (which is a legal action on this API but returns something different).

Here is the code:

$request = $this->client->createRequest('POST', 'sessions', [
  'json' => [
    'agent_id' => $agentId,
    'url' => $url
  ],
  'query' => [
    'api_key' => $this->apiKey
  ]
]);

echo $request->getMethod(); // comes out as POST
$response = $this->client->send($request);
echo $request->getMethod(); // suddenly becomes GET

Same thing happens when I use $this-client->post(…)

I really have no idea what to do next.


回答1:


You're probably getting a 3xx status code so that that the Redirect subscriber kicks in (redirect is enabled by default). From the docs:

[...] Pass an associative array containing the ‘max’ key to specify the maximum number of redirects and optionally provide a ‘strict’ key value to specify whether or not to use strict RFC compliant redirects (meaning redirect POST requests with POST requests vs. doing what most browsers do which is redirect POST requests with GET requests).

//edit Just saw you kinda answered that yourself in the question comments - still leaving this answer online as it provides some context.




回答2:


Try to change the key 'query' to 'body'.




回答3:


Please switch query to form_params. In Guzzle 6 it works.



来源:https://stackoverflow.com/questions/29470839/guzzle-http-request-transforms-from-post-to-get

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!