❮ Requests Module Nội dung chính - Definition and Usage
- Parameter Values
- Return Value
- installation
- What's GET request
- Sending GET requests in Python using requests
- What's POST request
- Sending POST requests in Python using requests
- What's the PUT request
- Sending PUT requests in Python using requests
- What's a delete request
- Sending a DELETE request in python
- What is get and post method in Python?
- How do I get an API response in Python?
- What is HTTP method in Python?
- How do you request a post in Python?
ExampleMake a DELETE request to a web page, and return the response text: import requests x = requests.delete('https://w3schools.com/python/demopage.php') print(x.text) Run Example »
Definition and UsageThe delete() method sends a DELETE
request to the specified url. DELETE requests are made for deleting the specified resource (file, record etc).
Syntax requests.delete(url, args) args means zero or more of the named arguments in the parameter table below. Example: requests.delete(url, timeout=2.50)
Parameter Values
Parameter | | Description |
---|
url
| Try it
| Required. The url of the request
| allow_redirects
| Try it
| Optional. A Boolean to enable/disable redirection. Default True (allowing redirects)
| auth
| Try it
| Optional. A tuple to enable a certain HTTP authentication. Default None
| cert
| Try it
| Optional. A String or Tuple specifying a cert file or key. Default None
| cookies
| Try it
| Optional. A dictionary of cookies to send to the specified url. Default None
| headers
| Try it
| Optional. A dictionary of HTTP headers to send to the specified url. Default None
| proxies
| Try it
| Optional. A dictionary of the protocol to the proxy url. Default None
| stream
| Try it
| Optional. A Boolean indication if the response should be immediately downloaded (False) or streamed (True). Default False
| timeout
| Try it
| Optional. A number, or a tuple, indicating how many seconds to wait for the client to make a connection and/or send a response. Default None which means the request will continue until the connection is closed
| verify
| Try it Try it
| Optional. A Boolean or a String indication to verify the servers TLS certificate or not. Default True
|
Return ValueThe delete() method returns a requests.Response object.
❮ Requests Module
Nội dung chính - installation
- What's GET request
- Sending GET requests in Python using requests
- What's POST request
- Sending POST requests in Python using requests
- What's the PUT request
- Sending PUT requests in Python using requests
- What's a delete
request
- Sending a DELETE request in python
- What is get and post method in Python?
- How do I get an API response in Python?
- What is HTTP method in Python?
- How do you request a post in Python?
Hello, I'm Aya Bouchiha, today, we'll talk about sending requests in python using the requests module installation pip install requests
Enter fullscreen mode Exit fullscreen mode What's GET request
GET: is a request used for getting or retrieving data or information from a specified server. Sending GET requests in Python using requests import requests
url = 'https://jsonplaceholder.typicode.com/posts/1'
response = requests.get(url)
# <Response [200]>
print(response)
# 200
print(response.status_code)
# {
# "userId": 1,
# "id": 1,
# "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
# "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
#}
print(response.text)
# https://jsonplaceholder.typicode.com/posts/1
print(response.url)
# application\json; charset=utf-8
print(response.headers['Content-Type'])
# b'{\n "userId": 1,\n "id": 1,\n "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",\n "body": "quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto"\n}'
print(response.content)
# {'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'}
print(response.json())
# <class 'dict'>
print(type(response.json()))
# for more information
# print(dir(response))
Enter fullscreen mode Exit fullscreen mode What's POST request POST: is a request that is used for sending information or data to a specific server. Sending POST requests in Python using requests import requests
import json
url = 'https://jsonplaceholder.typicode.com/posts'
data = { 'title': 'buy new mouse','body': 'I need to buy a new mouse !','userId': 5,}
headers = {'content-type': 'application/json; charset=UTF-8'}
response = requests.post(url, data=json.dumps(data), headers=headers)
# 201
print(response.status_code)
# True
print(response.ok)
# b'{\n "title": "buy new mouse",\n "body": "I need to buy a new mouse !",\n "userId": 5,\n "id": 101\n}'
print(response.content)
# {
# "title": "buy new mouse",
# "body": "I need to buy a new mouse !",
# "userId": 5,
# "id": 101
# }
print(response.text)
# <class 'str'>
print(type(response.text))
# https://jsonplaceholder.typicode.com/posts
print(response.url)
# application/json; charset=utf-8
print(response.headers['Content-Type'])
# utf-8
print(response.encoding)
Enter fullscreen mode Exit fullscreen mode What's the PUT request PUT: is a request used for creating or updating a resource in a specific server. Sending PUT requests in Python using requests import requests
import json
url = 'https://jsonplaceholder.typicode.com/posts/1'
data = {'id':1, 'userId':2, 'title':'drink water', 'body':'drinking water is important'}
headers = {'Content-Type':'application/json; charset=UTF-8'}
response = requests.put(url, data=json.dumps(data), headers=headers)
# 200
print(response.status_code)
# True
print(response.ok)
# b'{\n "id": 1,\n "userId": 2,\n "title": "drink water",\n "body": "drinking water is important"\n}'
print(response.content)
# {
# "id": 1,
# "userId": 2,
# "title": "drink water",
# "body": "drinking water is important"
# }
print(response.text)
# <class 'str'>
print(type(response.text))
# https://jsonplaceholder.typicode.com/posts
print(response.url)
# application/json; charset=utf-8
print(response.headers['Content-Type'])
# utf-8
print(response.encoding)
Enter fullscreen mode Exit fullscreen mode What's a delete request DELETE: is a request used to delete a specific resource in a server. Sending a DELETE request in python import requests
import json
url = 'https://jsonplaceholder.typicode.com/posts/1'
headers = {'Content-Type': 'application/json; charset=UTF-8'}
response = requests.delete(url, headers=headers)
print(response.status_code) # 200
print(response.ok) # True
print(type(response.text)) # <class 'str'>
print(response.url) # https://jsonplaceholder.typicode.com/posts/1
print(response.headers['Content-Type']) # application/json; charset=utf-8
print(response.encoding) # utf-8
Enter fullscreen mode Exit fullscreen mode Have a good day What is get and post method in Python?GET : to request data from the server. POST : to submit data to
be processed to the server. How do I get an API response in Python?Make your API call. def get_data(self, api):. response = requests.get(f"{api}"). if response.status_code == 200:. print("sucessfully fetched the data"). self.formatted_print(response.json()). print(f"Hello person, there's a {response.status_code} error with your request"). What
is HTTP method in Python?Python requests module has several built-in methods to make Http requests to specified URI using GET, POST, PUT, PATCH or HEAD requests. A Http request is meant to either retrieve data from a specified URI or to push data to a server. It works as a request-response protocol between a client and server. How do you request a post in Python?The post() method sends a POST request to the
specified url. The post() method is used when you want to send some data to the server.
|