You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

main.cpp 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "mbed.h"
  2. #include "test_env.h"
  3. #include "EthernetInterface.h"
  4. #include "HTTPClient.h"
  5. namespace {
  6. const int BUFFER_SIZE = 512;
  7. }
  8. int main() {
  9. MBED_HOSTTEST_TIMEOUT(15);
  10. MBED_HOSTTEST_SELECT(default_auto);
  11. MBED_HOSTTEST_DESCRIPTION(HTTP client hello world);
  12. MBED_HOSTTEST_START("NET_7");
  13. char http_request_buffer[BUFFER_SIZE + 1] = {0};
  14. HTTPClient http;
  15. EthernetInterface eth;
  16. eth.init(); //Use DHCP
  17. eth.connect();
  18. //GET data
  19. {
  20. bool result = true;
  21. const char *url_hello_txt = "http://developer.mbed.org/media/uploads/donatien/hello.txt";
  22. printf("HTTP_GET: Trying to fetch page '%s'...\r\n", url_hello_txt);
  23. HTTPResult ret = http.get(url_hello_txt, http_request_buffer, BUFFER_SIZE);
  24. if (ret == HTTP_OK) {
  25. printf("HTTP_GET: Read %d chars: '%s' ... [OK]\r\n", strlen(http_request_buffer), http_request_buffer);
  26. } else {
  27. printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
  28. result = false;
  29. }
  30. if (result == false) {
  31. eth.disconnect();
  32. MBED_HOSTTEST_RESULT(false);
  33. }
  34. }
  35. //POST data
  36. {
  37. bool result = true;
  38. const char *url_httpbin_post = "http://httpbin.org/post";
  39. HTTPText text(http_request_buffer, BUFFER_SIZE);
  40. HTTPMap map;
  41. map.put("Hello", "World");
  42. map.put("test", "1234");
  43. printf("HTTP_POST: Trying to post data to '%s' ...\r\n", url_httpbin_post);
  44. HTTPResult ret = http.post(url_httpbin_post, map, &text);
  45. if (ret == HTTP_OK) {
  46. printf("HTTP_POST: Read %d chars ... [OK]\r\n", strlen(http_request_buffer));
  47. printf("HTTP_POST: %s\r\n", http_request_buffer);
  48. } else {
  49. printf("HTTP_GET: Error(%d). HTTP error(%d) ... [FAIL]\r\n", ret, http.getHTTPResponseCode());
  50. result = false;
  51. }
  52. if (result == false) {
  53. eth.disconnect();
  54. MBED_HOSTTEST_RESULT(false);
  55. }
  56. }
  57. eth.disconnect();
  58. MBED_HOSTTEST_RESULT(true);
  59. }