How to use 'python requests post' in Python

Every line of 'python requests post' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
77def post(self, data):
78 if len(self.sockets) > 0 and self.try_count > 0:
79 current_socket = self.sockets.pop(0)
80 proxies = {
81 'http': 'http://' + current_socket,
82 'https': 'https://' + current_socket
83 }
84 try:
85 request = requests.post(
86 self.url, json=data, timeout=3.0, proxies=proxies)
87 self.set_request_data(request, current_socket)
88 except Exception:
89 self.try_count -= 1
90 self.post(data)
91 else:
92 self.try_count_succeeded()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

132def _do_post(post_url, api_name, request_body):
133 """Helper to do HTTP POST.
134
135 Note:
136 1) On success, Kubernetes returns a success code of 201(CREATED) not 200(OK)
137 2) A response code of 509(CONFLICT) is interpreted as a success code (since
138 the error is most likely due to the resource already existing). This makes
139 _do_post() idempotent which is semantically desirable.
140 """
141 is_success = True
142 try:
143 r = requests.post(post_url,
144 data=request_body,
145 timeout=_REQUEST_TIMEOUT_SECS)
146 if r.status_code == requests.codes.conflict:
147 print('WARN: Looks like the resource already exists. Api: %s, url: %s' %
148 (api_name, post_url))
149 elif r.status_code != requests.codes.created:
150 print('ERROR: %s API returned error. HTTP response: (%d) %s' %
151 (api_name, r.status_code, r.text))
152 is_success = False
153 except (requests.exceptions.Timeout,
154 requests.exceptions.ConnectionError) as e:
155 is_success = False
156 _print_connection_error(str(e))
157 return is_success

Related snippets