Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Get A Web Page Via A Post Request In Python

This is an example of how to use the Python `requests` module to send a POST request to a web server that includes headers and a json payload. (The specific example is for a meilisearch server in this case.)

NOTE: I'm pretty sure you need to install `requests` with `pip` or `pip3`. For example:

Code

pip install requests

Note: the results have been locked in so the bearer token can be removed. The code has been set to :eval never to prevent accidentally running it.

#+NAME: listing-1 #+begin_src python :post results_padder(data=*this*) :results output :wrap example :eval never

import requests

url = 'http://127.0.0.1:7700/indexes/grimoire/search'

headers = { 'Authorization': 'Bearer aaaaabbbbbccccccddddddd', 'Content-Type': 'application/json' }

data = { 'q': 'example' }

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

print(f'Status: {response.status_code}') print(f'Returned: {response.json()}')

#+end_src

Results

Status: 200
  
  Returned:
  {
    "hits": [
      {
        "id": "100",
        "filename": "Example State Names",
      }
    ]
  }

TODO: Link up other version of getting pages via GET