2

在文档中创建 HTTP 请求时我需要进行身份验证

        $httpClient = HttpClient::create([
            'auth_basic' => ['user@gmail.com', '45@8888'],
        ]);

$httpClient = HttpClient::create([
    // HTTP Basic authentication with only the username and not a password
    'auth_basic' => ['the-username'],

    // HTTP Basic authentication with a username and a password
    'auth_basic' => ['the-username', 'the-password'],

    // HTTP Bearer authentication (also called token authentication)
    'auth_bearer' => 'the-bearer-token',
]);

所以写的时候

$response = $httpClient->request('GET', 'https://...', [
    // use a different HTTP Basic authentication only for this request
    'auth_basic' => ['the-username', 'the-password'],

    // ...
]);

抛出异常

Symfony\Component\HttpClient\Exception\ClientException: HTTP/1.1 400 Bad Request 返回“ http://site.test/login_check ”。

是否因为我没有发布字段 user[_token] 而抛出异常?如果是,如何生成它并添加到请求中?如果有人已经使用此组件登录,请给我代码:)

4

1 回答 1

1

我使用了它,它可以尝试,用于身份验证和请求:

$httpClient = HttpClient::create();
    $uri = 'http://test:xxxx/login_check';
    $response = $httpClient->request('POST', $uri, [
        'headers' => [
            'Content-Type' => 'application/json',
        ],
        'json' => [
            'username' => 'jhon.doe@gmail.com',
            'password' => 'jhon.doe'
        ],
    ]);
     $statusCode = $response->getStatusCode();

     if($statusCode  == 200) {
        $content = $response->getContent();
        dd($content);
      }
    
    
// JWT Request Http 
$uri = 'http://test.xxx/api/xxxx';
    $response = $httpClient->request('GET', $uri, [
        'headers' => [
            'Content-Type' => 'application/json',
            'Authorization' => 'Bearer '.TOKEN
        ]
    ]);

    $statusCode = $response->getStatusCode();
    $content = $response->getContent();
    dd($content);
   //the easiest
    $httpClient->request('POST', $url, [
            'auth_bearer' => $jwt,
            'body' => $data,
        ])->getContent();

于 2019-07-09T11:37:19.027 回答