0

I'm trying to get the json response from a curl post into a json object so I can get to the key/value pair inside the json response.

My attempt so far looks something like this:

import pycurl
import json
c = pycurl.Curl()
#c.setopt(c.VERBOSE, True)
c.setopt(c.URL, 'https://some.place/get/token')
c.setopt(c.FOLLOWLOCATION, True)
post_data = {'id': '1q2w3e4r5t',
         'secret': 'abcdefghij'}
# Form data must be provided already urlencoded.
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
c.setopt(c.POSTFIELDS, postfields)
c.perform()
c.close()

This pumps out the json to the terminal screen, but any attempts so far to "get" the actual json returned by the curl command fails. Ideally, I do NOT want to write the json repsonse into a file first, and I'm also not sure if I would be able to "listen" to stdin - as soon as I get this right, it will probably be transformed into a AWS lambda script...

I'm omitting some functions from my actual code - like the encoding etc.

1 Answer 1

1

You have to create a buffer and configure your Curl instance to write to it. Then you can read it and deserialize it.

import io

buffer = io.BytesIO()
# ... Initialize your Curl() instance as you did
c.setopt(c.WRITEDATA, buffer)
c.perform()

data = json.loads(buffer.getvalue())
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.