0

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The important thing to realize, is that -XGET is just a simple GET. And since GET is the default for curl, you can actually omit it altogether for the curl request. -X in curl is followed by the http verb to use, so in the elastic search doc -XPUT is just a PUT.
1

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()

Comments

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.