Reputation: 359
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
Upvotes: 1
Views: 913
Reputation: 359
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')
Upvotes: 1