esp_http_client.h 25 KB

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