2

I'm trying to upload an image file (png or jpg) to MediaWiki using python. I need to make a POST http request.

Here is my code

mypath = "/Volumes/Home/kate/Downloads/west_wiki/images"
for i in listdir(mypath):
if isfile(join(mypath,i)):
    with open(join(mypath,i), 'rb') as content_file:        
        content = content_file.read()

        #get edit token2
        params3 = '?format=json&action=query&meta=tokens&continue='
        r3 = requests.get(baseurl+'api.php'+params3, cookies=r2.cookies)
        edit_token = r3.json()['query']['tokens']['csrftoken']

        edit_cookie = r2.cookies.copy()
        edit_cookie.update(r3.cookies)

        headers = {'content-type': 'image/x-png'}
        payload = {'action': 'upload', 'filename':i, 'file': content, 'token': edit_token}
        r4 = requests.post(baseurl+'api.php',headers=headers,data=payload, cookies=edit_cookie)

What am I doing wrong? Maybe the request is incorrect?

Nemo
  • 2,441
  • 2
  • 29
  • 63
Kate Zz
  • 3,002
  • 5
  • 14
  • 17
  • 1
    According to [the docs](https://www.mediawiki.org/wiki/API:Upload) it looks like you need to specify 'content-type' as 'multipart/form-data'. [This question](http://stackoverflow.com/q/12385179/4014959) may be helpful. – PM 2Ring Aug 26 '15 at 14:51

1 Answers1

0

Please use an appropriate Python client.

The most commonly used one is upload.py.

Nemo
  • 2,441
  • 2
  • 29
  • 63