Eärendil Baggins
Eärendil Baggins

Reputation: 572

How do I add a parameter to a POST request's body with urllib?

I need to send a POST request with this format using urllib:

POST /oauth/access_token HTTP/1.1
User-Agent: themattharris' HTTP Client
Host: api.twitter.com
Accept: */*
Authorization: OAuth oauth_consumer_key="cChZNFj6T5R0TigYB9yd1w",
                     oauth_nonce="a9900fe68e2573b27a37f10fbad6a755",
                     oauth_signature="39cipBtIOHEEnybAR4sATQTpl2I%3D",
                     oauth_signature_method="HMAC-SHA1",
                     oauth_timestamp="1318467427",
                     oauth_token="NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0",
                     oauth_version="1.0"
Content-Length: 57
Content-Type: application/x-www-form-urlencoded

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY

While for the header I add it this way:

AccessRequest = urllib.request.Request('https://api.twitter.com/oauth/access_token')
AccessRequest.add_header('Authorization', oauth_header)

What is the correct way to add the parameter which should stay in the body?

Right now I'm doing:

values = {
    'oauth_verifier': verifier
}
data = urllib.parse.urlencode(values).encode('ascii')
resp = urllib.request.urlopen(AccessRequest, data=data)

but there must be something wrong since the server returns a HTTP Error 401: Authorization Required. What am I doing wrong?

Upvotes: 3

Views: 9622

Answers (1)

Damotorie
Damotorie

Reputation: 596

from urllib import request, parse
data = parse.urlencode({"data":"Hello Word?"}).encode()
req =  request.Request("http://www.naver.com", data=data) # this will make the method "POST"
resp = request.urlopen(req)

If run this code, packet looks like this.

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 18
User-Agent: Python-urllib/3.4

data=Hello+Word%3F

If I change data = parse.urlencode({"data":"Hello Word?"}).encode() to data = parse.urlencode({"oauth_verifier":"uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY"}).encode()

Packet will look like this

POST / HTTP/1.1
Accept-Encoding: identity
Host: www.naver.com
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 57
User-Agent: Python-urllib/3.4

oauth_verifier=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY

Isn't this what you want?

Upvotes: 6

Related Questions