如何在Laravel中检索使用Http facade发送的所有标头?

如何在Laravel中检索使用Http facade发送的所有标头?,laravel,curl,Laravel,Curl,基于此,您可以通过CURL客户端生成请求。 在这里,断言特定的选项与Http::assertSent一起发送,但是给定了这样一个特定的请求 Http::withToken('mytoken')->withHeaders([ 'X-First' => 'foo', ])->post('http://test.com/users', [ 'name' => 'Taylor', 'role' => 'Developer', ]); 如何获取发送给

基于此,您可以通过CURL客户端生成请求。 在这里,断言特定的选项与Http::assertSent一起发送,但是给定了这样一个特定的请求

Http::withToken('mytoken')->withHeaders([
    'X-First' => 'foo',
])->post('http://test.com/users', [
    'name' => 'Taylor',
    'role' => 'Developer',
]);

如何获取发送给调试它的请求头的原始表示形式?

客户机的文档概述了一种方法,您可以断言一个特定的头,也可以选择性地断言该值

听起来你想把所有的标题都倒出来

如果我们查看文档,当您断言已发送请求时,会向您传递一个
illighte\Http\Client\request
的实例。 如果我们看一下
illumb\Http\Client\Request
,有一种获取所有头的公共方法:

/**
 * Get the request headers.
 *
 * @return array
 */
public function headers()
{
    return collect($this->request->getHeaders())->mapWithKeys(function ($values, $header) {
        return [$header => $values];
    })->all();
}
所以在你的测试中,你可以这样做:

Http::fake();

//... your test

Http::assertSent(function ($request) {
    dump($request->headers());

    // replace this with an actual assertion but it’s needed to print the dump out
    return true;
});

嗨,你测试过这个吗?我收到一个未录制的预期请求。断言false为true失败。明白了,您必须返回true才能打印结果,谢谢!您可能需要更新答案以添加此内容。我以前使用过此方法检查内容,是的。你得到这个错误是因为你没有从你的断言中返回真或假。我不知道你想断言什么,所以我不能为你写断言。