Python Requests | Get and Post Requests

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Http get requests are used for retrieving data from a specified resource. The content received from a get request could be used for rendering a list of data retrieved from an API or for filtering a list of products based on a query string. Get requests should not be used for sensitive information. To make a http get request using the python requests library, we first must import requests. We will then declare a response object called r and set this object equal to requests.get() In the get function, we will pass a string containing the url for the get request. The url we will use is “https://httpbin.org/get” . When the request to this url is made, our response object r will contain the information received from the url. We will be using httpbin because it provides us with a quick, simple way to send and receive well formatted responses from a server. Httpbin acts much like an API from google or twitter, however httpbin is an option to quickly test our http requests without needing authentication. As you can see, we have entered the base URL httpbin.org and appended /get to the end of the base URL which will send a http request to the get route as noted in the documentation. In addition, we can pass url parameters through the requests get function. URL parameters are declared as a query string in the url and are a way to dynamically send additional information to a page. To indicate a query string, a question mark is placed after the domain name and path which indicates the query string has started and the following text is parameter names and values written in pairs. After the parameter name is declared, an equals sign will be appended to the query string to assign a value to the parameter. To add multiple parameters with corresponding values, we’ll use an ‘&’ after each value and before the next parameter name. To add URL parameters to our get request, we will declare a dictionary above the response object called ‘payload’ with two parameters named ‘firstName’ and ‘lastName’ with values of ‘John’ and ‘Smith’ respectively. Within the get function, we will add a second parameter called params and set this equal to the payload dictionary we just created. The keyword params will convert the payload dictionary we just created into a query string which can be sent to the specified url. Printing r.url to the console will reveal the full url and after the question mark indicating the start of a query string, we can see keys and values from the payload dictionary have been url encoded. We can view the http response code by calling the status_code function on the response object r. An http response code of 200 indicates a succesful request has been made. To view the response content from our url, we have a couple options. The first option is to call the content function on the response object r. This will return the response body as bytes. Another option is to use r.text. This function will return the response body that is decoded by the requests library based on the http headers passed in the http request. Within the args object of the response body, we see our parameters that we passed through the get request. Another common http request type is a post request. Post requests are used most often when submitting data from a form or uploading files. A post request is designed for creating or updating resources and allows larger amounts of content to be sent in a single request compared to a get request. To convert our get request to a post request, there are just a few changes to be made. First, we will change the function from get to post. Then, within the post function we will change our url to 'https://httpbin.org/post' which is an endpoint setup by httpbin to receive data from a form or a file. Finally, we will change the params keyword to the keyword data which will convert our payload dictionary to send to our url. Note that the ‘/post’ route on httpbin is expecting a form or file in the post request so the data keyword will handle the conversion of the payload dictionary. Now we are able to run the script and this time send a post request instead of a get request. Notice that the url property of the response object is now the same url that we passed in the post function. This is because when we use the http post protocol, the parameters are not url encoded like in the get request. The dictionary passed through the post request is submitted to our server and is shown in the form object within the response body.
Info
Channel: PyPros
Views: 43,357
Rating: 4.9250002 out of 5
Keywords: python, python 3, python tutorial, python requests, requests tutorial, get request, python requests post, requests post, python requests post json, python post, python http post, python post request example, python requests get, python http get, get request url, python get html from url
Id: qriL9Qe8pJc
Channel Id: undefined
Length: 4min 53sec (293 seconds)
Published: Mon May 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.