gpt-4o-mini-2024-07-18· OpenAI
WildChat-4.8M (AI2)
Hello! I'm here to help you. Feel free to ask me anything! 👋
- User
Assistant: Hello! I'm here to help you. Feel free to ask me anything! 👋 User: Heyy Assistant: Hey there! How's it going? What can I help you with today? 😊 User: Write a python code for request User: Write a python code for request Assistant:
- Assistant
Sure! If you want to make HTTP requests in Python, a popular library to use is
requests. Below is a simple example of how to use it to make a GET request and a POST request.First, if you haven't already, you'll need to install the
requestslibrary. You can do this using pip:pip install requestsExample of a GET Request
import requests # URL to make the request to url = 'https://jsonplaceholder.typicode.com/posts/1' # Making a GET request response = requests.get(url) # Checking if the request was successful if response.status_code == 200: # Print the JSON response print(response.json()) else: print(f'Error: {response.status_code}')Example of a POST Request
import requests # URL to make the request to url = 'https://jsonplaceholder.typicode.com/posts' # Data to send in the POST request data = { 'title': 'foo', 'body': 'bar', 'userId': 1 } # Making a POST request response = requests.post(url, json=data) # Checking if the request was successful if response.status_code == 201: # 201 Created print('Post created successfully:') print(response.json()) else: print(f'Error: {response.status_code}')