[英]How come in Postman my post request works but not in the browser even though I'm doing the same?
In Postman, I can successfully authenticate (I'm using Passport) using my endpoint.在 Postman 中,我可以使用我的端点成功进行身份验证(我正在使用 Passport)。 I can then open up a new tab and use a different endpoint to post to my SQL database - the file name (which the user uploads), email, and user_id.
然后我可以打开一个新选项卡并使用不同的端点发布到我的 SQL 数据库 - 文件名(用户上传)、email 和 user_id。
In the browser, I can successfully authenticate.在浏览器中,我可以成功进行身份验证。 However, when I try to upload a file and send along the user's bearer token (to detect which user's uploading), I get a 500 error that says:
但是,当我尝试上传文件并发送用户的不记名令牌(以检测哪个用户正在上传)时,我收到一个 500 错误,上面写着:
ErrorException: Trying to get property 'id' of non-object in file.
I've tried to send the email and id in my post request but I get the same error.我尝试在我的发布请求中发送 email 和 id,但我得到了同样的错误。 What am I doing wrong?
我究竟做错了什么?
Backend controller code:后端 controller 代码:
public function store(Request $request) {
$filePath = $request->file('file')->getClientOriginalName();
$id = $request->user()->id;
$email = $request->user()->email;
$data = [
'file_path' => $filePath,
'user_id' => $id,
'email' => $email
];
DB::table('my db.photos')->insert($data);
echo "Record inserted successfully.<br/>";
}
Frontend code:前端代码:
fileUpload(file) {
const formData = new FormData()
console.log(this.state.access_token);
formData.append('file', file);
formData.set('access_token', "Bearer " + this.state.access_token);
// formData.set('email', this.getEmail());
// formData.set('user_id', this.getId());
fetch('http://127.0.0.1:8000/api/auth/myendpont/wall-of-fame', {
method: 'POST',
body: formData
})
.then(response => console.log(response))
.catch(error => { console.error(error) });
}
maybe like this?也许像这样?
formData.append('access_token', "Bearer " + this.state.access_token);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.