cornsnake.util_network
Making a POST request using the urllib library.
1""" 2Making a POST request using the urllib library. 3 4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_network.html) 5""" 6 7import urllib.request 8 9from . import util_log 10 11logger = util_log.getLogger(__name__) 12 13 14def post_request(api_url, headers, timeout): 15 """ 16 Function to make a POST request to a specified API URL. 17 18 Args: 19 api_url (str): The URL to which the POST request will be made. 20 headers (dict): The headers to be included in the request. 21 timeout (int): The timeout for the request in seconds. 22 23 Returns: 24 bool: True if the POST request is successful (status code 200), False otherwise. 25 """ 26 req = urllib.request.Request( 27 url=api_url, 28 data=None, 29 headers=headers, 30 origin_req_host=None, 31 unverifiable=False, 32 method="POST", 33 ) 34 with urllib.request.urlopen(req, timeout=timeout) as response: 35 if response.status == 200: 36 return True 37 print(f"POST failed [{response.status}]") 38 return False
logger =
<Logger cornsnake.util_network (INFO)>
def
post_request(api_url, headers, timeout):
15def post_request(api_url, headers, timeout): 16 """ 17 Function to make a POST request to a specified API URL. 18 19 Args: 20 api_url (str): The URL to which the POST request will be made. 21 headers (dict): The headers to be included in the request. 22 timeout (int): The timeout for the request in seconds. 23 24 Returns: 25 bool: True if the POST request is successful (status code 200), False otherwise. 26 """ 27 req = urllib.request.Request( 28 url=api_url, 29 data=None, 30 headers=headers, 31 origin_req_host=None, 32 unverifiable=False, 33 method="POST", 34 ) 35 with urllib.request.urlopen(req, timeout=timeout) as response: 36 if response.status == 200: 37 return True 38 print(f"POST failed [{response.status}]") 39 return False
Function to make a POST request to a specified API URL.
Args: api_url (str): The URL to which the POST request will be made. headers (dict): The headers to be included in the request. timeout (int): The timeout for the request in seconds.
Returns: bool: True if the POST request is successful (status code 200), False otherwise.