HTTP Client

HTTP Client library.

API Reference

Header File

Functions

int httpc_client_open(void)

Open a client

Return

On success, returns handle to http client, negative value on error

int httpc_client_close(int client)

Close a client

Return

0 on success, negative value on error

Parameters
  • client: [in] Client handle

int httpc_get(int client, httpc_arg *arg)

Perform HTTP Get

Return

0 on success, If server response is not 2xx then server error code is returned, if any operation issue negative error code will be returned

Parameters
  • client: [in] Client handle

  • arg: [in] HTTP client argument httpc_arg_t

int httpc_submit(int client, int method, httpc_arg *arg)

Perform HTTP Post, Put or Delete

Return

0 on success, If server response is not 2xx then server error code is returned, if any operation issue negative error code will be returned

Parameters
  • client: [in] Client handle

  • method: [in] HTTP Method httpc_method_e

  • arg: [in] HTTP client argument httpc_arg_t

int httpc_upload(const char *url, struct http_filemeta_t *meta, char *respbuf, int *buflen)

HTTP file upload. This function will perform a POST with “multipart/form-data” mime on the provided URL. Its a special case of httpc_submit

Return

0 on success, If server response is not 2xx then server error code is returned, if any operation issue negative error code will be returned

Parameters
  • url: [in] URL to post

  • meta: [in] File information structure http_filemeta_t

  • respbuf: [out] Response data buffer pointer

  • buflen: [out] size of response data, and returns actual data stored in response buffer

int httpc_download(const char *url, char *filename, http_download_cb httpc_cb)

HTTP file download to local storage

Return

0 on success, If server response is not 2xx then server error code is returned, if any operation issue negative error code will be returned

Parameters
  • url: [in] URL to download

  • filename: [in] path to filename where download file will be saved

  • httpc_cb: [in] Callback function called after download is finished see http_download_cb

int httpclient_set_keepalive(int client, int enable)

Enable or disable HTTP client keep-alive function. Keep-alive is enabled by default. If server returns a “connection: close” response then socket will be closed after execution

Return

0 on success, negative value on error

Parameters
  • client: [in] Client handle

  • enable: [in] TRUE or FALSE to enable or disable respectively.

int httpclient_get_keepalive(int client)

Get current HTTP client keep-alive configuration

Return

On success, configuration value is returned, negative value on error

Parameters
  • client: [in] Client handle

int urlencode(const unsigned char *in_buf, char *enc)

HTTP URL Encode data. Please make sure output buffer is enough to store encoded data

Return

0 on success

Parameters
  • in_buf: [in] Data to encode

  • enc: [out] Buffer to store encoded Data

int urldecode(const char *in_buf, char *dec)

HTTP URL Decode data. Please make sure output buffer is enough to store decoded data

Return

On success length of data stored in output buffer, negative value on error

Parameters
  • in_buf: [in] Input buffer containing encoded data

  • dec: [out] Buffere to store decoded data

Unions

union httpc_arg_t
#include <httpc.h>

HTTP Client argument

Public Members

struct _httpc_get_t get

HTTP Get _httpc_get_t for httpc_get

struct _httpc_post_t post

HTTP Post _httpc_post_t for httpc_submit

struct _httpc_up_t upload

HTTP Upload _httpc_up_t for httpc_upload

Structures

struct http_filemeta_t

HTTP File Upload information structure

Public Members

char *filename

File name (63 char max) to be uploaded with

char *filepath

Complete file path on storage media (255 max)

long timestamp

File creation UNIX timestamp

char *info

Other supplementary Info e.g. location info etc, This information is sent to server with HTTP form key “info” (127 max)

int filetype

MIME type to upload http_filetype_e for more information

struct _httpc_get_t

HTTP Client get argument

Public Members

const char *url

Client URL

const char *headers

Custom headers

struct ssl_certs_t *certs

SSL client certificates

int recv_headers

if TRUE, HTTP response headers are also stored in response buffer

char *resp_buffer

Pointer to response buffer

int buflen

Length of response buffer

struct _httpc_post_t

HTTP Client post argument

Public Members

const char *url

Client URL

const char *header

Custom headers

const unsigned char *submit_data

Post data

int submit_len

Length of post data

int mime

MIME type http_mimetype_e

struct ssl_certs_t *certs

SSL Client certificate

int recv_headers

if TRUE, HTTP response headers are also stored in response buffer

char *resp_buffer

Pointer to response buffer

int buflen

Length of response buffer

struct _httpc_up_t

HTTP Client upload argument

Public Members

const char *url

Client URL

const char *header

Custom headers

struct http_filemeta_t *meta

Upload file information http_filemeta_t

struct ssl_certs_t *certs

SSL Client certificates

char *respbuf

Pointer to response buffer

int buflen

Length of response buffer

Macros

HTTP_RECV_HEADER

Indicates data is a header

HTTP_CLIENT_DEFAULT

Default HTTP client

Type Definitions

typedef void (*http_download_cb)(unsigned int dl_size, unsigned int reserved, int error)

HTTP download finished callback

Parameters
  • dl_size: Download size

  • reserved: reserved

  • error: Error code if any

typedef union httpc_arg_t httpc_arg

HTTP Client argument

Enumerations

enum http_mimetype_e

HTTP supported MIME types

Values:

enumerator HTTP_MIME_TYPE_JPEG

For MIME type image/jpeg

enumerator HTTP_MIME_TYPE_PNG

For MIME type image/png

enumerator HTTP_MIME_TYPE_JSON

For MIME type application/json

enumerator HTTP_MIME_TYPE_CSV

For MIME type text/csv

enumerator HTTP_MIME_TYPE_TXT

For MIME type text/plain

enumerator HTTP_MIME_TYPE_BINARY

For MIME type application/octet-stream

enumerator HTTP_MIME_TYPE_AMR

For MIME type audio/amr

enumerator HTTP_MIME_TYPE_AAC

For MIME type audio/aac

enumerator HTTP_MIME_TYPE_WAV

For MIME type audio/wav

enumerator HTTP_MIME_TYPE_MPEG

For MIME type audio/mpeg

enumerator HTTP_MIME_TYPE_FORM

For MIME type application/x-www-form-urlencoded

enum httpup_status_e

HTTP Upload status

Values:

enumerator HTTPUP_STATUS_FAIL

Upload failed

enumerator HTTPUP_STATUS_SUCCESS

File uploaded success

enumerator HTTPUP_STATUS_RECVDATA

File upload response data

enumerator HTTPUP_SERVER_ERR

Upload server error

enum httpc_method_e

HTTP supported methods

Basic support for accessing REST API

Values:

enumerator HTTP_METHOD_GET

HTTP Method GET

enumerator HTTP_METHOD_POST

HTTP Method POST

enumerator HTTP_METHOD_PUT

HTTP Method PUT

enumerator HTTP_METHOD_DELETE

HTTP Method DELETE