Unable to upload using Python requests

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Unable to upload using Python requests

David Jensen
Hi all!

May I request that some python sample code be posted uploading a local  
file, ideally using the requests module (which seems to be relatively  
canonical)? I don't think I'm the first to be stymied by mw:API:Upload  
(cf. Boxsnake's 18-month old unanswered question at mw:API_talk:Upload  
; also  
http://stackoverflow.com/questions/33839706/simplest-way-to-upload-a-file-to-an-up-to-date-mediawiki-instance-with-python where the answer was essentially "give up and use a bot  
framework").

Essentially, I cannot seem to avoid getting a "badupload_file"/"File  
upload param file is not a file upload; be sure to use  
multipart/form-data for your POST and include a filename in the  
Content-Disposition header."

I am under the impression I am doing both of those things, my headers are:

{'Content-Type': 'multipart/form-data',
'Content-Disposition': 'form-data; name="file";  
filename="storkk_test_upload1.jpg"',
'User-Agent': 'Drudgebane Storkksbot (contact [[C:User:Storkk]]) Based  
on python requests/2.11.1',
'Connection': 'keep-alive',
'Content-Length': '93990',
'Cookie': 'forceHTTPS=true; commonswiki_BPsession=...(elided)...',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*'}

The full code (with a slightly different filename test), trying a few  
different things, is at http://pastebin.com/PxLk85mR

I would greatly appreciate some pointers on where I am going wrong.

Thanks for your time,

storkk


_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unable to upload using Python requests

Bartosz Dziewoński
You should probably just pass the file to the requests library and have
it do all the work, like setting up multipart/form-data and
Content-Disposition. I don't often write Python, but this looks like the
right approach: http://stackoverflow.com/a/22567429

--
Bartosz Dziewoński

_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Unable to upload using Python requests

David Jensen
Thanks Bartosz...
Trying that,

   files = {'file': open(LOCAL_FILENAME,'rb')}
   values = {'action': 'upload', 'format':'json','filename':fn,  
'comment':'test upload',
             'text':'{{OGL2}}\n\nPlease contact [[User:Storkk]] if  
there are issues','token':csrftoken}
   r = requests.post(baseurl, files=files, data=values)

Doesn't even return JSON, but the HTML API help.
Likewise with

   with open(LOCAL_FILENAME,'rb') as f:
       CONTENTS = f.read()
   files = {'file': CONTENTS}

same thing.

storkk

Quoting Bartosz Dziewoński <[hidden email]>:

> You should probably just pass the file to the requests library and  
> have it do all the work, like setting up multipart/form-data and  
> Content-Disposition. I don't often write Python, but this looks like  
> the right approach: http://stackoverflow.com/a/22567429
>
> --
> Bartosz Dziewoński
>
> _______________________________________________
> Mediawiki-api mailing list
> [hidden email]
> https://lists.wikimedia.org/mailman/listinfo/mediawiki-api




_______________________________________________
Mediawiki-api mailing list
[hidden email]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-api
Loading...