httpclient.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * Martin d'Allens <martin.dallens@gmail.com> wrote this file. As long as you retain
  5. * this notice you can do whatever you want with this stuff. If we meet some day,
  6. * and you think this stuff is worth it, you can buy me a beer in return.
  7. * ----------------------------------------------------------------------------
  8. */
  9. #ifndef __HTTPCLIENT_H__
  10. #define __HTTPCLIENT_H__
  11. static const char log_prefix[] = "HTTP client: ";
  12. #if defined(DEVELOP_VERSION)
  13. #define HTTPCLIENT_DEBUG_ON
  14. #endif
  15. #if defined(HTTPCLIENT_DEBUG_ON)
  16. #define HTTPCLIENT_DEBUG(format, ...) dbg_printf("%s"format"\n", log_prefix, ##__VA_ARGS__)
  17. #else
  18. #define HTTPCLIENT_DEBUG(...)
  19. #endif
  20. #if defined(NODE_ERROR)
  21. #define HTTPCLIENT_ERR(format, ...) NODE_ERR("%s"format"\n", log_prefix, ##__VA_ARGS__)
  22. #else
  23. #define HTTPCLIENT_ERR(...)
  24. #endif
  25. #if defined(USES_SDK_BEFORE_V140)
  26. #define espconn_send espconn_sent
  27. #define espconn_secure_send espconn_secure_sent
  28. #endif
  29. /*
  30. * In case of TCP or DNS error the callback is called with this status.
  31. */
  32. #define HTTP_STATUS_GENERIC_ERROR (-1)
  33. /*
  34. * Size of http responses that will cause an error.
  35. */
  36. #define BUFFER_SIZE_MAX (0x2000)
  37. /*
  38. * Timeout of http request.
  39. */
  40. #define HTTP_REQUEST_TIMEOUT_MS (60000)
  41. /*
  42. * "full_response" is a string containing all response headers and the response body.
  43. * "response_body and "http_status" are extracted from "full_response" for convenience.
  44. *
  45. * A successful request corresponds to an HTTP status code of 200 (OK).
  46. * More info at http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  47. */
  48. typedef void (* http_callback_t)(char * response_body, int http_status, char ** full_response_p, int body_size);
  49. /*
  50. * Call this function to skip URL parsing if the arguments are already in separate variables.
  51. */
  52. void ICACHE_FLASH_ATTR http_raw_request(const char * hostname, int port, bool secure, const char * method, const char * path, const char * headers, const char * post_data, http_callback_t callback_handle, int redirect_follow_count);
  53. /*
  54. * Request data from URL use custom method.
  55. * The data should be encoded as any format.
  56. * Try:
  57. * http_request("http://httpbin.org/post", "OPTIONS", "Content-type: text/plain", "Hello world", http_callback_example, 0);
  58. */
  59. void ICACHE_FLASH_ATTR http_request(const char * url, const char * method, const char * headers, const char * post_data, http_callback_t callback_handle, int redirect_follow_count);
  60. /*
  61. * Post data to a web form.
  62. * The data should be encoded as any format.
  63. * Try:
  64. * http_post("http://httpbin.org/post", "Content-type: application/json", "{\"hello\": \"world\"}", http_callback_example);
  65. */
  66. void ICACHE_FLASH_ATTR http_post(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  67. /*
  68. * Download a web page from its URL.
  69. * Try:
  70. * http_get("http://wtfismyip.com/text", NULL, http_callback_example);
  71. */
  72. void ICACHE_FLASH_ATTR http_get(const char * url, const char * headers, http_callback_t callback_handle);
  73. /*
  74. * Delete a web page from its URL.
  75. * Try:
  76. * http_delete("http://wtfismyip.com/text", NULL, http_callback_example);
  77. */
  78. void ICACHE_FLASH_ATTR http_delete(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  79. /*
  80. * Update data to a web form.
  81. * The data should be encoded as any format.
  82. * Try:
  83. * http_put("http://httpbin.org/post", "Content-type: application/json", "{\"hello\": \"world\"}", http_callback_example);
  84. */
  85. void ICACHE_FLASH_ATTR http_put(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  86. /*
  87. * Output on the UART.
  88. */
  89. void http_callback_example(char * response, int http_status, char * full_response);
  90. #endif // __HTTPCLIENT_H__