Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 7 months ago.
using POST for HTTPclient
I have a basic HTML website
this is my <form> code
<form action="post.php" method="GET"> Message: <input type="text" name="message" /> <input type="submit" name="submit" value="send"> </form>
everything is working fine. Im able to Post on my form via web URL
http://192.168.1.54/test/post.php?message=message&submit=send
I want to do the same thing on mbed, and im currently using HTTPClient library, but i cant seem to get it work. below is my code.
char str[512]; char ret; const char *DATA = "Test&submit=send"; //POST data HTTPMap map; HTTPText inText(str, 512); map.put("message", DATA); printf("\nSending Post...\n"); ret = http.post("http://192.168.1.54/test/post.php?", map, &inText); if (!ret) { printf("Executed POST successfully - read %d characters\n", strlen(str)); printf("Result: %s\n", str); } else { printf("Error - ret = %d - HTTP return code = %d\n", str, http.getHTTPResponseCode()); }
and im getting this on my debug
Sending Post... Executed POST successfully - read 11 characters Result: invalid // the reply i get from my webpage post.php
1 Answer
11 years, 7 months ago.
i know this is kinda weird, answering my own question. but i manage to solve my own problem. to use HTTPClient POST on a website.
i really didnt have good background on programming, it was hard for me to use the official HTTPClient library and customize it on my specific need. [that] or maybe i just didnt know how to use it correctly. nevertheless, i ended up with this http://mbed.org/users/okini3939/code/TinyHTTP/
the most important part for me is to be able to include a content type in a HTTPpost, in which the above link provides.
/** send http request * @param method METHOD_GET or METHOD_POST * @param host http server * @param uri URI * @param head http header (CR+LF) (or NULL) * @param body POST body (or NULL) * @return http code, -1:failue */ int httpRequest (int method, Host *host, char *uri, char *head, char *body);
change the values specific to your requirements.
host.setName("192.168.1.54"); r = httpRequest(METHOD_POST, &host, "/mbedtest/post.php", "Content-Type: application/x-www-form-urlencoded\r\n", "message=input by a micro-controller &submit=send");
and here is my post.php
<?php include_once("connect.php"); if (isset($_POST['submit'])) { if ($_POST['message'] == "") { echo " no input, return"; exit(); } else { $message = $_POST['message']; mysql_query("insert into data (message) values ('$message')"); header ('location:index.php'); exit (); } } else { echo "invalid"; } ?>
this is my backend MYSQL database. you can see the POST sent by mbed
now you can send HTTPpost on your website, or even use it to update facebook/twitter status via PHP SDK :)
Not weird at all, good you do it, that way others with the same issue who run into this question also know the answer!
Nothing more irritating than if you have a problem, and while googling for the answer you run into a forumpost of someone else with the same issue, who then mentions he solved it but didn't say how he solved it.
posted by 09 Aug 2013
Hi,
Can you send me the full code to my mail ID
msarun003@gmail.com
Thanks in Advance :-)
posted by M.S. Arun 07 Mar 2015