{"id":1083,"date":"2018-08-02T22:51:45","date_gmt":"2018-08-02T14:51:45","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=1083"},"modified":"2021-01-08T18:13:41","modified_gmt":"2021-01-08T10:13:41","slug":"how-to-upload-a-file-and-some-data-through-http-multipart-in-python-3-using-the-requests-library","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-upload-a-file-and-some-data-through-http-multipart-in-python-3-using-the-requests-library\/","title":{"rendered":"How to upload a file and some data through HTTP multipart in Python 3 using the requests library"},"content":{"rendered":"<p>Undeniably, the <a href=\"https:\/\/www.techcoil.com\/glossary\/http\" rel=\"noopener\" target=\"_blank\">HTTP protocol<\/a> had become the dominant communication protocol between computers. <\/p>\n<p>Through the HTTP protocol, a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-client\/\" rel=\"noopener\" target=\"_blank\">HTTP client<\/a> can send data to a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a>. For example, a client can upload a file and some data from to a HTTP server through a HTTP multipart request. <\/p>\n<p>If you are building that client with Python 3, then you can use the <a href=\"http:\/\/docs.python-requests.org\/\" rel=\"noopener\" target=\"_blank\">requests<\/a> library to construct the HTTP multipart request easily. <\/p>\n<p>In case you need it, this is how we can upload a file and some data through HTTP multipart in Python 3 using the <a href=\"http:\/\/docs.python-requests.org\/\" rel=\"noopener\" target=\"_blank\">requests<\/a> library. <\/p>\n<h2>1. Define the file upload scenario<\/h2>\n<p>First, let us define the file upload scenario. <\/p>\n<p>Assume that we are building a Python 3 application for your <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3<\/a>. Whenever a button is pressed, an image will be captured with the <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi-camera\" rel=\"noopener\" target=\"_blank\">camera on your Raspberry Pi<\/a> along with temperature <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-read-temperature-and-humidity-from-a-dht11-sensor-that-is-connected-to-a-raspberry-pi-3\/\" rel=\"noopener\" target=\"_blank\">read from a DHT11 sensor<\/a>. <\/p>\n<p>Once we <a href=\"https:\/\/www.techcoil.com\/blog\/building-a-raspberry-pi-3-prototype-camera-that-takes-a-picture-at-the-press-of-a-button\/\" rel=\"noopener\" target=\"_blank\">click the button, the Raspberry Pi 3 will snap a picture and make it available in its filesystem<\/a>.<\/p>\n<p>Given that, we will be building a function that will take a file path to an image and a float value for the temperature as input parameters. Given that, the function will upload the image file and the temperature to a server endpoint at <code>http:\/\/www.example.com\/api\/v1\/sensor_data<\/code>. <\/p>\n<h2>2. Install the requests library<\/h2>\n<p>In order to use the requests library, we need to install it by running the following pip command inside your Python environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install requests\r\n<\/pre>\n<p>If you are <a href=\"http:\/\/use Python 3 virtual environments to run Python 3 applications on your Raspberry Pi\" rel=\"noopener\" target=\"_blank\">using a virtual environment to run your Python application on your Raspberry Pi<\/a>, then you can run the command after activating the virtual environment.<\/p>\n<h2>3. Write the Python 3 function to upload the image file and temperature to the server endpoint<\/h2>\n<p>After you had installed the requests library, we can use the following codes to upload the image file and temperature value via a HTTP multipart request:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport requests\r\nimport os\r\n\r\nfunction send_data_to_server(image_path, temperature):\r\n        \r\n    image_filename = os.path.basename(image_path)\r\n\r\n    multipart_form_data = {\r\n        'image': (image_filename, open(image_path, 'rb')),\r\n        'temperature': ('', str(temperature)),\r\n    }\r\n\r\n    response = requests.post('http:\/\/www.example.com\/api\/v1\/sensor_data\/',\r\n                             files=multipart_form_data)\r\n\r\n    print(response.status_code)\r\n\r\n<\/pre>\n<p>The <code>send_data_to_server<\/code> function accepts a path to an image and a temperature value as its input value. Once the function is being called, it first get the filename of the image from the path. <\/p>\n<p>After that, it constructs a dictionary with two multipart variables - <code>image<\/code> and <code>temperature<\/code>. Both variables and their values are represented by 2-tuples. The <strong>image<\/strong> variable is assigned a filename and a file handle to the actual image. On the other hand, as <code>temperature<\/code> is a single value, the first element of the 2-tuple is empty while the second element contains the temperature as a string. <\/p>\n<p>Once we had constructed the dictionary to represent the content, we call <a href=\"http:\/\/docs.python-requests.org\/en\/master\/api\/#requests.post\" rel=\"noopener\" target=\"_blank\">requests.post<\/a> to send a HTTP multipart request to the server endpoint at <code>http:\/\/www.example.com\/api\/v1\/sensor_data\/<\/code>.<\/p>\n<p>Lastly, the function prints the status of the HTTP response received from the server.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-ht\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-ht&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-ht&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F1083&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Undeniably, the <a href=\"https:\/\/www.techcoil.com\/glossary\/http\" rel=\"noopener\" target=\"_blank\">HTTP protocol<\/a> had become the dominant communication protocol between computers. <\/p>\n<p>Through the HTTP protocol, a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-client\/\" rel=\"noopener\" target=\"_blank\">HTTP client<\/a> can send data to a <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a>. For example, a client can upload a file and some data from to a HTTP server through a HTTP multipart request. <\/p>\n<p>If you are building that client with Python 3, then you can use the <a href=\"http:\/\/docs.python-requests.org\/\" rel=\"noopener\" target=\"_blank\">requests<\/a> library to construct the HTTP multipart request easily. <\/p>\n<p>In case you need it, this is how we can upload a file and some data through HTTP multipart in Python 3 using the <a href=\"http:\/\/docs.python-requests.org\/\" rel=\"noopener\" target=\"_blank\">requests<\/a> library. <\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[166,34,59,226,233,551],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-ht","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1083"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=1083"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/1083\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=1083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=1083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=1083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}