How can I make a Post Request on Python with urllib3?
Asked Answered
H

3

17

I've been trying to make a request to an API, I have to pass the following body:

{
"description":"Tenaris",
"ticker":"TS.BA",
"industry":"Metalúrgica",
"currency":"ARS"
}

Altough the code seems to be right and it finished with "Process finished with exit code 0", it's not working well. I have no idea of what I'm missing but this is my code:

http = urllib3.PoolManager()
http.urlopen('POST', 'http://localhost:8080/assets', headers={'Content-Type':'application/json'},
                 data={
"description":"Tenaris",
"ticker":"TS.BA",
"industry":"Metalúrgica",
"currency":"ARS"
})

By the way, this the first day working with Python so excuse me if I'm not specific enough.

Haldis answered 3/8, 2015 at 2:55 Comment(2)
Try settings the return value of http.urlopen() to a variable and printing it.Mezzorelievo
Also, are you running this in an interactive interpreter? or a script?Mezzorelievo
S
45

Since you're trying to pass in a JSON request, you'll need to encode the body as JSON and pass it in with the body field.

For your example, you want to do something like:

import json
encoded_body = json.dumps({
        "description": "Tenaris",
        "ticker": "TS.BA",
        "industry": "Metalúrgica",
        "currency": "ARS",
    })

http = urllib3.PoolManager()

r = http.request('POST', 'http://localhost:8080/assets',
                 headers={'Content-Type': 'application/json'},
                 body=encoded_body)

print r.read() # Do something with the response?

Edit: My original answer was wrong. Updated it to encode the JSON. Also, related question: How do I pass raw POST data into urllib3?

Supposititious answered 3/8, 2015 at 8:13 Comment(1)
It doesn't appear that fields will encode the body as JSON which is what I think OP wants.Maleficence
M
3

I ran into this issue when making a call to Gitlab CI. Since the above did not work for me (gave me some kind of error about not being able to concatenate bytes to a string), and because the arguments I was attempting to pass were nested, I thought I would post what ended up working for me:

API_ENDPOINT = "https://gitlab.com/api/v4/projects/{}/pipeline".format(GITLAB_PROJECT_ID)

API_TOKEN = "SomeToken"

data = {
    "ref": ref,
    "variables": [
        {
            "key": "ENVIRONMENT",
            "value": some_env
        },
        {   "key": "S3BUCKET",
            "value": some_bucket
        },
    ]
}

req_headers = {
    'Content-Type': 'application/json',
    'PRIVATE-TOKEN': API_TOKEN,
}

http = urllib3.PoolManager()

encoded_data = json.dumps(data).encode('utf-8')

r = http.request('POST', API_ENDPOINT,
             headers=req_headers,
             body=encoded_data)

resp_body = r.data.decode('utf-8')
resp_dict = json.loads(r.data.decode('utf-8'))

logger.info('Response Code: {}'.format(r.status))
logger.info('Response Body: {}'.format(resp_body))

if 'message' in resp_body:
    logfile_msg = 'Failed Gitlab Response-- {} {message}'.format(r.status, **resp_dict)
Mazzola answered 23/11, 2020 at 22:51 Comment(1)
The reason that the top-voted answer doesn't work for you (and me) is because we need to manually encode query parameters in the URL. You can see my answer if you're still interested.Unclasp
U
-4

I recently became interested in using urllib3, and came across this problem. If you read the urllib3 "User Guide" page, you will see this:

For POST and PUT requests, you need to manually encode query parameters in the URL

Your code should be adjusted to look like this:

import urllib3
from urllib.parse import urlencode

data = {"description":"Tenaris",
        "ticker":"TS.BA",
        "industry":"Metalúrgica",
        "currency":"ARS"}

http = urllib3.PoolManager()
encoded_data = urlencode(data)
http.request('POST',
             'http://localhost:8080/assets?'+encoded_data,
             headers={'Content-Type':'application/json'})    
Unclasp answered 20/4, 2021 at 19:8 Comment(1)
the question is about body, not query parameters. This way you add request query string which is not the way how you pass JSON bodyGerard

© 2022 - 2024 — McMap. All rights reserved.