gpt-3.5-turbo-0613· OpenAI
Arena Human Preference 55K
generate python code to invoke a REST API
- User
generate python code to invoke a REST API
- Assistant
Sure! Here's an example of Python code to invoke a REST API using the
requestslibrary:import requests # Set the API endpoint URL url = "https://api.example.com/endpoint" # Set the request headers (if required) headers = { "Content-Type": "application/json", "Authorization": "Bearer YOUR_ACCESS_TOKEN" } # Set the request payload (if required) payload = { "param1": "value1", "param2": "value2" } # Send a GET request response = requests.get(url, headers=headers, params=payload) # Send a POST request # response = requests.post(url, headers=headers, json=payload) # Send a PUT request # response = requests.put(url, headers=headers, json=payload) # Send a DELETE request # response = requests.delete(url, headers=headers) # Check the response status code if response.status_code == 200: # Successful request print("Request successful!") print(response.json()) else: # Failed request print("Request failed!") print("Status code:", response.status_code) print("Response:", response.text)You can uncomment the appropriate request method (
GET,POST,PUT, orDELETE) based on the API you want to invoke. Remember to replace theurl,headers, andpayloadwith your own values.