【发布时间】:2018-09-06 15:10:36
【问题描述】:
我正在开发 iOS 应用
我自己做了 POST API。
现在,当我从 iOS 按下按钮时,我创建了一个函数来访问 APi 并发布值。
我尝试输入值以使用 PostMan 测试一次 API,但出现错误。 The page has expired due to inactivity.
我了解此错误是因为 POST 不包含 csrf_token。
如果在 iOS 上使用 csrf_token 进行认证,那么认证方式是什么?另外,还有其他的认证方式吗?
快速按钮功能
@IBAction func bookmarkBtn(_ sender: Any) {
let user_id = defaultValues.string(forKey: "id")
let urlString = "http://127.0.0.1:8000/store/favorite"
let request = NSMutableURLRequest(url: URL(string: urlString)!)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let params:[String:Any] = [
"user_id": user_id,
"store_id" : store_id,
]
do{
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
let task:URLSessionDataTask = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: {(data,response,error) -> Void in
let resultData = String(data: data!, encoding: .utf8)!
print("result:\(resultData)")
print("response:\(response)")
})
task.resume()
}catch{
print("Error:\(error)")
return
}
Laravel 收藏控制器
public function favorite(Request $request){
Favorite::create(
array(
'user_id' => $request->user_id,
'store_id' => $request->store_id,
)
);
return ['Status' => 'Success'];
}
Laravel 路由/web.php
Route::post('/store/favorite', 'FavoriteController@favorite');
【问题讨论】:
-
在请求中跳过 csrf 令牌,因为在每个请求中 laravel 都会生成新的 csrf 令牌所以..
标签: ios swift laravel api post