Postman测试Laravel 9 API出现Error: socket hang up的原因排查
Laravel 9 API调用/register路由出现「套接字中断」错误的排查方案
问题描述
我在开发Laravel 9 API并测试时,使用Postman调用http://localhost:8000/api/register路由,收到以下错误:
错误:套接字中断
检查源代码后未发现明显问题,且当移除Postman请求体中的参数时,能正常返回验证错误:
{ "message": "name字段是必填项(还有2个错误)", "errors": { "name": [ "name字段是必填项。" ], "email": [ "email字段是必填项。" ], "password": [ "password字段是必填项。" ] } }
相关代码如下:
api.php
<?php use App\Http\Controllers\ProductController; use App\Http\Controllers\AuthController; use Illuminate\Http\Request; use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ // Public Routes Route::post('/register', [AuthController::class, 'register']); Route::get('/products', [ProductController::class, 'index']); Route::get('/products/search/{name}', [ProductController::class, 'search']); Route::get('/products/{$id}', [ProductController::class, 'show($id)']); // Protected Routes Route::group(['middleware' => ['auth:sanctum']], function() { Route::post('/products', [ProductController::class, 'store']); Route::put('/products/{$id}', [ProductController::class, 'update ']); Route::delete('/products/{$id}', [ProductController::class, 'delete']); });
AuthController.php
<?php namespace App\Http\Controllers; use App\Models\User; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Hash; class AuthController extends Controller { public function register(Request $request) { $fields = $request->validate([ 'name' => 'required|string', 'email' => 'required|string|unique:users,email', 'password' => 'required|string|confirmed' ]); $user = User::create([ 'name' => $fields['name'], 'email' => $fields['email'], 'password' => bcrypt($fields['password']) ]); $token = $user->createToken('myapptoken')->plainTextToken; $response = [ 'user' => $user, 'token' => $token ]; return response($response, 201); } }
可能的原因及解决方法
路由定义语法错误:
查看api.php中的路由,发现多处语法错误:Route::get('/products/{$id}', [ProductController::class, 'show($id)']);:路由参数不应带$,应改为{id};控制器方法调用只需写方法名,无需传参,改为[ProductController::class, 'show']Route::put('/products/{$id}', [ProductController::class, 'update ']);:参数同理改为{id},且update后多了空格,需删除空格改为update
这些错误会导致路由解析失败,请求处理时触发未捕获异常,进而引发套接字中断。修正后重启服务测试。
请求参数不完整:
验证规则中password使用了confirmed规则,要求必须同时传入password和password_confirmation两个字段。如果Postman中只传了password,会导致验证通过后创建用户环节出现异常,Laravel无法正常返回响应,引发连接中断。检查请求体是否包含password_confirmation字段,且与password值一致。Laravel服务异常:
即使php artisan serve显示运行中,服务可能已进入异常状态。尝试重启服务:按Ctrl+C停止当前服务,重新执行php artisan serve。端口或网络问题:
- 确认8000端口未被其他程序占用,可通过
netstat -ano | findstr :8000(Windows)或lsof -i :8000(Linux/macOS)检查 - 换端口启动服务:
php artisan serve --port=8001,用新地址http://localhost:8001/api/register测试 - 检查Postman代理设置,关闭代理后直接访问本地地址,同时确认防火墙未拦截本地请求
- 确认8000端口未被其他程序占用,可通过
查看日志定位问题:
打开Laravel日志文件storage/logs/laravel.log,里面会记录详细的错误堆栈信息,能直接定位到具体问题(比如数据库连接失败、路由解析错误等)。
内容的提问来源于stack exchange,提问作者SyncMasta
相关产品推荐
相关产品推荐

