我有一個Laravel應用程序和一個通過Passport連接到它的C#應用程序。laravel護照:爲什麼相同的代碼可以與Http Get請求一起使用,但不適用於POST?
我做了簡單的功能,使用戶更新自己的密碼是這樣的:
Laravel路由
Route::post('user/updatepassword','[email protected]')->middleware('auth:api');
Laravel控制器
public function changepassword(Request $request)
{
$this->validate($request, [ 'password' => 'required|string|max:255' ]);
$newuser = User::find(Auth::user()->id);
$newuser->password = bcrypt($request->password);
$newuser->save();
}
C#功能
public string UpdatePassword(string password)
{
var client = new RestClient(APPConnection.ApiRoot + "user/updatepassword");
//// client.Authenticator = new HttpBasicAuthenticator(username, password);
var request = new RestRequest("", Method.POST);
request.AddParameter("password", password); // adds to POST or URL querystring based on Method
request.AddHeader("Authorization", "Bearer " + Current_User.access_token);
// execute the request
IRestResponse response = client.Execute(request);
var content = response.Content; // raw content as string
return content;
}
此代碼不起作用,它會拋出許多無意義的異常,或者至少我無法弄清楚。 但是,當我更改所有在C#代碼和Laravel結束時使用HTTP GET請求時,它工作正常。
我不確定什麼是錯的。這可能是我傳遞令牌的方式對於GET和POST請求應該是不同的。
這是我得到的答覆的複印件:
{ "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException", "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", "line": 203, "trace": [ { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", "line": 175, "function": "prepareException", "class": "Illuminate\\Foundation\\Exceptions\\Handler", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplication\\app\\Exceptions\\Handler.php", "line": 51, "function": "render", "class": "Illuminate\\Foundation\\Exceptions\\Handler", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 83, "function": "render", "class": "App\\Exceptions\\Handler", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 55, "function": "handleException", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php", "line": 49, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php", "line": 63, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Session\\Middleware\\StartSession", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php", "line": 37, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php", "line": 59, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 102, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", "line": 647, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", "line": 622, "function": "runRouteWithinStack", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", "line": 588, "function": "runRoute", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", "line": 577, "function": "dispatchToRoute", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", "line": 176, "function": "dispatch", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 30, "function": "Illuminate\\Foundation\\Http\\{closure}", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\fideloper\\proxy\\src\\TrustProxies.php", "line": 56, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Fideloper\\Proxy\\TrustProxies", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", "line": 30, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", "line": 30, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php", "line": 27, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php", "line": 46, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 149, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", "line": 102, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", "line": 151, "function": "then", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", "line": 116, "function": "sendRequestThroughRouter", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" }, { "file": "C:\\xampp\\htdocs\\myefapplication\\public\\index.php", "line": 55, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Kernel", "type": "->" } ] }
雖然你沒有顯示錯誤,但我猜這是一個CSRF錯誤。 POST路由默認在Laravel中由CSRF保護,所以您需要傳遞有效的CSRF令牌,或者從CSRF保護中免除更新密碼路由(不推薦)。 – patricus
試試這個'Route :: post('/ user/updatepassword','UserController @ changepassword') - > middleware('auth:api');' – lewis4u
@patricus我添加了我從帖子請求中得到的回覆。我沒有理解任何東西,但你可能會。在laravel文檔的api auth部分中,我沒有讀到有關csrf的任何信息。我認爲令牌是它的替代品。我甚至不知道如何通過第三方應用程序傳遞它。 –