简体   繁体   English

为什么在 Postman 中我的帖子请求可以工作,但即使我也在做同样的事情,但在浏览器中却不行?

[英]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.

相关问题 为什么相同的POST请求在POSTMAN中起作用,但在浏览器AJAX中却不起作用(找不到404)? - Why the same POST request works in POSTMAN but not in browser AJAX (404 Not Found)? 为什么即使同一请求在Postman中显示数据,CasperJS中的POST请求响应数据为何也为空 - Why is the POST request response data null in CasperJS even though same request shows data in Postman 为什么即使它适用于 Postman,它也不能成功发布我的数据? - Why won't it POST my data successfully even though it works on Postman? 简单的 POST 请求在 Postman 中有效,但在浏览器中无效 - Simple POST request works in Postman but not in browser 为什么即使在 Postman 中返回,我也无法从 POST 请求中接收正文? - Why can't I receive the body from a POST request even though it is returned in Postman? POST请求在POSTMAN中有效,但在我的代码JS中无效 - POST request works in POSTMAN but not in my code JS POST 请求正在使用 POSTMAN,但是当我尝试使用 Axios 从浏览器(UI)调用相同的链接时,它显示 404 错误 | node.js - POST request is working using POSTMAN, but when I'm trying to call the same link using Axios from browser(UI) it's showing 404 error | node.js 我的AJAX发布请求为何不起作用? - How come my AJAX post request is not working? 发布请求在 Postman 中有效,但在 Javascript 中无效 - Post Request Works in Postman But Not in Javascript 为什么在进行发布请求时我得到空 object - Why I'm getting empty object when doing post request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM