I have a HTTP server in Python which handles file upload requests from curl
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
if re.search('/api/v1/addphoto', self.path):
form_data=cgi.FieldStorage()
file_data=form_data['photofile'].value
# Next I will save this file
# fp =open('some/file','wb')
# fp.write(file_data)
# fp.close()
Now I have to use curl to send the request, and the command is
curl -i -F name=photofile -F filedata=@01.jpeg http://server_ip:port/api/v1/addphoto
But the server report error
File "./apiserver.py", line 21, in do_POST
file_data=form_data['photofile'].value
File "/usr/lib/python2.7/cgi.py", line 541, in __getitem__
raise KeyError, key
KeyError: 'photofile'
What is the problem here?
OK, I find the reason and how to do it from here.
The reason is "You can't embed the CGI code in the server code like that. The whole point of CGIHTTPServer (and CGI in general) is that it executes separate scripts in designated directories and returns the output. Because you had overridden the do_POST method, the code to parse the form data was never called." (Actually I print the form_data and it is empty - "None, None, []
").
And to fix it, use passed in parameters (details)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})