0

I am trying to send a csv file by using request module but I keep getting 500: Internal Server Error. I have tried using cURL and it worked fine.

Below is my python code:

import requests
filepath = 'ABC.csv'
with open('ABC.csv','rb') as input_file:
    response = requests.put('http://....', headers = {'Content-Disposition': 'attachment;filename=Cu_temp.csv', 'content-type':'application/csv'},files={'docfile': input_file})
response.raise_for_status()

The curl command that worked for me is;

curl -X PUT -H "Content-Disposition: attachment;filename=ABC.csv" -H "Content-Type:application/csv" -T ABC.csv http://...

Below is the error that I am getting;

HTTPError                                 Traceback (most recent call last)
<ipython-input-24-077dff9ba8c1> in <module>()
     13                 )
---> 14 response.raise_for_status()

C:\Anaconda2\lib\site-packages\requests\models.pyc in raise_for_status(self)
    838 
    839         if http_error_msg:
--> 840             raise HTTPError(http_error_msg, response=self)
    841 
    842     def close(self):

HTTPError: 500 Server Error: INTERNAL SERVER ERROR for url: http://.....

After editing my code from suggestions from @brunodesthuilliers in the comments, now I am getting 400 Client error: Here is my new code

import requests
filepath = 'ABC.csv'
url = 'http://...'
with open('ABC.csv','rb') as input_file:
    files= {'file': ('ABC.csv', 'application/csv','attachment;filename=ABC.csv')}
    response = requests.put(url,files=files)
response.raise_for_status()

Any idea, what is going wrong?

Muhammad
  • 305
  • 2
  • 6
  • 20
  • Can you add what error you're getting from the server – Mani Apr 18 '16 at 10:38
  • Why are you opening the file? I think you want to give the filename, but you are trying to send it as an attachment. So in your put request you are passing along a file handle. Is that right? – joel goldstick Apr 18 '16 at 11:20
  • Looks like `requests` expects the various headers fields to be set in the `files` dict, not as headers: http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file – bruno desthuilliers Apr 18 '16 at 11:57
  • @joelgoldstick: Yes, I want to send it as an attachment. – Muhammad Apr 18 '16 at 13:00
  • @brunodesthuilliers: you mean like `files = {'file': ('ABC.csv', open('ABC.csv', 'rb'), 'application/csv')} r = requests.post(url,files=files)` ? – Muhammad Apr 18 '16 at 13:11
  • @muhammadwaseem yeps something like this - except that your API seems to expect a PUT (not POST) and that you really should use the `with` block to make sure your file will be closed. Caveat: I don't know your API and have not tested the suggested solution so I can't tell if that will solve anything. – bruno desthuilliers Apr 18 '16 at 13:29
  • @muhammadwaseem as a side note : have you checked your 500 reponse's content ? It may contain some more informations... – bruno desthuilliers Apr 18 '16 at 13:30
  • @brunodesthuilliers: I have done that as you suggested but now it is giving me 400 Client error: Bad request for URL: ... Please see my edited question. – Muhammad Apr 18 '16 at 13:41
  • @muhammadwaseem please check (and eventually post) the response's *content* - as I said, it might contains more informations (specially in the case of 4XX). – bruno desthuilliers Apr 18 '16 at 13:47
  • @brunodesthuilliers: for 400 error: it says `{"detail":"FileUpload parse error - none of upload handlers can handle the stream"}` – Muhammad Apr 18 '16 at 13:52
  • @muhammadwaseem Are you posting (well "putting") on your own app or is it an external one ? The error message seems to come from Django Rest Framework, cf http://stackoverflow.com/questions/33721905/django-rest-framework-and-file-upload – bruno desthuilliers Apr 18 '16 at 14:48
  • If it's your own app and the PUT request works correctly when using cURL, you could inspect the `HttpRequest` you get from cURL and compare it with the one you get from `requests`... Else, you may want to check the API doc or contact the technical support. – bruno desthuilliers Apr 18 '16 at 14:50
  • @brunodesthuilliers: It is not app, it is a data warehouse, to which I am trying to send variable definition document. The doc, only gives instruction for cURL, which I have already posted. – Muhammad Apr 18 '16 at 15:22
  • @muhammadwaseem "It is not app, it is a data warehouse" => it IS an application (for the more general definition of "application" in IT and CS) nonetheless - what the app does with your data is totally irrelevant here. And you have not answered on my questions about the response's **content** in both cases (ie when you get either a 500 or 400 status code). And without more informations, it's a mostly useless guessing game. – bruno desthuilliers Apr 19 '16 at 11:04

0 Answers0