python requests post text/plain

Python Requests Post text/plain

If you need to send data in a plain text format to a server using Python, you can use the requests library. The requests library is a popular Python library for making HTTP requests. In this guide, we will show you how to use the requests.post() method to send data in a plain text format.

Step 1: Install requests Library

If you haven't installed the requests library, you can install it using pip:

pip install requests

Step 2: Import requests Library

After installing requests library, you need to import it in your Python script:

import requests

Step 3: Send POST Request with Plain Text Data

You can send a POST request with plain text data using the requests.post() method:

url = 'https://example.com/api'
data = 'Hello, World!'
response = requests.post(url, data=data, headers={'Content-Type': 'text/plain'})

print(response.status_code)
print(response.text)
  • url: The URL of the API endpoint where you want to send the POST request.
  • data: The plain text data that you want to send with the request.
  • headers: The headers of the request. In this case, we set the Content-Type header to text/plain.

The response.status_code variable contains the HTTP status code of the response, and the response.text variable contains the content of the response.

Conclusion

In this guide, we showed you how to send plain text data using the requests.post() method in Python. By using this method, you can easily send plain text data to an API endpoint and get the response back in your Python script.