大量的HTTP请求从POST转换为GET [英] Guzzle HTTP request transforms from POST to GET

查看:683
本文介绍了大量的HTTP请求从POST转换为GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试发布到外部API时,我发生了一件非常奇怪的事情,我尝试向URL发出POST请求,但是Guzzle却发出GET请求(这是对此API的合法操作,但返回了一些内容不同).

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).

这是代码:

$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

当我使用$this-client->post(…)

我真的不知道下一步该怎么做.

I really have no idea what to do next.

推荐答案

我遇到了同样的问题. 原因是当存在代码为301或302的位置重定向时,Guzzle会将Request-Method更改为"GET". 我在 RedirectMiddleware.php <中找到了问题代码" /a>.

I run into the same problem. the reason is that Guzzle Changes the Request-Method to 'GET' when there is a location Redirect with code 301 or 302. I found the 'Problem-Code' in the RedirectMiddleware.php.

但是,当您看到if条件时,可以通过在选项中添加'allow_redirects'=>['strict'=>true]来禁用此行为. 找到此选项后,我发现该选项已列在枪口选项文档

But when you see the if-condition you can disable this behavior by adding 'allow_redirects'=>['strict'=>true] to your options. After finding this option, I discovered that the option is listed in the Guzzle Options Documentation

因此您必须像这样重写您的createRequest:

So yust rewrite your createRequest like this:

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

并且重定向后应保留方法POST.

And it should stay Method POST after the redirect.

这篇关于大量的HTTP请求从POST转换为GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆