esp_http_client.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _ESP_HTTP_CLIENT_H
  7. #define _ESP_HTTP_CLIENT_H
  8. #include "freertos/FreeRTOS.h"
  9. #include "sdkconfig.h"
  10. #include "esp_err.h"
  11. #include <sys/socket.h>
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #define DEFAULT_HTTP_BUF_SIZE (512)
  16. #include "esp_event.h"
  17. ESP_EVENT_DECLARE_BASE(ESP_HTTP_CLIENT_EVENT);
  18. typedef struct esp_http_client *esp_http_client_handle_t;
  19. typedef struct esp_http_client_event *esp_http_client_event_handle_t;
  20. /**
  21. * @brief HTTP Client events id
  22. */
  23. typedef enum {
  24. HTTP_EVENT_ERROR = 0, /*!< This event occurs when there are any errors during execution */
  25. HTTP_EVENT_ON_CONNECTED, /*!< Once the HTTP has been connected to the server, no data exchange has been performed */
  26. HTTP_EVENT_HEADERS_SENT, /*!< After sending all the headers to the server */
  27. HTTP_EVENT_HEADER_SENT = HTTP_EVENT_HEADERS_SENT, /*!< This header has been kept for backward compatability
  28. and will be deprecated in future versions esp-idf */
  29. HTTP_EVENT_ON_HEADER, /*!< Occurs when receiving each header sent from the server */
  30. HTTP_EVENT_ON_DATA, /*!< Occurs when receiving data from the server, possibly multiple portions of the packet */
  31. HTTP_EVENT_ON_FINISH, /*!< Occurs when finish a HTTP session */
  32. HTTP_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
  33. HTTP_EVENT_REDIRECT, /*!< Intercepting HTTP redirects to handle them manually */
  34. } esp_http_client_event_id_t;
  35. /**
  36. * @brief HTTP Client events data
  37. */
  38. typedef struct esp_http_client_event {
  39. esp_http_client_event_id_t event_id; /*!< event_id, to know the cause of the event */
  40. esp_http_client_handle_t client; /*!< esp_http_client_handle_t context */
  41. void *data; /*!< data of the event */
  42. int data_len; /*!< data length of data */
  43. void *user_data; /*!< user_data context, from esp_http_client_config_t user_data */
  44. char *header_key; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header key */
  45. char *header_value; /*!< For HTTP_EVENT_ON_HEADER event_id, it's store current http header value */
  46. } esp_http_client_event_t;
  47. /**
  48. * @brief Argument structure for HTTP_EVENT_ON_DATA event
  49. */
  50. typedef struct esp_http_client_on_data {
  51. esp_http_client_handle_t client; /*!< Client handle */
  52. int64_t data_process; /*!< Total data processed */
  53. } esp_http_client_on_data_t;
  54. /**
  55. * @brief Argument structure for HTTP_EVENT_REDIRECT event
  56. */
  57. typedef struct esp_http_client_redirect_event_data {
  58. esp_http_client_handle_t client; /*!< Client handle */
  59. int status_code; /*!< Status Code */
  60. } esp_http_client_redirect_event_data_t;
  61. /**
  62. * @brief HTTP Client transport
  63. */
  64. typedef enum {
  65. HTTP_TRANSPORT_UNKNOWN = 0x0, /*!< Unknown */
  66. HTTP_TRANSPORT_OVER_TCP, /*!< Transport over tcp */
  67. HTTP_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
  68. } esp_http_client_transport_t;
  69. /*
  70. * @brief TLS Protocol version
  71. */
  72. typedef enum {
  73. ESP_HTTP_CLIENT_TLS_VER_ANY = 0, /* No preference */
  74. ESP_HTTP_CLIENT_TLS_VER_TLS_1_2 = 0x1, /* (D)TLS 1.2 */
  75. ESP_HTTP_CLIENT_TLS_VER_TLS_1_3 = 0x2, /* (D)TLS 1.3 */
  76. ESP_HTTP_CLIENT_TLS_VER_MAX, /* to indicate max */
  77. } esp_http_client_proto_ver_t;
  78. typedef esp_err_t (*http_event_handle_cb)(esp_http_client_event_t *evt);
  79. /**
  80. * @brief HTTP method
  81. */
  82. typedef enum {
  83. HTTP_METHOD_GET = 0, /*!< HTTP GET Method */
  84. HTTP_METHOD_POST, /*!< HTTP POST Method */
  85. HTTP_METHOD_PUT, /*!< HTTP PUT Method */
  86. HTTP_METHOD_PATCH, /*!< HTTP PATCH Method */
  87. HTTP_METHOD_DELETE, /*!< HTTP DELETE Method */
  88. HTTP_METHOD_HEAD, /*!< HTTP HEAD Method */
  89. HTTP_METHOD_NOTIFY, /*!< HTTP NOTIFY Method */
  90. HTTP_METHOD_SUBSCRIBE, /*!< HTTP SUBSCRIBE Method */
  91. HTTP_METHOD_UNSUBSCRIBE,/*!< HTTP UNSUBSCRIBE Method */
  92. HTTP_METHOD_OPTIONS, /*!< HTTP OPTIONS Method */
  93. HTTP_METHOD_COPY, /*!< HTTP COPY Method */
  94. HTTP_METHOD_MOVE, /*!< HTTP MOVE Method */
  95. HTTP_METHOD_LOCK, /*!< HTTP LOCK Method */
  96. HTTP_METHOD_UNLOCK, /*!< HTTP UNLOCK Method */
  97. HTTP_METHOD_PROPFIND, /*!< HTTP PROPFIND Method */
  98. HTTP_METHOD_PROPPATCH, /*!< HTTP PROPPATCH Method */
  99. HTTP_METHOD_MKCOL, /*!< HTTP MKCOL Method */
  100. HTTP_METHOD_MAX,
  101. } esp_http_client_method_t;
  102. /**
  103. * @brief HTTP Authentication type
  104. */
  105. typedef enum {
  106. HTTP_AUTH_TYPE_NONE = 0, /*!< No authention */
  107. HTTP_AUTH_TYPE_BASIC, /*!< HTTP Basic authentication */
  108. HTTP_AUTH_TYPE_DIGEST, /*!< HTTP Disgest authentication */
  109. } esp_http_client_auth_type_t;
  110. /**
  111. * @brief HTTP configuration
  112. */
  113. typedef struct {
  114. const char *url; /*!< HTTP URL, the information on the URL is most important, it overrides the other fields below, if any */
  115. const char *host; /*!< Domain or IP as string */
  116. int port; /*!< Port to connect, default depend on esp_http_client_transport_t (80 or 443) */
  117. const char *username; /*!< Using for Http authentication */
  118. const char *password; /*!< Using for Http authentication */
  119. esp_http_client_auth_type_t auth_type; /*!< Http authentication type, see `esp_http_client_auth_type_t` */
  120. const char *path; /*!< HTTP Path, if not set, default is `/` */
  121. const char *query; /*!< HTTP query */
  122. const char *cert_pem; /*!< SSL server certification, PEM format as string, if the client requires to verify server */
  123. size_t cert_len; /*!< Length of the buffer pointed to by cert_pem. May be 0 for null-terminated pem */
  124. const char *client_cert_pem; /*!< SSL client certification, PEM format as string, if the server requires to verify client */
  125. size_t client_cert_len; /*!< Length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem */
  126. const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */
  127. size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
  128. const char *client_key_password; /*!< Client key decryption password string */
  129. size_t client_key_password_len; /*!< String length of the password pointed to by client_key_password */
  130. esp_http_client_proto_ver_t tls_version; /*!< TLS protocol version of the connection, e.g., TLS 1.2, TLS 1.3 (default - no preference) */
  131. #ifdef CONFIG_MBEDTLS_HARDWARE_ECDSA_SIGN
  132. bool use_ecdsa_peripheral; /*!< Use ECDSA peripheral to use private key. */
  133. uint8_t ecdsa_key_efuse_blk; /*!< The efuse block where ECDSA key is stored. */
  134. #endif
  135. const char *user_agent; /*!< The User Agent string to send with HTTP requests */
  136. esp_http_client_method_t method; /*!< HTTP Method */
  137. int timeout_ms; /*!< Network timeout in milliseconds */
  138. bool disable_auto_redirect; /*!< Disable HTTP automatic redirects */
  139. int max_redirection_count; /*!< Max number of redirections on receiving HTTP redirect status code, using default value if zero*/
  140. int max_authorization_retries; /*!< Max connection retries on receiving HTTP unauthorized status code, using default value if zero. Disables authorization retry if -1*/
  141. http_event_handle_cb event_handler; /*!< HTTP Event Handle */
  142. esp_http_client_transport_t transport_type; /*!< HTTP transport type, see `esp_http_client_transport_t` */
  143. int buffer_size; /*!< HTTP receive buffer size */
  144. int buffer_size_tx; /*!< HTTP transmit buffer size */
  145. void *user_data; /*!< HTTP user_data context */
  146. bool is_async; /*!< Set asynchronous mode, only supported with HTTPS for now */
  147. bool use_global_ca_store; /*!< Use a global ca_store for all the connections in which this bool is set. */
  148. bool skip_cert_common_name_check; /*!< Skip any validation of server certificate CN field */
  149. const char *common_name; /*!< Pointer to the string containing server certificate common name.
  150. If non-NULL, server certificate CN must match this name,
  151. If NULL, server certificate CN must match hostname. */
  152. esp_err_t (*crt_bundle_attach)(void *conf); /*!< Function pointer to esp_crt_bundle_attach. Enables the use of certification
  153. bundle for server verification, must be enabled in menuconfig */
  154. bool keep_alive_enable; /*!< Enable keep-alive timeout */
  155. int keep_alive_idle; /*!< Keep-alive idle time. Default is 5 (second) */
  156. int keep_alive_interval; /*!< Keep-alive interval time. Default is 5 (second) */
  157. int keep_alive_count; /*!< Keep-alive packet retry send count. Default is 3 counts */
  158. struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
  159. #if CONFIG_ESP_TLS_USE_SECURE_ELEMENT
  160. bool use_secure_element; /*!< Enable this option to use secure element */
  161. #endif
  162. #if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
  163. void *ds_data; /*!< Pointer for digital signature peripheral context, see ESP-TLS Documentation for more details */
  164. #endif
  165. } esp_http_client_config_t;
  166. /**
  167. * Enum for the HTTP status codes.
  168. */
  169. typedef enum {
  170. /* 2xx - Success */
  171. HttpStatus_Ok = 200,
  172. /* 3xx - Redirection */
  173. HttpStatus_MultipleChoices = 300,
  174. HttpStatus_MovedPermanently = 301,
  175. HttpStatus_Found = 302,
  176. HttpStatus_SeeOther = 303,
  177. HttpStatus_TemporaryRedirect = 307,
  178. HttpStatus_PermanentRedirect = 308,
  179. /* 4xx - Client Error */
  180. HttpStatus_BadRequest = 400,
  181. HttpStatus_Unauthorized = 401,
  182. HttpStatus_Forbidden = 403,
  183. HttpStatus_NotFound = 404,
  184. /* 5xx - Server Error */
  185. HttpStatus_InternalError = 500
  186. } HttpStatus_Code;
  187. #define ESP_ERR_HTTP_BASE (0x7000) /*!< Starting number of HTTP error codes */
  188. #define ESP_ERR_HTTP_MAX_REDIRECT (ESP_ERR_HTTP_BASE + 1) /*!< The error exceeds the number of HTTP redirects */
  189. #define ESP_ERR_HTTP_CONNECT (ESP_ERR_HTTP_BASE + 2) /*!< Error open the HTTP connection */
  190. #define ESP_ERR_HTTP_WRITE_DATA (ESP_ERR_HTTP_BASE + 3) /*!< Error write HTTP data */
  191. #define ESP_ERR_HTTP_FETCH_HEADER (ESP_ERR_HTTP_BASE + 4) /*!< Error read HTTP header from server */
  192. #define ESP_ERR_HTTP_INVALID_TRANSPORT (ESP_ERR_HTTP_BASE + 5) /*!< There are no transport support for the input scheme */
  193. #define ESP_ERR_HTTP_CONNECTING (ESP_ERR_HTTP_BASE + 6) /*!< HTTP connection hasn't been established yet */
  194. #define ESP_ERR_HTTP_EAGAIN (ESP_ERR_HTTP_BASE + 7) /*!< Mapping of errno EAGAIN to esp_err_t */
  195. #define ESP_ERR_HTTP_CONNECTION_CLOSED (ESP_ERR_HTTP_BASE + 8) /*!< Read FIN from peer and the connection closed */
  196. /**
  197. * @brief Start a HTTP session
  198. * This function must be the first function to call,
  199. * and it returns a esp_http_client_handle_t that you must use as input to other functions in the interface.
  200. * This call MUST have a corresponding call to esp_http_client_cleanup when the operation is complete.
  201. *
  202. * @param[in] config The configurations, see `http_client_config_t`
  203. *
  204. * @return
  205. * - `esp_http_client_handle_t`
  206. * - NULL if any errors
  207. */
  208. esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *config);
  209. /**
  210. * @brief Invoke this function after `esp_http_client_init` and all the options calls are made, and will perform the
  211. * transfer as described in the options. It must be called with the same esp_http_client_handle_t as input as the esp_http_client_init call returned.
  212. * esp_http_client_perform performs the entire request in either blocking or non-blocking manner. By default, the API performs request in a blocking manner and returns when done,
  213. * or if it failed, and in non-blocking manner, it returns if EAGAIN/EWOULDBLOCK or EINPROGRESS is encountered, or if it failed. And in case of non-blocking request,
  214. * the user may call this API multiple times unless request & response is complete or there is a failure. To enable non-blocking esp_http_client_perform(), `is_async` member of esp_http_client_config_t
  215. * must be set while making a call to esp_http_client_init() API.
  216. * You can do any amount of calls to esp_http_client_perform while using the same esp_http_client_handle_t. The underlying connection may be kept open if the server allows it.
  217. * If you intend to transfer more than one file, you are even encouraged to do so.
  218. * esp_http_client will then attempt to re-use the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources.
  219. * Just note that you will have to use `esp_http_client_set_**` between the invokes to set options for the following esp_http_client_perform.
  220. *
  221. * @note You must never call this function simultaneously from two places using the same client handle.
  222. * Let the function return first before invoking it another time.
  223. * If you want parallel transfers, you must use several esp_http_client_handle_t.
  224. * This function include `esp_http_client_open` -> `esp_http_client_write` -> `esp_http_client_fetch_headers` -> `esp_http_client_read` (and option) `esp_http_client_close`.
  225. *
  226. * @param client The esp_http_client handle
  227. *
  228. * @return
  229. * - ESP_OK on successful
  230. * - ESP_FAIL on error
  231. */
  232. esp_err_t esp_http_client_perform(esp_http_client_handle_t client);
  233. /**
  234. * @brief Cancel an ongoing HTTP request. This API closes the current socket and opens a new socket with the same esp_http_client context.
  235. *
  236. * @param client The esp_http_client handle
  237. * @return
  238. * - ESP_OK on successful
  239. * - ESP_FAIL on error
  240. * - ESP_ERR_INVALID_ARG
  241. * - ESP_ERR_INVALID_STATE
  242. */
  243. esp_err_t esp_http_client_cancel_request(esp_http_client_handle_t client);
  244. /**
  245. * @brief Set URL for client, when performing this behavior, the options in the URL will replace the old ones
  246. *
  247. * @param[in] client The esp_http_client handle
  248. * @param[in] url The url
  249. *
  250. * @return
  251. * - ESP_OK
  252. * - ESP_FAIL
  253. */
  254. esp_err_t esp_http_client_set_url(esp_http_client_handle_t client, const char *url);
  255. /**
  256. * @brief Set post data, this function must be called before `esp_http_client_perform`.
  257. * Note: The data parameter passed to this function is a pointer and this function will not copy the data
  258. *
  259. * @param[in] client The esp_http_client handle
  260. * @param[in] data post data pointer
  261. * @param[in] len post length
  262. *
  263. * @return
  264. * - ESP_OK
  265. * - ESP_FAIL
  266. */
  267. esp_err_t esp_http_client_set_post_field(esp_http_client_handle_t client, const char *data, int len);
  268. /**
  269. * @brief Get current post field information
  270. *
  271. * @param[in] client The client
  272. * @param[out] data Point to post data pointer
  273. *
  274. * @return Size of post data
  275. */
  276. int esp_http_client_get_post_field(esp_http_client_handle_t client, char **data);
  277. /**
  278. * @brief Set http request header, this function must be called after esp_http_client_init and before any
  279. * perform function
  280. *
  281. * @param[in] client The esp_http_client handle
  282. * @param[in] key The header key
  283. * @param[in] value The header value
  284. *
  285. * @return
  286. * - ESP_OK
  287. * - ESP_FAIL
  288. */
  289. esp_err_t esp_http_client_set_header(esp_http_client_handle_t client, const char *key, const char *value);
  290. /**
  291. * @brief Get http request header.
  292. * The value parameter will be set to NULL if there is no header which is same as
  293. * the key specified, otherwise the address of header value will be assigned to value parameter.
  294. * This function must be called after `esp_http_client_init`.
  295. *
  296. * @param[in] client The esp_http_client handle
  297. * @param[in] key The header key
  298. * @param[out] value The header value
  299. *
  300. * @return
  301. * - ESP_OK
  302. * - ESP_FAIL
  303. */
  304. esp_err_t esp_http_client_get_header(esp_http_client_handle_t client, const char *key, char **value);
  305. /**
  306. * @brief Get http request username.
  307. * The address of username buffer will be assigned to value parameter.
  308. * This function must be called after `esp_http_client_init`.
  309. *
  310. * @param[in] client The esp_http_client handle
  311. * @param[out] value The username value
  312. *
  313. * @return
  314. * - ESP_OK
  315. * - ESP_ERR_INVALID_ARG
  316. */
  317. esp_err_t esp_http_client_get_username(esp_http_client_handle_t client, char **value);
  318. /**
  319. * @brief Set http request username.
  320. * The value of username parameter will be assigned to username buffer.
  321. * If the username parameter is NULL then username buffer will be freed.
  322. *
  323. * @param[in] client The esp_http_client handle
  324. * @param[in] username The username value
  325. *
  326. * @return
  327. * - ESP_OK
  328. * - ESP_ERR_INVALID_ARG
  329. */
  330. esp_err_t esp_http_client_set_username(esp_http_client_handle_t client, const char *username);
  331. /**
  332. * @brief Get http request password.
  333. * The address of password buffer will be assigned to value parameter.
  334. * This function must be called after `esp_http_client_init`.
  335. *
  336. * @param[in] client The esp_http_client handle
  337. * @param[out] value The password value
  338. *
  339. * @return
  340. * - ESP_OK
  341. * - ESP_ERR_INVALID_ARG
  342. */
  343. esp_err_t esp_http_client_get_password(esp_http_client_handle_t client, char **value);
  344. /**
  345. * @brief Set http request password.
  346. * The value of password parameter will be assigned to password buffer.
  347. * If the password parameter is NULL then password buffer will be freed.
  348. *
  349. * @param[in] client The esp_http_client handle
  350. * @param[in] password The password value
  351. *
  352. * @return
  353. * - ESP_OK
  354. * - ESP_ERR_INVALID_ARG
  355. */
  356. esp_err_t esp_http_client_set_password(esp_http_client_handle_t client, const char *password);
  357. /**
  358. * @brief Set http request auth_type.
  359. *
  360. * @param[in] client The esp_http_client handle
  361. * @param[in] auth_type The esp_http_client auth type
  362. *
  363. * @return
  364. * - ESP_OK
  365. * - ESP_ERR_INVALID_ARG
  366. */
  367. esp_err_t esp_http_client_set_authtype(esp_http_client_handle_t client, esp_http_client_auth_type_t auth_type);
  368. /**
  369. * @brief Get http request user_data.
  370. * The value stored from the esp_http_client_config_t will be written
  371. * to the address passed into data.
  372. *
  373. * @param[in] client The esp_http_client handle
  374. * @param[out] data A pointer to the pointer that will be set to user_data.
  375. *
  376. * @return
  377. * - ESP_OK
  378. * - ESP_ERR_INVALID_ARG
  379. */
  380. esp_err_t esp_http_client_get_user_data(esp_http_client_handle_t client, void **data);
  381. /**
  382. * @brief Set http request user_data.
  383. * The value passed in +data+ will be available during event callbacks.
  384. * No memory management will be performed on the user's behalf.
  385. *
  386. * @param[in] client The esp_http_client handle
  387. * @param[in] data The pointer to the user data
  388. *
  389. * @return
  390. * - ESP_OK
  391. * - ESP_ERR_INVALID_ARG
  392. */
  393. esp_err_t esp_http_client_set_user_data(esp_http_client_handle_t client, void *data);
  394. /**
  395. * @brief Get HTTP client session errno
  396. *
  397. * @param[in] client The esp_http_client handle
  398. *
  399. * @return
  400. * - (-1) if invalid argument
  401. * - errno
  402. */
  403. int esp_http_client_get_errno(esp_http_client_handle_t client);
  404. /**
  405. * @brief Set http request method
  406. *
  407. * @param[in] client The esp_http_client handle
  408. * @param[in] method The method
  409. *
  410. * @return
  411. * - ESP_OK
  412. * - ESP_ERR_INVALID_ARG
  413. */
  414. esp_err_t esp_http_client_set_method(esp_http_client_handle_t client, esp_http_client_method_t method);
  415. /**
  416. * @brief Set http request timeout
  417. *
  418. * @param[in] client The esp_http_client handle
  419. * @param[in] timeout_ms The timeout value
  420. *
  421. * @return
  422. * - ESP_OK
  423. * - ESP_ERR_INVALID_ARG
  424. */
  425. esp_err_t esp_http_client_set_timeout_ms(esp_http_client_handle_t client, int timeout_ms);
  426. /**
  427. * @brief Delete http request header
  428. *
  429. * @param[in] client The esp_http_client handle
  430. * @param[in] key The key
  431. *
  432. * @return
  433. * - ESP_OK
  434. * - ESP_FAIL
  435. */
  436. esp_err_t esp_http_client_delete_header(esp_http_client_handle_t client, const char *key);
  437. /**
  438. * @brief This function will be open the connection, write all header strings and return
  439. *
  440. * @param[in] client The esp_http_client handle
  441. * @param[in] write_len HTTP Content length need to write to the server
  442. *
  443. * @return
  444. * - ESP_OK
  445. * - ESP_FAIL
  446. */
  447. esp_err_t esp_http_client_open(esp_http_client_handle_t client, int write_len);
  448. /**
  449. * @brief This function will write data to the HTTP connection previously opened by esp_http_client_open()
  450. *
  451. * @param[in] client The esp_http_client handle
  452. * @param buffer The buffer
  453. * @param[in] len This value must not be larger than the write_len parameter provided to esp_http_client_open()
  454. *
  455. * @return
  456. * - (-1) if any errors
  457. * - Length of data written
  458. */
  459. int esp_http_client_write(esp_http_client_handle_t client, const char *buffer, int len);
  460. /**
  461. * @brief This function need to call after esp_http_client_open, it will read from http stream, process all receive headers
  462. *
  463. * @param[in] client The esp_http_client handle
  464. *
  465. * @return
  466. * - (0) if stream doesn't contain content-length header, or chunked encoding (checked by `esp_http_client_is_chunked` response)
  467. * - (-1: ESP_FAIL) if any errors
  468. * - (-ESP_ERR_HTTP_EAGAIN = -0x7007) if call is timed-out before any data was ready
  469. * - Download data length defined by content-length header
  470. */
  471. int64_t esp_http_client_fetch_headers(esp_http_client_handle_t client);
  472. /**
  473. * @brief Check response data is chunked
  474. *
  475. * @param[in] client The esp_http_client handle
  476. *
  477. * @return true or false
  478. */
  479. bool esp_http_client_is_chunked_response(esp_http_client_handle_t client);
  480. /**
  481. * @brief Read data from http stream
  482. *
  483. * @param[in] client The esp_http_client handle
  484. * @param buffer The buffer
  485. * @param[in] len The length
  486. *
  487. * @return
  488. * - (-1) if any errors
  489. * - Length of data was read
  490. *
  491. * @note (-ESP_ERR_HTTP_EAGAIN = -0x7007) is returned when call is timed-out before any data was ready
  492. */
  493. int esp_http_client_read(esp_http_client_handle_t client, char *buffer, int len);
  494. /**
  495. * @brief Get http response status code, the valid value if this function invoke after `esp_http_client_perform`
  496. *
  497. * @param[in] client The esp_http_client handle
  498. *
  499. * @return Status code
  500. */
  501. int esp_http_client_get_status_code(esp_http_client_handle_t client);
  502. /**
  503. * @brief Get http response content length (from header Content-Length)
  504. * the valid value if this function invoke after `esp_http_client_perform`
  505. *
  506. * @param[in] client The esp_http_client handle
  507. *
  508. * @return
  509. * - (-1) Chunked transfer
  510. * - Content-Length value as bytes
  511. */
  512. int64_t esp_http_client_get_content_length(esp_http_client_handle_t client);
  513. /**
  514. * @brief Close http connection, still kept all http request resources
  515. *
  516. * @param[in] client The esp_http_client handle
  517. *
  518. * @return
  519. * - ESP_OK
  520. * - ESP_FAIL
  521. */
  522. esp_err_t esp_http_client_close(esp_http_client_handle_t client);
  523. /**
  524. * @brief This function must be the last function to call for an session.
  525. * It is the opposite of the esp_http_client_init function and must be called with the same handle as input that a esp_http_client_init call returned.
  526. * This might close all connections this handle has used and possibly has kept open until now.
  527. * Don't call this function if you intend to transfer more files, re-using handles is a key to good performance with esp_http_client.
  528. *
  529. * @param[in] client The esp_http_client handle
  530. *
  531. * @return
  532. * - ESP_OK
  533. * - ESP_FAIL
  534. */
  535. esp_err_t esp_http_client_cleanup(esp_http_client_handle_t client);
  536. /**
  537. * @brief Get transport type
  538. *
  539. * @param[in] client The esp_http_client handle
  540. *
  541. * @return
  542. * - HTTP_TRANSPORT_UNKNOWN
  543. * - HTTP_TRANSPORT_OVER_TCP
  544. * - HTTP_TRANSPORT_OVER_SSL
  545. */
  546. esp_http_client_transport_t esp_http_client_get_transport_type(esp_http_client_handle_t client);
  547. /**
  548. * @brief Set redirection URL.
  549. * When received the 30x code from the server, the client stores the redirect URL provided by the server.
  550. * This function will set the current URL to redirect to enable client to execute the redirection request.
  551. * When `disable_auto_redirect` is set, the client will not call this function but the event `HTTP_EVENT_REDIRECT` will be dispatched giving the user contol over the redirection event.
  552. *
  553. * @param[in] client The esp_http_client handle
  554. *
  555. * @return
  556. * - ESP_OK
  557. * - ESP_FAIL
  558. */
  559. esp_err_t esp_http_client_set_redirection(esp_http_client_handle_t client);
  560. /**
  561. * @brief On receiving a custom authentication header, this API can be invoked to set the
  562. * authentication information from the header. This API can be called from the event
  563. * handler.
  564. *
  565. * @param[in] client The esp_http_client handle
  566. * @param[in] auth_data The authentication data received in the header
  567. * @param[in] len length of auth_data.
  568. *
  569. * @return
  570. * - ESP_ERR_INVALID_ARG
  571. * - ESP_OK
  572. */
  573. esp_err_t esp_http_client_set_auth_data(esp_http_client_handle_t client, const char *auth_data, int len);
  574. /**
  575. * @brief On receiving HTTP Status code 401, this API can be invoked to add authorization
  576. * information.
  577. *
  578. * @note There is a possibility of receiving body message with redirection status codes, thus make sure
  579. * to flush off body data after calling this API.
  580. *
  581. * @param[in] client The esp_http_client handle
  582. */
  583. void esp_http_client_add_auth(esp_http_client_handle_t client);
  584. /**
  585. * @brief Checks if entire data in the response has been read without any error.
  586. *
  587. * @param[in] client The esp_http_client handle
  588. *
  589. * @return
  590. * - true
  591. * - false
  592. */
  593. bool esp_http_client_is_complete_data_received(esp_http_client_handle_t client);
  594. /**
  595. * @brief Helper API to read larger data chunks
  596. * This is a helper API which internally calls `esp_http_client_read` multiple times till the end of data is reached or till the buffer gets full.
  597. *
  598. * @param[in] client The esp_http_client handle
  599. * @param buffer The buffer
  600. * @param[in] len The buffer length
  601. *
  602. * @return
  603. * - Length of data was read
  604. */
  605. int esp_http_client_read_response(esp_http_client_handle_t client, char *buffer, int len);
  606. /**
  607. * @brief Process all remaining response data
  608. * This uses an internal buffer to repeatedly receive, parse, and discard response data until complete data is processed.
  609. * As no additional user-supplied buffer is required, this may be preferrable to `esp_http_client_read_response` in situations where the content of the response may be ignored.
  610. *
  611. * @param[in] client The esp_http_client handle
  612. * @param len Length of data discarded
  613. *
  614. * @return
  615. * - ESP_OK If successful, len will have discarded length
  616. * - ESP_FAIL If failed to read response
  617. * - ESP_ERR_INVALID_ARG If the client is NULL
  618. */
  619. esp_err_t esp_http_client_flush_response(esp_http_client_handle_t client, int *len);
  620. /**
  621. * @brief Get URL from client
  622. *
  623. * @param[in] client The esp_http_client handle
  624. * @param[inout] url The buffer to store URL
  625. * @param[in] len The buffer length
  626. *
  627. * @return
  628. * - ESP_OK
  629. * - ESP_FAIL
  630. */
  631. esp_err_t esp_http_client_get_url(esp_http_client_handle_t client, char *url, const int len);
  632. /**
  633. * @brief Get Chunk-Length from client
  634. *
  635. * @param[in] client The esp_http_client handle
  636. * @param[out] len Variable to store length
  637. *
  638. * @return
  639. * - ESP_OK If successful, len will have length of current chunk
  640. * - ESP_FAIL If the server is not a chunked server
  641. * - ESP_ERR_INVALID_ARG If the client or len are NULL
  642. */
  643. esp_err_t esp_http_client_get_chunk_length(esp_http_client_handle_t client, int *len);
  644. #ifdef __cplusplus
  645. }
  646. #endif
  647. #endif