Laravel + Python请求不起作用,但是在Postman中它可以工作(Laravel + Python request not working but in Postman it works

编程入门 行业动态 更新时间:2024-10-27 02:28:19
Laravel + Python请求不起作用,但是在Postman中它可以工作(Laravel + Python request not working but in Postman it works)

我试图从我的树莓派发送一些python脚本的数据,我已经有过类似的问题,有些回答我不是我在找什么,但它的作品,你可以在这里检查它的东西是我试图做一个base64编码发送它,然后解码并保存它,现在我想尝试一种不同的方式我试图发送一些数据在multipart/form-data问题是,在我的python脚本,当我试图发送视频似乎没有发送任何视频,但是当我从邮差发送它完美的作品,我试图看看邮差上的代码是什么。

这是邮递员代码发送给我的PHP服务器

import requests url = "http://192.168.1.208:8080/api/createvideo" payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"20180215-033512.mp4\"\r\nContent-Type: video/mp4\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ip_address\"\r\n\r\n10.10.10.110\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"date\"\r\n\r\n2018-02-14 18:27:20\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" headers = { 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 'Accept': "application/json", 'Content-Type': "application/x-www-form-urlencoded", 'Cache-Control': "no-cache", 'Postman-Token': "50de5d50-e0f9-d2ab-6b37-aee8aa127bb3" } response = requests.request("POST", url, data=payload, headers=headers) print(response.text)

我正在做的邮差形象: 在imgur.com上查看帖子

这里是我的python代码,这里是我建立请求的地方:

file = {'files': open(filename,"rb"), 'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()} r = self.api.postvideo(file)

发布请求服务代码:

def postvideo(self, file): return requests.post(self.url+'createdevice',files=file)

为什么在邮差里它会很好,我想知道我错过了什么,因为当它到达我的Laravel服务器时,它显示为空,这是我在Laravel中做的事情

if ($request->hasFile('files')) { $device = Device::where('ip_address',request('ip_address'))->first(); $path = public_path().'/video/'.$device->id.'/'; $file = str_random(8).'.mp4'; $file_name = $path.$file; $request->file('files')->move($path,$file); $video = Video::create([ 'date' => request('date'), 'device_id' => $device->id, 'video' => $file_name ]); return response()->json([ 'data' => $video]); } return response()->json([ 'data' => 'No Video Attach'],404);

PS我已经试过了

I'm trying to send some data with a python script from my raspberry pi, i already had a similar question and some answer me not what i was looking for but it kind of works you can check it here the thing is i was trying to make a base64 encode send it and then decode it and save it, now i'm trying a different way I'm trying to send some data in a multipart/form-data the problem is that in my python script when i'm trying to send the video it seems that it's not sending any video, but when i send it from Postman works perfectly, i tried to see what was the code on Postman.

Here is the postman code to send it to my php server

import requests url = "http://192.168.1.208:8080/api/createvideo" payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"files\"; filename=\"20180215-033512.mp4\"\r\nContent-Type: video/mp4\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"ip_address\"\r\n\r\n10.10.10.110\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"date\"\r\n\r\n2018-02-14 18:27:20\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--" headers = { 'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 'Accept': "application/json", 'Content-Type': "application/x-www-form-urlencoded", 'Cache-Control': "no-cache", 'Postman-Token': "50de5d50-e0f9-d2ab-6b37-aee8aa127bb3" } response = requests.request("POST", url, data=payload, headers=headers) print(response.text)

Postman image of what i'm doing: View post on imgur.com

And here is my python code here is where i'm building the request:

file = {'files': open(filename,"rb"), 'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()} r = self.api.postvideo(file)

Post request service code:

def postvideo(self, file): return requests.post(self.url+'createdevice',files=file)

why would it be fine in Postman, i'm wondering what I'm missing because when it arrives in my Laravel Server it shows empty, here is what I'm doing in Laravel

if ($request->hasFile('files')) { $device = Device::where('ip_address',request('ip_address'))->first(); $path = public_path().'/video/'.$device->id.'/'; $file = str_random(8).'.mp4'; $file_name = $path.$file; $request->file('files')->move($path,$file); $video = Video::create([ 'date' => request('date'), 'device_id' => $device->id, 'video' => $file_name ]); return response()->json([ 'data' => $video]); } return response()->json([ 'data' => 'No Video Attach'],404);

p.s I already tried this

最满意答案

所以我犯了一些错误,我没有真正做出相同的结果,但它的工作原理,首先我改变了这一点:

file = {'files': open(filename,"rb"), 'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()}

因为你可以看到我分割数据和文件:

file = {'files': ('video.mp4',open(filename,"rb"))} data = {'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()}

我真的不知道发生了什么,但是我做了一个时间。在所有这些之前time.sleep ,因为我使用的是一个子进程,显然该视频无法在读取模式下使用,所以对我也有帮助。

还有一件事就是这样发布帖子请求:

return requests.post(self.url+'createvideo',files=file,data=data)

我这样做是为了当它到达Laravel时,我可以像这样正确使用它们:

这读取文件并重新定位它

$request->file('files')->move($path,$file);

这可以获取任何数据:

$request->input('ip_address')

So i did a few mistakes i didn't really make the same result but it works, first i changed this:

file = {'files': open(filename,"rb"), 'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()}

for this as you can see i split the data and the file:

file = {'files': ('video.mp4',open(filename,"rb"))} data = {'date': file = {'file': open(filename,'rb'), 'ip_address': self.api.get_ip()}

I don't really know what happened but i did a time.sleep too before all this because i was using a subprocess and apparently the video wasn't able used it on read mode so that helped me too.

There other thing was to make the post request like this:

return requests.post(self.url+'createvideo',files=file,data=data)

I did this so that when it arrives in Laravel i could used them properly like this:

this to read the file and relocate it

$request->file('files')->move($path,$file);

And this to fetch any data:

$request->input('ip_address')

更多推荐

本文发布于:2023-07-22 12:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:它可以   不起作用   但是在   工作   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!