Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Can';t在针对API路由的Laravel测试中执行POST请求_Php_Laravel_Phpunit - Fatal编程技术网

Php Can';t在针对API路由的Laravel测试中执行POST请求

Php Can';t在针对API路由的Laravel测试中执行POST请求,php,laravel,phpunit,Php,Laravel,Phpunit,我正在为我的Laravel应用程序编写功能测试,在那里我对我的服务执行一些请求 在尝试测试我的API时,所有GET请求都可以正常工作,但所有POST请求都返回以下响应: <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="refresh" content="0;

我正在为我的Laravel应用程序编写功能测试,在那里我对我的服务执行一些请求

在尝试测试我的API时,所有GET请求都可以正常工作,但所有POST请求都返回以下响应:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="refresh" content="0;url='http://localhost'" />

        <title>Redirecting to http://localhost</title>
    </head>
    <body>
我的api路由如下所示:

$this->post('api/my/route')->dump();
Route::prefix('my')->group(function() {
    Route::post('/route', function() {
        return 'ok';
    });
$this->postJson('api/my/route')
是否有任何中间件等。在创建这样的请求之前,我可能需要更改/停用


使用
web.php
routes

时,这些请求可以正常工作。您需要返回json响应。json方法将自动将内容类型头设置为application/json,并使用json_encode PHP函数将给定数组转换为json:

因此,您确实应该返回以下数据

Route::prefix('my')->group(function() {
    Route::post('/route', function() {
        return response()->json(['message'=>'ok']);
    });
如前所述,您应该返回JSON

在测试中,您应该像这样添加
postJson

$this->post('api/my/route')->dump();
Route::prefix('my')->group(function() {
    Route::post('/route', function() {
        return 'ok';
    });
$this->postJson('api/my/route')
通过检查


祝你好运

您在收到请求后得到了哪些响应?您能显示响应吗?您是否尝试了
$this->postJson('api/my/route')
?@mare96谢谢!postJson似乎做到了这一点,尽管我没有从我的邮箱返回JSONroute@topiji不客气。我添加了一个更精确的答案。@topiji您知道(我当然不知道)对于代码块中的语言规范,您必须使用小写的
``html
(而不是
``html
)?