I have a working curl
command as follows :
curl -X POST \
http://localhost:7777/upload \
-H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
-F file_name=temp.py \
-F file_content=@/Users/blah/requirements.txt \
-F d_name=blah \
-F owner=blah \
-F name=blah \
-F local_git_path=blah \
-F local_git_hash=blah
Ofcourse I fiddled around with POSTMAN - and got my POST to work from my computer to my test service. The issue I am facing is when I am writing a client
for my server. A sample conversion for the above I have is as follows :
def upload(self, file_path):
api_url = os.path.join(Config().get_url(), "upload")
payload = "------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"file_name\"\r\n\r\n{filename}\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"file_content\"; filename=\"{file_content}\"\r\nContent-Type: text/plain\r\n\r\n\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"d_name\"\r\n\r\n{dname}\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"owner\"\r\n\r\n{owner}\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"name\"\r\n\r\n{name}\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"local_git_path\"\r\n\r\nblah\r\n" \
"------WebKitFormBoundaryTESTING\r\nContent-Disposition: form-data; " \
"name=\"local_git_hash\"\r\n\r\nblah\r\n" \
"------WebKitFormBoundaryAAFDAGSTORE--".format(filename="temp.py", file_content=file_path, dname="blah", owner="blah",
name="blah")
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundaryTESTING",
'cache-control': "no-cache"
}
response = requests.post(url=api_url, data=payload, headers=headers)
return json.loads(response.text)
This really has started to annoy me, because the POST works fine - but my file content received on the server side is an EMPTY file. This happens only in the python side.
But doen'tt happen with cURL
. I am pretty comfortable with request
module. I prefer not changing it.
Also the webserver is a flask
application - seems to be working fine because my cURL
works.
Update#1
Also tried the following, this was my first approach before I changed to webkits. This really tripped me badly - I was getting a 404 from my webserver. This was strange because my cURL
was still working indicating - my webserver was doing its job right.
def upload(self, file_path):
headers = {
'Content-Type': "application/json",
'content-type': "multipart/form-data"
}
api_url = os.path.join(Config().get_url(), "upload")
files_dict = dict(file_content=open(file_path, "rb"),
file_name=(None, "temp.py"),
dag_name=(None, "dag_name"),
owner=(None, "blah"),
name=(None, "blah"),
local_git_path=(None, "blah"),
local_git_hash=(None, "blah"))
response = requests.post(url=api_url, files=files_dict, headers=headers)
return json.loads(response.text)