我正在尝试使用 Guzzle 发送 post http 请求,但出现错误 500
Internal server error
ServerException in RequestException.php line 113:
Server error: `POST https://domainname/api/v1/user/register` resulted in a `500 Internal Server Error` response:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="robots" content="noindex,nofo (truncated...)
我尝试使用 Postman 发送相同的标头和正文密钥,它没有任何问题:
{
"status": "true"
}
这是我的 Guzzle 客户请求:-
$client = new GuzzleHttp\Client(['base_uri' => 'https://domainname/api/v1/']);
$response = $client->request('POST', 'user/register', [
'headers' => [
'Authorization' => Session::get('token'),
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => [
'start_date' => $start_date,
'expiry_date' => $expiry_date,
'type' => 'normal',
'status' => 'active',
]
]);
$response= $response->getBody();
$re= json_decode($response);
我到该 api 的路线:-
Route::post('/user/register', 'UsersAPI@register');
我的注册控制器:
public function register(Request $request){
Log::info('Request: ' . $request);
//dd($request->all());
$validator = Validator::make($request->all(), [
// 'user_id' => 'required',
'start_date' => 'required',
'expiry_date' => 'required',
'type' => 'required',
'status'=>'required',
//'receipt' => 'required',
//'purchase_id'=>'required',
]);
$user = JWTAuth::toUser();
//Check whether validation is failed or passed
if($validator->fails()){
//Redirect back with validation errors
return response()->json(['error' => $validator->errors()], 400);
}
$user = new User($data);
$user->user()->save($user);
Log::info('save_user done for this user: ' . $user->id);
return ['status' => 'true'];
}
NGINX 错误.log :-
[02/Dec/2017:15:49:13 +0300] "POST /api/v1/user/register HTTP/1.1" 500 16904 "-" "GuzzleHttp/6.2.1 curl/7.55.0 PHP/7.1.9"
如果相同的代码根据您发送请求的方式产生不同的结果,我认为请求负载之间仍然存在差异。
您仔细检查过 Session::get('token')、$start_date 和 $expiry_date 的内容吗?