I am trying to use a web API in my Python program, but I can't figure out how.
Here is an example call using the command line in Bash/Unix:
wget --post-file="example.txt" "http://example.com/api" -O output_filename
How would I replicate this functionality with Python's urllib
module? I'm reading through the documentation for the module, but I'm not quite sure how to send a POST request to the website with a file attached. Any help or advice would be appreciated.
I found this answer to a question a few years ago that seems to be doing something along the lines of what I am trying to do, but I don't seems to be getting the desired result when I try to replicate it:
from urllib import request, parse
data = parse.urlencode('example.txt').encode()
req = request.Request('http://example.com/api', data=data)
resp = request.urlopen(req)