Sending request with guzzle with raw data

﹥>﹥吖頭↗ 提交于 2019-12-23 16:23:28

问题


I want to send a request with guzzle that looks like this.

Post man request

Thing is, I always get

Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: POST http://localhost:8080/project/user/login resulted in a 400 Bad Request

Here is the part of the code that creates and sends the request:

if (isset ($_REQUEST['username']) && isset($_REQUEST['password'])){
require '../php/vendor/autoload.php';

$myClient = new GuzzleHttp\Client();

$request = $myClient->post("http://localhost:8080/project/user/login");
$request->setBody($_REQUEST['username']. ',' . $_REQUEST['password']);
$feed_response = $request->send();

I've read most of the guzzle's manual, but it got me nowhere.


回答1:


What version of Guzzle are you using. Use the latest version. In the latest version of Guzzle, a http POST request is sent as:

$client->request('POST', '/post', [
    'form_params' => [
        'foo' => 'bar',
        'baz' => ['hi', 'there!']
    ]
]);

See this link: http://docs.guzzlephp.org/en/latest/request-options.html?#form-params



来源:https://stackoverflow.com/questions/41724689/sending-request-with-guzzle-with-raw-data

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