Thursday 18 July 2013

Implementation of PUT and DELETE in pycurl

Implementation of PUT and DELETE in pycurl


Through this post explaining about the usage of PUT and DELETE in pycurl.

import pycurl
import json

"""
Here is the sample example of curl which is using PUT and DELETE
curl -XPUT localhost:9200/tweets/tweet/1 -d '
{
user: "Test User",
message: "Test Message",
postDate: "20130201T02:00:00",
"mappings" : {
    "tweet" : {
        "_source" : {"enabled" : false}
    }
}
}
'

curl -XDELETE 'http://localhost:9200/twitter/tweet/1'
"""
page_index = {
user: "Test User",
message: "Test Message",
postDate: "20130201T02:00:00",
"mappings" : {
    "tweet" : {
        "_source" : {"enabled" : false}
    }
}
}

c = pycurl.Curl()
# Remove Index if alreday exists
c.setopt(pycurl.URL, "http://localhost:9200/twitter")
c.setopt(pycurl.CUSTOMREQUEST, "DELETE")
c.perform()
 
# Create new index
c.setopt(pycurl.URL, "http://localhost:9200/twitter")
c.setopt(pycurl.CUSTOMREQUEST, "PUT")
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.POSTFIELDS, '%s' % json.dumps(page_index ))
c.perform()

2 comments:

  1. I don't see any data being PUT, am I missing something?

    ReplyDelete
  2. No you are not missing anything, fixed code for PUT. Create a dictionary and convert into json. After that its possible to insert data for a particular URL.

    ReplyDelete