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?