Skip to content

Instantly share code, notes, and snippets.

@shohrehsharifib
Created February 21, 2021 18:17
Show Gist options
  • Save shohrehsharifib/796ba9227a1cc41af82a49ae9cd21bfc to your computer and use it in GitHub Desktop.
Save shohrehsharifib/796ba9227a1cc41af82a49ae9cd21bfc to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://cognitiveclass.ai/\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/Ad/CCLog.png\" width=\"200\" align=\"center\">\n",
"</a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1> HTTP and Requests</h1>\n",
"\n",
"Estimated time needed: **15** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"- Understand HTTP \n",
"- Handle the HTTP Requests\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li>\n",
" <a href=\"#index\">Overview of HTTP </a>\n",
" <ul>\n",
" <li><a href=\"#HTTP\">Uniform Resource Locator:URL</a></li>\n",
" <li><a href=\"slice\">Request</a></li>\n",
" <li><a href=\"stride\">Response</a></li>\n",
" </ul>\n",
" </li>\n",
" <li>\n",
" <a href=\"#RP\">Requests in Python </a>\n",
" <ul>\n",
" <li><a href=\"#get\">Get Request with URL Parameters</a></li>\n",
" <li><a href=\"#post\">Post Requests </a></li>\n",
"\n",
"</ul>\n",
" \n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"\">Overview of HTTP </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you, the **client**, use a web page your browser sends an **HTTP** request to the **server** where the page is hosted. The server tries to find the desired **resource** by default \"<code>index.html</code>\". If your request is successful, the server will send the object to the client in an **HTTP response**; this includes information like the type of the **resource**, the length of the **resource**, and other information. \n",
"\n",
"<p>\n",
"The figure below represents the process; the circle on the left represents the client, the circle on the right represents the Web server. The table under the Web server represents a list of resources stored in the web server. In this case an <code>HTML</code> file, <code>png</code> image, and <code>txt</code> file .\n",
"</p>\n",
"<p>\n",
"The <b>HTTP</b> protocol allows you to send and receive information through the web including webpages, images, and other web resources. In this lab, we will provide an overview of the Requests library for interacting with the <code>HTTP </code> protocol. \n",
"</p\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/reqest_basics.png\" width=\"750\" align=\"center\">\n",
" \n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"URL\">Uniform Resource Locator:URL</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Uniform resource locator (URL) is the most popular way to find resources on the web. We can break the URL into three parts. \n",
"\n",
"<ul>\n",
" <li><b>scheme</b> this is this protocol, for this lab it will always be <code>http://</code> </li>\n",
" <li><b> Internet address or Base URL </b> this will be used to find the location here are some examples: <code>www.ibm.com</code> and <code> www.gitlab.com </code> </li>\n",
" <li><b>route</b> location on the web server for example: <code>/images/IDSNlogo.png</code> </li>\n",
"</ul>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You may also here the term uniform resource identifier (URI), URL are actually a subset of URIs. Another popular term is endpoint, this is the URL of an operation provided by a Web server.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"RE\">Request </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The process can be broken into the <b>request</b> and <b>response </b> process. The request using the get method is partially illustrated below. In the start line we have the <code>GET</code> method, this is an <code>HTTP</code> method. Also the location of the resource <code>/index.html</code> and the <code>HTTP</code> version .The Request header passes additional information with an <code>HTTP</code> request:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/reqest_messege.png\" width=\"400\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When an <code>HTTP</code> request is made, an <code>HTTP</code> method is sent, this tells the server what action to perform. A list of several <code>HTTP</code> methods is shown below. We will go over more examples later.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/http_methods.png\" width=\"400\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"RES\">Response</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The figure below represents the response; the response start line contains the version number <code>HTTP/1.0</code>, a status code (200) meaning success, followed by a descriptive phrase (OK). The response header contains useful information. Finally, we have the response body containing the requested file an <code> HTML </code> document. It should be noted that some request have headers.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/response_message.png\" width=\"400\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Some status code examples are shown in the table below, the prefix indicates the class; these are shown in yellow, with actual status codes shown in white. Check out the following <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Status\">link </a> for more descriptions.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/status_code.png\" width=\"300\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"RP\">Requests in Python</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Requests is a python Library that allows you to send <code>HTTP/1.1</code> requests easily. We can import the library as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will also use the following libraries \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os \n",
"from PIL import Image\n",
"from IPython.display import IFrame"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can make a <code>GET</code> request via the method <code>get</code> to [www.ibm.com](http://www.ibm.com?cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ): \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url='https://www.ibm.com/'\n",
"r=requests.get(url)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have the response object <code>r</code> , this has information about the request, like the status of the request. We can view the status code using the attribute <code>status_code </code>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.status_code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can view the request headers:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(r.request.headers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can view the request body, in the following line, as there is no body for a get request we get a <code>None </code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"request body:\", r.request.body)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can view the <code>HTTP</code> response header using the attribute <code>headers</code>. This returns a python dictionary of <code>HTTP</code> response headers. \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"header=r.headers\n",
"print(r.headers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can obtain the date the request was sent using the key <code>Date</code>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"header['date']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<code>Content-Type</code> indicates the type of data:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"header['Content-Type']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also check the <code>encoding</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" r.encoding"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As the <code>Content-Type</code> is <code>text/html</code> we can use the attribute <code>text</code> to display the <code>HTML</code> in the body. We can review the first 100 characters:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.text[0:100]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can load other types of data for non-text requests like images, consider the URL of the following image:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use single quotation marks for defining string\n",
"url='https://gitlab.com/ibm/skills-network/courses/placeholder101/-/raw/master/labs/module%201/images/IDSNlogo.png'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can make a get request:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r=requests.get(url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can look at the response header:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(r.headers)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can we can see the <code>'Content-Type'</code>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.headers['Content-Type']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An image is a response object that contains the image as a <a href=\"https://docs.python.org/3/glossary.html#term-bytes-like-object\">bytes-like object</a>. As a result, we must save it using a file object. First, we specify the file path and\n",
"name \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"path=os.path.join(os.getcwd(),'image.png')\n",
"path"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We save the file, in order to access the body of the response we use the attribute <code>content</code> then save it using the <code>open</code> function and write <code>method</code>: \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(path,'wb') as f:\n",
" f.write(r.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can view the image:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Image.open(path) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Question 1: write <a href=\"https://www.gnu.org/software/wget/\"><code> wget </code></a></h3>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the previous section, we used the <code>wget</code> function to retrieve content from the web server as shown below. Write the python code to perform the same task. The code should be the same as the one used to download the image, but the file name should be <code>'example.txt'</code>. \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<code>!wget -O /resources/data/Example1.txt <https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Example1.txt></code>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Example1.txt'\n",
"path=os.path.join(os.getcwd(),'example1.txt')\n",
"r=requests.get(url)\n",
"with open(path,'wb') as f:\n",
" f.write(r.content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<details><summary>Click here for the solution</summary>\n",
"\n",
"```python\n",
"url='https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%205/data/Example1.txt'\n",
"path=os.path.join(os.getcwd(),'example1.txt')\n",
"r=requests.get(url)\n",
"with open(path,'wb') as f:\n",
" f.write(r.content)\n",
"\n",
"```\n",
"\n",
"</details>\n",
" \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"URL_P\">Get Request with URL Parameters </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use the <b>GET</b> method to modify the results of your query, for example retrieving data from an API . We send a <b>GET</b> request to the server. Like before we have the <b>Base URL</b>, in the <b>Route</b> we append <code>/get</code> this indicates we would like to preform a <code>GET</code> request, this is demonstrated in the following table:\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/base_URL_Route.png\" width=\"400\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The Base URL is for <code>[http://httpbin.org/](http://httpbin.org?cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ&cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork-19487395&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12345678&cvo_campaign=000026UJ)</code> is a simple HTTP Request & Response Service. The <code>URL</code> in Python is given by:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url_get='http://httpbin.org/get'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A <a href=\"https://en.wikipedia.org/wiki/Query_string\">query string</a> is a part of a uniform resource locator (URL), this sends other information to the web server. The start of the query is a <code>?</code>, followed by a series of parameter and value pairs, as shown in the table below. The first parameter name is <code>name</code> and the value is <code>Joseph</code> the second parameter name is <code>ID</code> and the Value is <code>123</code>. Each pair, parameter and value is separated by an equals sign, <code>=</code>.\n",
"The series of pairs is separated by the ampersand <code>&</code>.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/PY0101EN/images/query_string.png\" width=\"500\" align=\"center\">\n",
"</div>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To create a Query string, add a dictionary. The keys are the parameter names, and the values are the value of the Query string.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"payload={\"name\":\"Joseph\",\"ID\":\"123\"}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then passing the dictionary <code>payload</code> to the <code>params</code> parameter of the <code> get()</code> function:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r=requests.get(url_get,params=payload)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can print out the <code>URL</code> and see the name and values \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.url"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is no request body \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"request body:\", r.request.body)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can print out the status code\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(r.status_code)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can view the response as text:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(r.text)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can look at the <code>'Content-Type'</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.headers['Content-Type']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As the content <code>'Content-Type'</code> is in the <code>JSON</code> format we can use the method <code>json()</code> , it returns a Python <code>dict</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.json()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The key <code>args</code> had the name and values:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r.json()['args']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"POST\">Post Requests </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Like a <code>GET</code> request a <code>POST</code> is used to send data to a server, but the <code>POST</code> request sends the data in a request body. In order to send the Post Request in Python in the <code>URL</code> we change the route to <code>POST</code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"url_post='http://httpbin.org/post'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This endpoint will expect data as a file or as a form, a from is convenient way to configure an HTTP request to send data to a server.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To make a <code>POST</code> request we use the <code>post()</code> function, the variable <code>payload</code> is passed to the parameter <code> data </code>:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r_post=requests.post(url_post,data=payload)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Comparing the URL from the response object of the <code>GET</code> and <code>POST</code> request we see the <code>POST</code> request has no name or value pairs.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"POST request URL:\",r_post.url )\n",
"print(\"GET request URL:\",r.url)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can compare the <code>POST</code> and <code>GET</code> request body, we see only the <code>POST</code> request has a body:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"POST request body:\",r_post.request.body)\n",
"print(\"GET request body:\",r.request.body)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can view the form as well:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"r_post.json()['form']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There is a lot more you can do check out <a href=\"https://requests.readthedocs.io/en/master/\">Requests </a> for more.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Authors\n",
"\n",
"<p><a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\" target=\"_blank\">Joseph Santarcangelo</a> <br>A Data Scientist at IBM, and holds a PhD in Electrical Engineering. His research focused on using Machine Learning, Signal Processing, and Computer Vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.</p>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Other Contributors\n",
"\n",
"<a href=\"www.linkedin.com/in/jiahui-mavis-zhou-a4537814a\">Mavis Zhou</a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ---------- | ---------------------------- |\n",
"| 2021-12-20 | 2.1 | Malika | Updated the links |\n",
"| 2020-09-02 | 2.0 | Simran | Template updates to the file |\n",
"| | | | |\n",
"| | | | |\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment