Rajat Srivastava
Rajat Srivastava

Reputation: 113

How to use curl -XGET in python

I have a curl command which i want to use in python version 2.7. curl -k -XGET '' -d '' But I am not finding any appropriate function in Request library or pycurl library. I can only find simple curl get command alternates in it. But i need to post a JSON object which is a query to elasticsearch. Any help? Thanks a lot!

Upvotes: 0

Views: 4818

Answers (2)

qvpham
qvpham

Reputation: 1938

for get request you can use library requests http://docs.python-requests.org/en/master/

import requests

response  = requests.get(url, data=your_data)

your_data is a dict of data in json format.

Upvotes: 2

venpa
venpa

Reputation: 4318

You can use urllib2 for posting json and getting the response back:

import json
import urllib2
data = json.dumps(['a', 'b', 'c'])
url = <define the url to be called>
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
resp = f.read()
f.close()

Upvotes: 1

Related Questions