[{"data":1,"prerenderedAt":1847},["ShallowReactive",2],{"doc-\u002Fhow-to\u002Fhow-to-send-a-post-request-in-python":3},{"id":4,"title":5,"body":6,"description":1840,"extension":1841,"meta":1842,"navigation":72,"path":1843,"seo":1844,"stem":1845,"__hash__":1846},"content\u002Fhow-to\u002Fhow-to-send-a-post-request-in-python.md","How to Send a POST Request in Python",{"type":7,"value":8,"toc":1804},"minimark",[9,13,17,20,39,44,224,235,242,260,263,267,270,273,293,297,300,303,317,320,323,327,333,336,348,351,371,374,385,396,400,403,409,550,555,581,591,598,607,622,637,641,644,648,667,670,673,709,713,716,735,738,781,785,788,808,815,822,826,829,838,980,984,996,999,1003,1006,1008,1024,1031,1191,1200,1204,1207,1210,1227,1237,1447,1451,1471,1475,1478,1509,1512,1537,1584,1594,1598,1601,1635,1641,1645,1656,1661,1666,1671,1675,1681,1691,1698,1701,1704,1722,1727,1731,1737,1745,1751,1761,1765,1800],[10,11,5],"h1",{"id":12},"how-to-send-a-post-request-in-python",[14,15,16],"p",{},"If you want to send data to an API in Python, a POST request is one of the most common ways to do it.",[14,18,19],{},"On this page, you will learn how to:",[21,22,23,27,30,33,36],"ul",{},[24,25,26],"li",{},"Send a basic POST request in Python",[24,28,29],{},"Send JSON data to an API",[24,31,32],{},"Check the status code",[24,34,35],{},"Read JSON or text responses",[24,37,38],{},"Avoid common beginner mistakes",[40,41,43],"h2",{"id":42},"quick-answer","Quick answer",[45,46,51],"pre",{"className":47,"code":48,"language":49,"meta":50,"style":50},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\npayload = {\"name\": \"Alice\", \"age\": 25}\n\nresponse = requests.post(url, json=payload)\nprint(response.status_code)\nprint(response.json())\n","python","",[52,53,54,67,74,95,144,149,187,207],"code",{"__ignoreMap":50},[55,56,59,63],"span",{"class":57,"line":58},"line",1,[55,60,62],{"class":61},"sVHd0","import",[55,64,66],{"class":65},"su5hD"," requests\n",[55,68,70],{"class":57,"line":69},2,[55,71,73],{"emptyLinePlaceholder":72},true,"\n",[55,75,77,80,84,88,92],{"class":57,"line":76},3,[55,78,79],{"class":65},"url ",[55,81,83],{"class":82},"smGrS","=",[55,85,87],{"class":86},"sjJ54"," \"",[55,89,91],{"class":90},"s_sjI","https:\u002F\u002Fhttpbin.org\u002Fpost",[55,93,94],{"class":86},"\"\n",[55,96,98,101,103,107,110,113,115,118,120,123,125,128,130,133,135,137,141],{"class":57,"line":97},4,[55,99,100],{"class":65},"payload ",[55,102,83],{"class":82},[55,104,106],{"class":105},"sP7_E"," {",[55,108,109],{"class":86},"\"",[55,111,112],{"class":90},"name",[55,114,109],{"class":86},[55,116,117],{"class":105},":",[55,119,87],{"class":86},[55,121,122],{"class":90},"Alice",[55,124,109],{"class":86},[55,126,127],{"class":105},",",[55,129,87],{"class":86},[55,131,132],{"class":90},"age",[55,134,109],{"class":86},[55,136,117],{"class":105},[55,138,140],{"class":139},"srdBf"," 25",[55,142,143],{"class":105},"}\n",[55,145,147],{"class":57,"line":146},5,[55,148,73],{"emptyLinePlaceholder":72},[55,150,152,155,157,160,163,167,170,173,175,179,181,184],{"class":57,"line":151},6,[55,153,154],{"class":65},"response ",[55,156,83],{"class":82},[55,158,159],{"class":65}," requests",[55,161,162],{"class":105},".",[55,164,166],{"class":165},"slqww","post",[55,168,169],{"class":105},"(",[55,171,172],{"class":165},"url",[55,174,127],{"class":105},[55,176,178],{"class":177},"s99_P"," json",[55,180,83],{"class":82},[55,182,183],{"class":165},"payload",[55,185,186],{"class":105},")\n",[55,188,190,194,196,199,201,205],{"class":57,"line":189},7,[55,191,193],{"class":192},"sptTA","print",[55,195,169],{"class":105},[55,197,198],{"class":165},"response",[55,200,162],{"class":105},[55,202,204],{"class":203},"skxfh","status_code",[55,206,186],{"class":105},[55,208,210,212,214,216,218,221],{"class":57,"line":209},8,[55,211,193],{"class":192},[55,213,169],{"class":105},[55,215,198],{"class":165},[55,217,162],{"class":105},[55,219,220],{"class":165},"json",[55,222,223],{"class":105},"())\n",[14,225,226,227,230,231,234],{},"Use ",[52,228,229],{},"requests.post()"," with ",[52,232,233],{},"json="," when the API expects JSON data.",[14,236,237,238,241],{},"Install ",[52,239,240],{},"requests"," first if needed:",[45,243,247],{"className":244,"code":245,"language":246,"meta":50,"style":50},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","pip install requests\n","bash",[52,248,249],{"__ignoreMap":50},[55,250,251,255,258],{"class":57,"line":58},[55,252,254],{"class":253},"sbgvK","pip",[55,256,257],{"class":90}," install",[55,259,66],{"class":90},[261,262],"hr",{},[40,264,266],{"id":265},"what-this-page-helps-you-do","What this page helps you do",[14,268,269],{},"This page shows you the practical steps for sending a POST request in Python.",[14,271,272],{},"You will learn how to:",[21,274,275,278,284,287,290],{},[24,276,277],{},"Send data to a server",[24,279,280,281,283],{},"Use the ",[52,282,240],{}," library",[24,285,286],{},"Send JSON and form data",[24,288,289],{},"Read the server response",[24,291,292],{},"Handle common errors safely",[40,294,296],{"id":295},"when-to-use-a-post-request","When to use a POST request",[14,298,299],{},"Use a POST request when you want to send data to a server.",[14,301,302],{},"Common examples:",[21,304,305,308,311,314],{},[24,306,307],{},"Creating a new user",[24,309,310],{},"Sending form data",[24,312,313],{},"Logging in",[24,315,316],{},"Submitting JSON to an API",[14,318,319],{},"POST is different from GET because POST sends data in the request body.",[14,321,322],{},"If you are new to working with APIs, you may also want to read how to make an API request in Python.",[40,324,326],{"id":325},"install-and-import-the-requests-library","Install and import the requests library",[14,328,329,330,332],{},"Most beginners use the ",[52,331,240],{}," package because it is much simpler than built-in alternatives.",[14,334,335],{},"Install it with pip:",[45,337,338],{"className":244,"code":245,"language":246,"meta":50,"style":50},[52,339,340],{"__ignoreMap":50},[55,341,342,344,346],{"class":57,"line":58},[55,343,254],{"class":253},[55,345,257],{"class":90},[55,347,66],{"class":90},[14,349,350],{},"If that does not work, try:",[45,352,354],{"className":244,"code":353,"language":246,"meta":50,"style":50},"python -m pip install requests\n",[52,355,356],{"__ignoreMap":50},[55,357,358,360,364,367,369],{"class":57,"line":58},[55,359,49],{"class":253},[55,361,363],{"class":362},"stzsN"," -m",[55,365,366],{"class":90}," pip",[55,368,257],{"class":90},[55,370,66],{"class":90},[14,372,373],{},"Then import it at the top of your Python file:",[45,375,377],{"className":47,"code":376,"language":49,"meta":50,"style":50},"import requests\n",[52,378,379],{"__ignoreMap":50},[55,380,381,383],{"class":57,"line":58},[55,382,62],{"class":61},[55,384,66],{"class":65},[14,386,387,388,162],{},"If Python says the module does not exist, see ",[389,390,392,393],"a",{"href":391},"\u002Ferrors\u002Fmodulenotfounderror-no-module-named-x-fix","how to fix ",[52,394,395],{},"ModuleNotFoundError",[40,397,399],{"id":398},"send-a-simple-post-request-with-json","Send a simple POST request with JSON",[14,401,402],{},"This is the most common beginner example.",[14,404,226,405,408],{},[52,406,407],{},"requests.post(url, json=data)"," and pass a Python dictionary.",[45,410,412],{"className":47,"code":411,"language":49,"meta":50,"style":50},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\ndata = {\n    \"name\": \"Alice\",\n    \"age\": 25\n}\n\nresponse = requests.post(url, json=data)\n\nprint(response.status_code)\nprint(response.json())\n",[52,413,414,420,424,436,446,466,479,483,487,515,520,535],{"__ignoreMap":50},[55,415,416,418],{"class":57,"line":58},[55,417,62],{"class":61},[55,419,66],{"class":65},[55,421,422],{"class":57,"line":69},[55,423,73],{"emptyLinePlaceholder":72},[55,425,426,428,430,432,434],{"class":57,"line":76},[55,427,79],{"class":65},[55,429,83],{"class":82},[55,431,87],{"class":86},[55,433,91],{"class":90},[55,435,94],{"class":86},[55,437,438,441,443],{"class":57,"line":97},[55,439,440],{"class":65},"data ",[55,442,83],{"class":82},[55,444,445],{"class":105}," {\n",[55,447,448,451,453,455,457,459,461,463],{"class":57,"line":146},[55,449,450],{"class":86},"    \"",[55,452,112],{"class":90},[55,454,109],{"class":86},[55,456,117],{"class":105},[55,458,87],{"class":86},[55,460,122],{"class":90},[55,462,109],{"class":86},[55,464,465],{"class":105},",\n",[55,467,468,470,472,474,476],{"class":57,"line":151},[55,469,450],{"class":86},[55,471,132],{"class":90},[55,473,109],{"class":86},[55,475,117],{"class":105},[55,477,478],{"class":139}," 25\n",[55,480,481],{"class":57,"line":189},[55,482,143],{"class":105},[55,484,485],{"class":57,"line":209},[55,486,73],{"emptyLinePlaceholder":72},[55,488,490,492,494,496,498,500,502,504,506,508,510,513],{"class":57,"line":489},9,[55,491,154],{"class":65},[55,493,83],{"class":82},[55,495,159],{"class":65},[55,497,162],{"class":105},[55,499,166],{"class":165},[55,501,169],{"class":105},[55,503,172],{"class":165},[55,505,127],{"class":105},[55,507,178],{"class":177},[55,509,83],{"class":82},[55,511,512],{"class":165},"data",[55,514,186],{"class":105},[55,516,518],{"class":57,"line":517},10,[55,519,73],{"emptyLinePlaceholder":72},[55,521,523,525,527,529,531,533],{"class":57,"line":522},11,[55,524,193],{"class":192},[55,526,169],{"class":105},[55,528,198],{"class":165},[55,530,162],{"class":105},[55,532,204],{"class":203},[55,534,186],{"class":105},[55,536,538,540,542,544,546,548],{"class":57,"line":537},12,[55,539,193],{"class":192},[55,541,169],{"class":105},[55,543,198],{"class":165},[55,545,162],{"class":105},[55,547,220],{"class":165},[55,549,223],{"class":105},[551,552,554],"h3",{"id":553},"what-this-code-does","What this code does",[21,556,557,562,567,576],{},[24,558,559,561],{},[52,560,172],{}," is the API endpoint",[24,563,564,566],{},[52,565,512],{}," is a Python dictionary",[24,568,569,572,573,575],{},[52,570,571],{},"json=data"," tells ",[52,574,240],{}," to convert the dictionary to JSON",[24,577,578,580],{},[52,579,198],{}," stores the server's reply",[14,582,583,584,587,588,162],{},"A successful response often has a status code like ",[52,585,586],{},"200"," or ",[52,589,590],{},"201",[551,592,594,595,597],{"id":593},"why-use-json","Why use ",[52,596,233],{},"?",[14,599,600,601,603,604,606],{},"When you use ",[52,602,233],{},", the ",[52,605,240],{}," library:",[21,608,609,612,615],{},[24,610,611],{},"Converts your Python dictionary into JSON",[24,613,614],{},"Sends it in the request body",[24,616,617,618,621],{},"Usually adds the correct ",[52,619,620],{},"Content-Type"," header for JSON",[14,623,624,625,629,630,162],{},"If you need help understanding JSON data, see ",[389,626,628],{"href":627},"\u002Fhow-to\u002Fhow-to-parse-json-in-python","how to parse JSON in Python"," or the ",[389,631,633,634,636],{"href":632},"\u002Fstandard-library\u002Fpython-json-module-overview","Python ",[52,635,220],{}," module overview",[40,638,640],{"id":639},"check-the-response","Check the response",[14,642,643],{},"After sending the request, you should always check the response.",[551,645,647],{"id":646},"get-the-status-code","Get the status code",[45,649,651],{"className":47,"code":650,"language":49,"meta":50,"style":50},"print(response.status_code)\n",[52,652,653],{"__ignoreMap":50},[55,654,655,657,659,661,663,665],{"class":57,"line":58},[55,656,193],{"class":192},[55,658,169],{"class":105},[55,660,198],{"class":165},[55,662,162],{"class":105},[55,664,204],{"class":203},[55,666,186],{"class":105},[14,668,669],{},"This helps you see whether the request worked.",[14,671,672],{},"Examples:",[21,674,675,680,685,691,697,703],{},[24,676,677,679],{},[52,678,586],{}," = OK",[24,681,682,684],{},[52,683,590],{}," = Created",[24,686,687,690],{},[52,688,689],{},"400"," = Bad request",[24,692,693,696],{},[52,694,695],{},"401"," = Unauthorized",[24,698,699,702],{},[52,700,701],{},"404"," = Not found",[24,704,705,708],{},[52,706,707],{},"500"," = Server error",[551,710,712],{"id":711},"read-json-from-the-response","Read JSON from the response",[14,714,715],{},"If the API returns JSON, use:",[45,717,719],{"className":47,"code":718,"language":49,"meta":50,"style":50},"print(response.json())\n",[52,720,721],{"__ignoreMap":50},[55,722,723,725,727,729,731,733],{"class":57,"line":58},[55,724,193],{"class":192},[55,726,169],{"class":105},[55,728,198],{"class":165},[55,730,162],{"class":105},[55,732,220],{"class":165},[55,734,223],{"class":105},[14,736,737],{},"Example:",[45,739,741],{"className":47,"code":740,"language":49,"meta":50,"style":50},"result = response.json()\nprint(result[\"json\"])\n",[52,742,743,760],{"__ignoreMap":50},[55,744,745,748,750,753,755,757],{"class":57,"line":58},[55,746,747],{"class":65},"result ",[55,749,83],{"class":82},[55,751,752],{"class":65}," response",[55,754,162],{"class":105},[55,756,220],{"class":165},[55,758,759],{"class":105},"()\n",[55,761,762,764,766,769,772,774,776,778],{"class":57,"line":69},[55,763,193],{"class":192},[55,765,169],{"class":105},[55,767,768],{"class":165},"result",[55,770,771],{"class":105},"[",[55,773,109],{"class":86},[55,775,220],{"class":90},[55,777,109],{"class":86},[55,779,780],{"class":105},"])\n",[551,782,784],{"id":783},"read-plain-text-instead","Read plain text instead",[14,786,787],{},"If the server returns plain text or HTML, use:",[45,789,791],{"className":47,"code":790,"language":49,"meta":50,"style":50},"print(response.text)\n",[52,792,793],{"__ignoreMap":50},[55,794,795,797,799,801,803,806],{"class":57,"line":58},[55,796,193],{"class":192},[55,798,169],{"class":105},[55,800,198],{"class":165},[55,802,162],{"class":105},[55,804,805],{"class":203},"text",[55,807,186],{"class":105},[14,809,810,811,814],{},"This is useful when ",[52,812,813],{},"response.json()"," fails.",[14,816,817,818,162],{},"If you want more help reading API replies, see ",[389,819,821],{"href":820},"\u002Fhow-to\u002Fhow-to-handle-api-responses-in-python","how to handle API responses in Python",[40,823,825],{"id":824},"send-form-data-instead-of-json","Send form data instead of JSON",[14,827,828],{},"Some APIs or websites expect form-style data instead of JSON.",[14,830,831,832,835,836,162],{},"In that case, use ",[52,833,834],{},"data="," instead of ",[52,837,233],{},[45,839,841],{"className":47,"code":840,"language":49,"meta":50,"style":50},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\nform_data = {\n    \"username\": \"alice\",\n    \"password\": \"secret123\"\n}\n\nresponse = requests.post(url, data=form_data)\n\nprint(response.status_code)\nprint(response.text)\n",[52,842,843,849,853,865,874,894,912,916,920,948,952,966],{"__ignoreMap":50},[55,844,845,847],{"class":57,"line":58},[55,846,62],{"class":61},[55,848,66],{"class":65},[55,850,851],{"class":57,"line":69},[55,852,73],{"emptyLinePlaceholder":72},[55,854,855,857,859,861,863],{"class":57,"line":76},[55,856,79],{"class":65},[55,858,83],{"class":82},[55,860,87],{"class":86},[55,862,91],{"class":90},[55,864,94],{"class":86},[55,866,867,870,872],{"class":57,"line":97},[55,868,869],{"class":65},"form_data ",[55,871,83],{"class":82},[55,873,445],{"class":105},[55,875,876,878,881,883,885,887,890,892],{"class":57,"line":146},[55,877,450],{"class":86},[55,879,880],{"class":90},"username",[55,882,109],{"class":86},[55,884,117],{"class":105},[55,886,87],{"class":86},[55,888,889],{"class":90},"alice",[55,891,109],{"class":86},[55,893,465],{"class":105},[55,895,896,898,901,903,905,907,910],{"class":57,"line":151},[55,897,450],{"class":86},[55,899,900],{"class":90},"password",[55,902,109],{"class":86},[55,904,117],{"class":105},[55,906,87],{"class":86},[55,908,909],{"class":90},"secret123",[55,911,94],{"class":86},[55,913,914],{"class":57,"line":189},[55,915,143],{"class":105},[55,917,918],{"class":57,"line":209},[55,919,73],{"emptyLinePlaceholder":72},[55,921,922,924,926,928,930,932,934,936,938,941,943,946],{"class":57,"line":489},[55,923,154],{"class":65},[55,925,83],{"class":82},[55,927,159],{"class":65},[55,929,162],{"class":105},[55,931,166],{"class":165},[55,933,169],{"class":105},[55,935,172],{"class":165},[55,937,127],{"class":105},[55,939,940],{"class":177}," data",[55,942,83],{"class":82},[55,944,945],{"class":165},"form_data",[55,947,186],{"class":105},[55,949,950],{"class":57,"line":517},[55,951,73],{"emptyLinePlaceholder":72},[55,953,954,956,958,960,962,964],{"class":57,"line":522},[55,955,193],{"class":192},[55,957,169],{"class":105},[55,959,198],{"class":165},[55,961,162],{"class":105},[55,963,204],{"class":203},[55,965,186],{"class":105},[55,967,968,970,972,974,976,978],{"class":57,"line":537},[55,969,193],{"class":192},[55,971,169],{"class":105},[55,973,198],{"class":165},[55,975,162],{"class":105},[55,977,805],{"class":203},[55,979,186],{"class":105},[551,981,983],{"id":982},"important-difference","Important difference",[21,985,986,991],{},[24,987,226,988,990],{},[52,989,233],{}," for JSON data",[24,992,226,993,995],{},[52,994,834],{}," for form data or raw body content",[14,997,998],{},"Do not mix them up unless the API documentation tells you to.",[40,1000,1002],{"id":1001},"add-headers-when-needed","Add headers when needed",[14,1004,1005],{},"Some APIs require headers.",[14,1007,302],{},[21,1009,1010,1015,1019],{},[24,1011,1012],{},[52,1013,1014],{},"Authorization",[24,1016,1017],{},[52,1018,620],{},[24,1020,1021],{},[52,1022,1023],{},"Accept",[14,1025,1026,1027,1030],{},"You can send headers with the ",[52,1028,1029],{},"headers="," argument.",[45,1032,1034],{"className":47,"code":1033,"language":49,"meta":50,"style":50},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\ndata = {\"message\": \"Hello\"}\n\nheaders = {\n    \"Authorization\": \"Bearer your_token_here\"\n}\n\nresponse = requests.post(url, json=data, headers=headers)\n\nprint(response.status_code)\nprint(response.json())\n",[52,1035,1036,1042,1046,1058,1084,1088,1097,1114,1118,1122,1158,1162,1176],{"__ignoreMap":50},[55,1037,1038,1040],{"class":57,"line":58},[55,1039,62],{"class":61},[55,1041,66],{"class":65},[55,1043,1044],{"class":57,"line":69},[55,1045,73],{"emptyLinePlaceholder":72},[55,1047,1048,1050,1052,1054,1056],{"class":57,"line":76},[55,1049,79],{"class":65},[55,1051,83],{"class":82},[55,1053,87],{"class":86},[55,1055,91],{"class":90},[55,1057,94],{"class":86},[55,1059,1060,1062,1064,1066,1068,1071,1073,1075,1077,1080,1082],{"class":57,"line":97},[55,1061,440],{"class":65},[55,1063,83],{"class":82},[55,1065,106],{"class":105},[55,1067,109],{"class":86},[55,1069,1070],{"class":90},"message",[55,1072,109],{"class":86},[55,1074,117],{"class":105},[55,1076,87],{"class":86},[55,1078,1079],{"class":90},"Hello",[55,1081,109],{"class":86},[55,1083,143],{"class":105},[55,1085,1086],{"class":57,"line":146},[55,1087,73],{"emptyLinePlaceholder":72},[55,1089,1090,1093,1095],{"class":57,"line":151},[55,1091,1092],{"class":65},"headers ",[55,1094,83],{"class":82},[55,1096,445],{"class":105},[55,1098,1099,1101,1103,1105,1107,1109,1112],{"class":57,"line":189},[55,1100,450],{"class":86},[55,1102,1014],{"class":90},[55,1104,109],{"class":86},[55,1106,117],{"class":105},[55,1108,87],{"class":86},[55,1110,1111],{"class":90},"Bearer your_token_here",[55,1113,94],{"class":86},[55,1115,1116],{"class":57,"line":209},[55,1117,143],{"class":105},[55,1119,1120],{"class":57,"line":489},[55,1121,73],{"emptyLinePlaceholder":72},[55,1123,1124,1126,1128,1130,1132,1134,1136,1138,1140,1142,1144,1146,1148,1151,1153,1156],{"class":57,"line":517},[55,1125,154],{"class":65},[55,1127,83],{"class":82},[55,1129,159],{"class":65},[55,1131,162],{"class":105},[55,1133,166],{"class":165},[55,1135,169],{"class":105},[55,1137,172],{"class":165},[55,1139,127],{"class":105},[55,1141,178],{"class":177},[55,1143,83],{"class":82},[55,1145,512],{"class":165},[55,1147,127],{"class":105},[55,1149,1150],{"class":177}," headers",[55,1152,83],{"class":82},[55,1154,1155],{"class":165},"headers",[55,1157,186],{"class":105},[55,1159,1160],{"class":57,"line":522},[55,1161,73],{"emptyLinePlaceholder":72},[55,1163,1164,1166,1168,1170,1172,1174],{"class":57,"line":537},[55,1165,193],{"class":192},[55,1167,169],{"class":105},[55,1169,198],{"class":165},[55,1171,162],{"class":105},[55,1173,204],{"class":203},[55,1175,186],{"class":105},[55,1177,1179,1181,1183,1185,1187,1189],{"class":57,"line":1178},13,[55,1180,193],{"class":192},[55,1182,169],{"class":105},[55,1184,198],{"class":165},[55,1186,162],{"class":105},[55,1188,220],{"class":165},[55,1190,223],{"class":105},[14,1192,1193,1194,1196,1197,1199],{},"If you use ",[52,1195,233],{},", ",[52,1198,240],{}," usually sets the JSON content type for you.",[40,1201,1203],{"id":1202},"handle-errors-safely","Handle errors safely",[14,1205,1206],{},"Network requests do not always work.",[14,1208,1209],{},"They can fail because of:",[21,1211,1212,1215,1218,1221,1224],{},[24,1213,1214],{},"A bad URL",[24,1216,1217],{},"No internet connection",[24,1219,1220],{},"A timeout",[24,1222,1223],{},"Server errors",[24,1225,1226],{},"Missing authentication",[14,1228,1229,1230,1233,1234,162],{},"A simple beginner-friendly pattern is to use ",[52,1231,1232],{},"try"," and ",[52,1235,1236],{},"except",[45,1238,1240],{"className":47,"code":1239,"language":49,"meta":50,"style":50},"import requests\n\nurl = \"https:\u002F\u002Fhttpbin.org\u002Fpost\"\ndata = {\"name\": \"Alice\"}\n\ntry:\n    response = requests.post(url, json=data, timeout=10)\n    response.raise_for_status()\n\n    print(\"Status:\", response.status_code)\n    print(\"Response JSON:\", response.json())\n\nexcept requests.exceptions.RequestException as error:\n    print(\"Request failed:\", error)\n",[52,1241,1242,1248,1252,1264,1288,1292,1299,1336,1348,1352,1376,1399,1403,1427],{"__ignoreMap":50},[55,1243,1244,1246],{"class":57,"line":58},[55,1245,62],{"class":61},[55,1247,66],{"class":65},[55,1249,1250],{"class":57,"line":69},[55,1251,73],{"emptyLinePlaceholder":72},[55,1253,1254,1256,1258,1260,1262],{"class":57,"line":76},[55,1255,79],{"class":65},[55,1257,83],{"class":82},[55,1259,87],{"class":86},[55,1261,91],{"class":90},[55,1263,94],{"class":86},[55,1265,1266,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286],{"class":57,"line":97},[55,1267,440],{"class":65},[55,1269,83],{"class":82},[55,1271,106],{"class":105},[55,1273,109],{"class":86},[55,1275,112],{"class":90},[55,1277,109],{"class":86},[55,1279,117],{"class":105},[55,1281,87],{"class":86},[55,1283,122],{"class":90},[55,1285,109],{"class":86},[55,1287,143],{"class":105},[55,1289,1290],{"class":57,"line":146},[55,1291,73],{"emptyLinePlaceholder":72},[55,1293,1294,1296],{"class":57,"line":151},[55,1295,1232],{"class":61},[55,1297,1298],{"class":105},":\n",[55,1300,1301,1304,1306,1308,1310,1312,1314,1316,1318,1320,1322,1324,1326,1329,1331,1334],{"class":57,"line":189},[55,1302,1303],{"class":65},"    response ",[55,1305,83],{"class":82},[55,1307,159],{"class":65},[55,1309,162],{"class":105},[55,1311,166],{"class":165},[55,1313,169],{"class":105},[55,1315,172],{"class":165},[55,1317,127],{"class":105},[55,1319,178],{"class":177},[55,1321,83],{"class":82},[55,1323,512],{"class":165},[55,1325,127],{"class":105},[55,1327,1328],{"class":177}," timeout",[55,1330,83],{"class":82},[55,1332,1333],{"class":139},"10",[55,1335,186],{"class":105},[55,1337,1338,1341,1343,1346],{"class":57,"line":209},[55,1339,1340],{"class":65},"    response",[55,1342,162],{"class":105},[55,1344,1345],{"class":165},"raise_for_status",[55,1347,759],{"class":105},[55,1349,1350],{"class":57,"line":489},[55,1351,73],{"emptyLinePlaceholder":72},[55,1353,1354,1357,1359,1361,1364,1366,1368,1370,1372,1374],{"class":57,"line":517},[55,1355,1356],{"class":192},"    print",[55,1358,169],{"class":105},[55,1360,109],{"class":86},[55,1362,1363],{"class":90},"Status:",[55,1365,109],{"class":86},[55,1367,127],{"class":105},[55,1369,752],{"class":165},[55,1371,162],{"class":105},[55,1373,204],{"class":203},[55,1375,186],{"class":105},[55,1377,1378,1380,1382,1384,1387,1389,1391,1393,1395,1397],{"class":57,"line":522},[55,1379,1356],{"class":192},[55,1381,169],{"class":105},[55,1383,109],{"class":86},[55,1385,1386],{"class":90},"Response JSON:",[55,1388,109],{"class":86},[55,1390,127],{"class":105},[55,1392,752],{"class":165},[55,1394,162],{"class":105},[55,1396,220],{"class":165},[55,1398,223],{"class":105},[55,1400,1401],{"class":57,"line":537},[55,1402,73],{"emptyLinePlaceholder":72},[55,1404,1405,1407,1409,1411,1414,1416,1419,1422,1425],{"class":57,"line":1178},[55,1406,1236],{"class":61},[55,1408,159],{"class":65},[55,1410,162],{"class":105},[55,1412,1413],{"class":203},"exceptions",[55,1415,162],{"class":105},[55,1417,1418],{"class":203},"RequestException",[55,1420,1421],{"class":61}," as",[55,1423,1424],{"class":65}," error",[55,1426,1298],{"class":105},[55,1428,1430,1432,1434,1436,1439,1441,1443,1445],{"class":57,"line":1429},14,[55,1431,1356],{"class":192},[55,1433,169],{"class":105},[55,1435,109],{"class":86},[55,1437,1438],{"class":90},"Request failed:",[55,1440,109],{"class":86},[55,1442,127],{"class":105},[55,1444,1424],{"class":165},[55,1446,186],{"class":105},[551,1448,1450],{"id":1449},"why-this-helps","Why this helps",[21,1452,1453,1459,1465],{},[24,1454,1455,1458],{},[52,1456,1457],{},"timeout=10"," prevents the program from waiting forever",[24,1460,1461,1464],{},[52,1462,1463],{},"raise_for_status()"," turns HTTP errors into exceptions",[24,1466,1467,1470],{},[52,1468,1469],{},"except requests.exceptions.RequestException"," catches common request problems",[40,1472,1474],{"id":1473},"beginner-debugging-checklist","Beginner debugging checklist",[14,1476,1477],{},"If your POST request is not working, check these first:",[21,1479,1480,1483,1490,1495,1498,1503,1506],{},[24,1481,1482],{},"Is the URL correct?",[24,1484,1485,1486,587,1488,597],{},"Does the API expect ",[52,1487,233],{},[52,1489,834],{},[24,1491,1492,1493,597],{},"Did you install ",[52,1494,240],{},[24,1496,1497],{},"Are you sending the correct field names?",[24,1499,1500,1501,597],{},"Does the API require headers such as ",[52,1502,1014],{},[24,1504,1505],{},"Did you check the status code?",[24,1507,1508],{},"Did you print the response body?",[14,1510,1511],{},"Useful debugging commands:",[45,1513,1515],{"className":244,"code":1514,"language":246,"meta":50,"style":50},"pip install requests\npython -m pip install requests\n",[52,1516,1517,1525],{"__ignoreMap":50},[55,1518,1519,1521,1523],{"class":57,"line":58},[55,1520,254],{"class":253},[55,1522,257],{"class":90},[55,1524,66],{"class":90},[55,1526,1527,1529,1531,1533,1535],{"class":57,"line":69},[55,1528,49],{"class":253},[55,1530,363],{"class":362},[55,1532,366],{"class":90},[55,1534,257],{"class":90},[55,1536,66],{"class":90},[45,1538,1540],{"className":47,"code":1539,"language":49,"meta":50,"style":50},"print(response.status_code)\nprint(response.text)\nprint(response.headers)\n",[52,1541,1542,1556,1570],{"__ignoreMap":50},[55,1543,1544,1546,1548,1550,1552,1554],{"class":57,"line":58},[55,1545,193],{"class":192},[55,1547,169],{"class":105},[55,1549,198],{"class":165},[55,1551,162],{"class":105},[55,1553,204],{"class":203},[55,1555,186],{"class":105},[55,1557,1558,1560,1562,1564,1566,1568],{"class":57,"line":69},[55,1559,193],{"class":192},[55,1561,169],{"class":105},[55,1563,198],{"class":165},[55,1565,162],{"class":105},[55,1567,805],{"class":203},[55,1569,186],{"class":105},[55,1571,1572,1574,1576,1578,1580,1582],{"class":57,"line":76},[55,1573,193],{"class":192},[55,1575,169],{"class":105},[55,1577,198],{"class":165},[55,1579,162],{"class":105},[55,1581,1155],{"class":203},[55,1583,186],{"class":105},[14,1585,1586,1587,1589,1590,1593],{},"If ",[52,1588,813],{}," gives an error, print ",[52,1591,1592],{},"response.text"," first. The server may have returned HTML or a plain text error message instead of JSON.",[40,1595,1597],{"id":1596},"common-mistakes","Common mistakes",[14,1599,1600],{},"Here are some common causes of POST request problems:",[21,1602,1603,1609,1615,1621,1624,1629,1632],{},[24,1604,1605,1606,1608],{},"Using ",[52,1607,834],{}," when the API expects JSON",[24,1610,1611,1612,1614],{},"Calling ",[52,1613,813],{}," when the server did not return JSON",[24,1616,1617,1618,1620],{},"Forgetting to install the ",[52,1619,240],{}," package",[24,1622,1623],{},"Using the wrong URL or endpoint",[24,1625,1626,1627],{},"Missing required headers such as ",[52,1628,1014],{},[24,1630,1631],{},"Sending fields with the wrong names",[24,1633,1634],{},"Not checking the status code before processing the response",[14,1636,1637,1638,162],{},"A very common mistake is treating a string like a dictionary after a bad JSON parse step. If that happens, see how to fix ",[52,1639,1640],{},"TypeError: string indices must be integers or slices",[40,1642,1644],{"id":1643},"faq","FAQ",[551,1646,1648,1649,1233,1651,1653,1654,597],{"id":1647},"what-is-the-difference-between-data-and-json-in-requestspost","What is the difference between ",[52,1650,834],{},[52,1652,233],{}," in ",[52,1655,229],{},[14,1657,226,1658,1660],{},[52,1659,233],{}," to send JSON.",[14,1662,226,1663,1665],{},[52,1664,834],{}," for form data or raw body content.",[14,1667,1668,1669,162],{},"Many APIs expect ",[52,1670,233],{},[551,1672,1674],{"id":1673},"do-i-need-to-import-a-special-module-to-send-post-requests","Do I need to import a special module to send POST requests?",[14,1676,1677,1678,1680],{},"Yes. Beginners usually use the ",[52,1679,240],{}," library, so you need:",[45,1682,1683],{"className":47,"code":376,"language":49,"meta":50,"style":50},[52,1684,1685],{"__ignoreMap":50},[55,1686,1687,1689],{"class":57,"line":58},[55,1688,62],{"class":61},[55,1690,66],{"class":65},[551,1692,1694,1695,1697],{"id":1693},"why-does-responsejson-fail","Why does ",[52,1696,813],{}," fail?",[14,1699,1700],{},"It usually fails when the server returned plain text, HTML, or an error page instead of JSON.",[14,1702,1703],{},"In that case, print:",[45,1705,1706],{"className":47,"code":790,"language":49,"meta":50,"style":50},[52,1707,1708],{"__ignoreMap":50},[55,1709,1710,1712,1714,1716,1718,1720],{"class":57,"line":58},[55,1711,193],{"class":192},[55,1713,169],{"class":105},[55,1715,198],{"class":165},[55,1717,162],{"class":105},[55,1719,805],{"class":203},[55,1721,186],{"class":105},[14,1723,1724,1725,162],{},"You can also learn more from ",[389,1726,628],{"href":627},[551,1728,1730],{"id":1729},"how-do-i-know-if-the-post-request-worked","How do I know if the POST request worked?",[14,1732,1733,1734,162],{},"Check ",[52,1735,1736],{},"response.status_code",[14,1738,1739,1740,587,1742,1744],{},"Codes like ",[52,1741,586],{},[52,1743,590],{}," often mean success.",[551,1746,1748,1749,597],{"id":1747},"can-i-send-a-post-request-without-requests","Can I send a POST request without ",[52,1750,240],{},[14,1752,1753,1754,1757,1758,1760],{},"Yes, with ",[52,1755,1756],{},"urllib",", but ",[52,1759,240],{}," is much easier for beginners.",[40,1762,1764],{"id":1763},"see-also","See also",[21,1766,1767,1772,1777,1783,1792],{},[24,1768,1769],{},[389,1770,1771],{"href":820},"How to handle API responses in Python",[24,1773,1774],{},[389,1775,1776],{"href":627},"How to parse JSON in Python",[24,1778,1779],{},[389,1780,633,1781,636],{"href":632},[52,1782,220],{},[24,1784,1785],{},[389,1786,633,1788,1791],{"href":1787},"\u002Fstandard-library\u002Fjson.loads-function-explained",[52,1789,1790],{},"json.loads()"," explained",[24,1793,1794],{},[389,1795,633,1797,1791],{"href":1796},"\u002Fstandard-library\u002Fjson.dumps-function-explained",[52,1798,1799],{},"json.dumps()",[1801,1802,1803],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":50,"searchDepth":69,"depth":69,"links":1805},[1806,1807,1808,1809,1810,1815,1820,1823,1824,1827,1828,1829,1839],{"id":42,"depth":69,"text":43},{"id":265,"depth":69,"text":266},{"id":295,"depth":69,"text":296},{"id":325,"depth":69,"text":326},{"id":398,"depth":69,"text":399,"children":1811},[1812,1813],{"id":553,"depth":76,"text":554},{"id":593,"depth":76,"text":1814},"Why use json=?",{"id":639,"depth":69,"text":640,"children":1816},[1817,1818,1819],{"id":646,"depth":76,"text":647},{"id":711,"depth":76,"text":712},{"id":783,"depth":76,"text":784},{"id":824,"depth":69,"text":825,"children":1821},[1822],{"id":982,"depth":76,"text":983},{"id":1001,"depth":69,"text":1002},{"id":1202,"depth":69,"text":1203,"children":1825},[1826],{"id":1449,"depth":76,"text":1450},{"id":1473,"depth":69,"text":1474},{"id":1596,"depth":69,"text":1597},{"id":1643,"depth":69,"text":1644,"children":1830},[1831,1833,1834,1836,1837],{"id":1647,"depth":76,"text":1832},"What is the difference between data= and json= in requests.post()?",{"id":1673,"depth":76,"text":1674},{"id":1693,"depth":76,"text":1835},"Why does response.json() fail?",{"id":1729,"depth":76,"text":1730},{"id":1747,"depth":76,"text":1838},"Can I send a POST request without requests?",{"id":1763,"depth":69,"text":1764},"Master how to send a post request in python in our comprehensive Python beginner guide.","md",{},"\u002Fhow-to\u002Fhow-to-send-a-post-request-in-python",{"title":5,"description":1840},"how-to\u002Fhow-to-send-a-post-request-in-python","ZMLCQrc8qk-I6cAT1ZM5Syfdo53sP6KKANY0TsaoQ4o",1777585507484]