【问题标题】:404 Not Found in API testingAPI 测试中未找到 404
【发布时间】:2019-05-03 13:08:18
【问题描述】:

我在 Laravel 中做了我的 API 后端

我测试了Route::post('register', 'UserController@register'); 使用http://localhost/cloudengine-sandbox/cloudsandboxbackend/public/api/register

它工作正常。 我想测试简码时遇到问题。

api.php

Route::post('register', 'UserController@register');
Route::post('login', 'UserController@login');
Route::get('profile', 'UserController@getAuthenticatedUser');

Route::get('/shortcodes', 'ShortcodeController@index')
    ->name('shortcodes.index');

Route::post('/shortcodes', 'ShortcodeController@store')
    ->name('shortcodes.store');

Route::get('/shortcodes/{shortcode}', 'ShortcodeController@show')
    ->name('shortcodes.show');

Route::put('/shortcodes/{shortcode}', 'ShortcodeController@update')
    ->name('shortcodes.update');

Route::delete('/shortcodes/{shortcode}', 'ShortcodeController@destroy')
    ->name('shortcodes.destroy');

我在 POSTMAN 中输入什么来测试每个简码。

我输入了http://localhost/cloudengine-sandbox/cloudsandboxbackend/public/api/shortcodes/show

我希望可以,但给了 404 Not found

【问题讨论】:

  • 您是否在邮递员的 get 请求中传递了正确的参数?如 Key : shortcode, value : show?
  • 发布控制器的显示方法
  • 控制器显示方法:' public function show(Shortcode $shortcode) { return $shortcode; }'

标签: laravel postman


【解决方案1】:

如果您正在为您的应用程序编写测试用例,那么

.env 中设置您的应用网址。

APP_URL=http://web.local  //Any url that you  are using.

因为 laravel 使用来自 .envAPP_URL 创建一个 url。

如果您使用 POSTMAN 进行测试

还要确保在 POSTMAN 中传递标头 ACCEPT=application/json

【讨论】:

    【解决方案2】:

    Routes 上的名称只是快捷方式,但是当作为 url 访问它们时,您仍然需要使用定义的整个 url。

    在您的示例中,网址必须是:

    http://localhost/cloudengine-sandbox/cloudsandboxbackend/public/api/shortcodes/{shortcode}

    {shortcode} 替换为数据库中短代码的有效 ID。

    确保将shortcode 绑定到RouteServiceProvider 上的模型。

    【讨论】:

    • 如何在 Angular 7 上使用它
    猜你喜欢
    • 2021-02-21
    • 1970-01-01
    • 2016-11-28
    • 2016-06-05
    • 2020-01-22
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    相关资源
    最近更新 更多