如何通过标头设置cookie?

问题描述:

在我的验收测试,我试图发送一个JSON请求有一个cookie:如何通过标头设置cookie?

// CartTest.php 
$response = $this->postJson(route('carts.store'), [], [ 
    'Cookie' => 'uuid='.Crypt::encrypt($cart->uuid) 
]); 

当我在控制器里做$request->hasCookie('uuid'),我得到false

我也尝试了不同的变化,如Set-Cookie,HTTP_SET_COOKIE等,但没有一个似乎工作。为了简洁起见,我宁愿传递postJson而不是通用的call

任何想法?谢谢

+0

您是否尝试过HTTP_COOKIE? –

我想我现在明白了为什么Laravel在其postJson方法签名中不接受任何cookie。事实上,it's not a recommended practice首先与API交换cookie。这回答了这个问题。

为什么不试试这个?在提出要求之前:

$_COOKIE['uuid'] = Crypt::encrypt($cart->uuid); 

是的,它是大胆的。但似乎也起作用。还有other ideas