httpclient.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #if defined(GLOBAL_DEBUG_ON)
  12. #define HTTPCLIENT_DEBUG_ON
  13. #endif
  14. #if defined(HTTPCLIENT_DEBUG_ON)
  15. #define HTTPCLIENT_DEBUG(format, ...) dbg_printf(format, ##__VA_ARGS__)
  16. #else
  17. #define HTTPCLIENT_DEBUG(format, ...)
  18. #endif
  19. #if defined(USES_SDK_BEFORE_V140)
  20. #define espconn_send espconn_sent
  21. #define espconn_secure_send espconn_secure_sent
  22. #endif
  23. /*
  24. * In case of TCP or DNS error the callback is called with this status.
  25. */
  26. #define HTTP_STATUS_GENERIC_ERROR (-1)
  27. /*
  28. * Size of http responses that will cause an error.
  29. */
  30. #define BUFFER_SIZE_MAX (0x2000)
  31. /*
  32. * Timeout of http request.
  33. */
  34. #define HTTP_REQUEST_TIMEOUT_MS (10000)
  35. /*
  36. * "full_response" is a string containing all response headers and the response body.
  37. * "response_body and "http_status" are extracted from "full_response" for convenience.
  38. *
  39. * A successful request corresponds to an HTTP status code of 200 (OK).
  40. * More info at http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  41. */
  42. typedef void (* http_callback_t)(char * response_body, int http_status, char * full_response);
  43. /*
  44. * Call this function to skip URL parsing if the arguments are already in separate variables.
  45. */
  46. 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);
  47. /*
  48. * Request data from URL use custom method.
  49. * The data should be encoded as any format.
  50. * Try:
  51. * http_request("http://httpbin.org/post", "OPTIONS", "Content-type: text/plain", "Hello world", http_callback_example, 0);
  52. */
  53. 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);
  54. /*
  55. * Post data to a web form.
  56. * The data should be encoded as any format.
  57. * Try:
  58. * http_post("http://httpbin.org/post", "Content-type: application/json", "{\"hello\": \"world\"}", http_callback_example);
  59. */
  60. void ICACHE_FLASH_ATTR http_post(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  61. /*
  62. * Download a web page from its URL.
  63. * Try:
  64. * http_get("http://wtfismyip.com/text", NULL, http_callback_example);
  65. */
  66. void ICACHE_FLASH_ATTR http_get(const char * url, const char * headers, http_callback_t callback_handle);
  67. /*
  68. * Delete a web page from its URL.
  69. * Try:
  70. * http_delete("http://wtfismyip.com/text", NULL, http_callback_example);
  71. */
  72. void ICACHE_FLASH_ATTR http_delete(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  73. /*
  74. * Update data to a web form.
  75. * The data should be encoded as any format.
  76. * Try:
  77. * http_put("http://httpbin.org/post", "Content-type: application/json", "{\"hello\": \"world\"}", http_callback_example);
  78. */
  79. void ICACHE_FLASH_ATTR http_put(const char * url, const char * headers, const char * post_data, http_callback_t callback_handle);
  80. /*
  81. * Output on the UART.
  82. */
  83. void http_callback_example(char * response, int http_status, char * full_response);
  84. #endif // __HTTPCLIENT_H__