0

I have to post a file along with some data to to api. Here is the python code i wrote for testing:

fl={'payload' : open('C:/data/log2.txt')}
params = {
        'topic':'pos',
        'store':storeID,
         }

r = requests.post(url,files=fl,data=params)

print r.status
print r.text

But i always get a message saying, "file is not in correct format"

I tested the api with POSTMAN (chrome extension to test rest API) and it seems to work fine with postman i get a success response and the file is sent, here is a snapshot.

api test using postman

6
  • Can you post the stack trace you are getting? Commented Dec 8, 2013 at 0:25
  • I don't get a error, i just get a response from the server saying that "file is not in correct format". Commented Dec 8, 2013 at 7:18
  • That sounds like maybe your log file isn't formatted correctly... your code is probably good. In your question you reference two different files, amazonlog.log and log2.txt. Make sure that the file in your code is sent with the same name and has the same contents as amazonlog.log. Maybe you need a .log extension...? Commented Dec 8, 2013 at 7:20
  • the server accepts any text file, i tested it with log2.txt using postman and server accepts it. I can't check it right now but i will check it as soon as i get back to work. Commented Dec 8, 2013 at 7:58
  • I tried with same file names and content. While it was working with postman it did not work with python requests. I suspect that there is a bug related to encoding in python requests module. For the same file that was uploaded using postman, i get a response in python as: {"status": "Error", "message": "{'payload': [u'File type is not supported']}"} Commented Dec 8, 2013 at 13:31

1 Answer 1

2

From the docs

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('report.xls', 'rb')}

>>> r = requests.post(url, files=files)
>>> r.text
{
  ...
  "files": {
    "file": "<censored...binary...data>"
  },
  ...
}

Try adding 'rb' to your open statement so that you are uploading binary data.

Change

fl={'payload' : open('C:/data/log2.txt')}

to

fl={'payload' : open('C:/data/log2.txt', 'rb')}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.