Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

I have a RESTful API that I have uncovered utilizing an execution of Elasticsearch on an EC2 occasion to list a corpus of substance. I can question the search by executing the accompanying from my terminal (MacOSX): 

curl -XGET 'http://ES_search_demo.com/document/record/_search?pretty=true' -d '{

  "query": {

    "bool": {

      "must": [

        {

          "text": {

            "record.document": "SOME_JOURNAL"

          }

        },

        {

          "text": {

            "record.articleTitle": "farmers"

          }

        }

      ],

      "must_not": [],

      "should": []

    }

  },

  "from": 0,

  "size": 50,

  "sort": [],

  "facets": {}

}'

How would I transform the above into an API request utilizing python/requests or python/urllib2 (not certain which one to go for - have been utilizing urllib2, yet hear that solicitations is better...)? Do I pass as a header or something else?

1 Answer

0 votes
by (26.4k points)

With the help of requests:

import requests

url = 'http://ES_search_demo.com/document/record/_search?pretty=true'

data = '''{

  "query": {

    "bool": {

      "must": [

        {

          "text": {

            "record.document": "SOME_JOURNAL"

          }

        },

        {

          "text": {

            "record.articleTitle": "farmers"

          }

        }

      ],

      "must_not": [],

      "should": []

    }

  },

  "from": 0,

  "size": 50,

  "sort": [],

  "facets": {}

}'''

response = requests.post(url, data=data)

Contingent upon what sort of reaction your API returns, you will at that point likely need to take a gander at response.text or response.json() (or perhaps assess response.status_code first). See the quickstart docs here, particularly this segment

Join the python online course fast, to learn python concepts in detail and get certified.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...