{"id":4490,"date":"2023-11-30T11:01:07","date_gmt":"2023-11-30T11:01:07","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4490"},"modified":"2025-05-01T16:29:52","modified_gmt":"2025-05-01T15:29:52","slug":"http-post","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/tcp-ip\/http\/http-post","title":{"rendered":"HTTP POST"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Post a file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\n\n    #Send POST\n    cloud_url = 'https:\/\/some_domain.com\/some_file'\n    files = {'file': open('my_image.jpg', 'rb')}\n    response = requests.post(cloud_url, files=files, timeout=10)    #Timeout in secs\n    if response.status_code == 200:\n        print(\"Success: \\n\" + response.text)\n    else:\n        print(\"Error: \" + response.status_code)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Post a file from binary data<\/h4>\n\n\n\n<p>I.e, POST without needing to save your data to a file first<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import requests\nimport io\n\n    image1_byte_array = io.BytesIO()\n    image1.save(image1_byte_array, format='JPEG')\n    image1_byte_array = image1_byte_array.getvalue()\n\n    #Send POST\n    cloud_url = 'https:\/\/some_domain.com\/some_file'\n    files = {'file': open('my_image.jpg', 'rb')}\n\n        #files = {'file': image_data}                           #You can use this to send just the binary data as the file content\n        files = {'file': ('1.jpg', image_data, 'image\/jpeg')}   #You can use this to also pass a file name and type if the server wants to see these to handle the file correctly\n        response = requests.post(cloud_server_url, files=files, timeout=10)    #Timeout in secs<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Post file and other data fields<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    #Send POST\n    cloud_url = 'https:\/\/some_domain.com\/some_file'\n    files = {'file': open('my_image.jpg', 'rb')}\n    data_fields = {'my_value1': 'abc'}  # Additional data you want to pass\n    response = requests.post(cloud_url, data=data_fields files=files, timeout=10)    #Timeout in secs\n    if response.status_code == 200:\n        print(\"Success: \\n\" + response.text)\n    else:\n        print(\"Error: \" + response.status_code)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Getting the data with PHP<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$my_value1 = $_POST&#91;'my_value1'];<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Post a file Post a file from binary data I.e, POST without needing to save your data to a file first Post file and other data fields Getting the data with PHP<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[369],"tags":[],"class_list":["post-4490","post","type-post","status-publish","format-standard","hentry","category-http"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4490","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4490"}],"version-history":[{"count":11,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4490\/revisions"}],"predecessor-version":[{"id":4926,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4490\/revisions\/4926"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4490"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4490"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4490"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}