Cross-platform C SDK logo

Cross-platform C SDK

HTTP

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Header

#include <inet/httpreq.h>


Functions

Http*http_create (...)
Http*http_secure (...)
voidhttp_destroy (...)
voidhttp_clear_headers (...)
voidhttp_add_header (...)
voidhttp_cookies_policy (...)
voidhttp_cookies_reload (...)
uint32_thttp_cookies_size (...)
const char_t*http_cookie_name (...)
const char_t*http_cookie_value (...)
const char_t*http_cookie_search (...)
voidhttp_cookie_delete (...)
voidhttp_cookie_delete_all (...)
bool_thttp_get (...)
bool_thttp_post (...)
bool_thttp_put (...)
bool_thttp_patch (...)
bool_thttp_delete (...)
uint32_thttp_response_status (...)
const char_t*http_response_protocol (...)
const char_t*http_response_message (...)
uint32_thttp_response_size (...)
const char_t*http_response_name (...)
const char_t*http_response_value (...)
const char_t*http_response_header (...)
bool_thttp_response_body (...)
Stream*http_dget (...)
bool_thttp_exists (...)

It is common for an application to need information beyond that stored on the computer itself. The simplest and most common way to share information is to store it on a Web Server and publish a URL that provides the desired content (Figure 1). This client/server scheme uses the HTTP/HTTPS protocol, which was originally designed to transmit HTML documents between Web servers and browsers. Due to the great impact it has had over the years, its use has been expanding for the exchange of structured information between any application that "understands" HTTP. The response from the server will typically be a block of text formatted in JSON or XML.

HTTP protocol, basic operating scheme.
Figure 1: Request for a remote resource using HTTP.

1. HTTP sessions

If we are going to make successive calls to the same server or if we need greater control over the HTTP headers, we must create a session (Listing 2). On the same session, we can make different requests using the typical HTTP "verbs": GET, POST, PUT, PATCH, DELETE. In addition to the body (or payload) of the HTTP request, a series of headers are sent that include additional information for the server. By default, the typical headers expected by most servers are included, but it is possible to include additional headers.

Listing 2: HTTP Session.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Stream *webpage = NULL;

const byte_t *body = ...
uint32_t size = ...
Http *http = http_secure("myserver.com", UINT16_MAX);
http_add_header(http, "MyHeader", "CustomValue");
if (http_post(http, "/en/start/win_mac_linux.html", body, size, NULL) == TRUE)
{
    if (http_response_status(http) == 200)
    {
        webpage = stm_memory(1024);
        if (http_response_body(http, webpage, NULL) == FALSE)
            stm_close(&webpage);
    }
}

http_destroy(&http);

if (webpage != NULL)
{
    ...
    stm_close(&webpage);
}

2. HTTP response

The response that the server sends after a call to http_get has a format similar to that of the request:

  • Status code: 200, 400, 500, etc.
  • Response body: Block of bytes.
  • Response headers: Key/value pairs with additional information.

3. HTTP Cookies

HTTP cookies are small pieces of information that a web server sends to the client (usually the Web browser) to store data and maintain state between requests. In summary, work like this (Figure 2):

  • The server sends a cookie using the Set-Cookie HTTP header in the response.
  • The client saves the cookie in its local storage according to the domain and path rules.
  • Every time the client makes a new HTTP request to the same domain and route, it automatically sends the corresponding cookies in the Cookie header.

By default, cookie management is done automatically and transparently for the application using the ekCOOKIES_ALL policy. If necessary, we have methods to read and delete cookies associated with a session. If we want greater control over the management, storage and sending of cookies to the server, we can disable automatic management using the ekCOOKIES_OFF policy, leaving the processing of the Set-Cookie and Cookie headers in the hands of the application itself.

Basic diagram of how HTTP cookies work.
Figure 2: How HTTP cookies work.
❮ Back
Next ❯

http_create ()

Create an HTTP session.

Http*
http_create(const char_t *host,
            const uint16_t port);
host

Server name.

port

Connection port. If we pass UINT16_MAX 80 will be used (default for HTTP).

Return

HTTP session.

Remarks

See HTTP sessions.


http_secure ()

Create an HTTPS session.

Http*
http_secure(const char_t *host,
            const uint16_t port);
host

Server name.

port

Connection port. If we pass UINT16_MAX 413 will be used (default for HTTPS).

Return

HTTPS session.

Remarks

See HTTP sessions.


http_destroy ()

Destroys an HTTP session.

void
http_destroy(Http **http);
http

The HTTP session. It will be set to NULL upon destruction.


http_clear_headers ()

Removes previously assigned HTTP headers.

void
http_clear_headers(Http *http);
http

HTTP session.

Remarks

See HTTP sessions.


http_add_header ()

Add a header to the HTTP request.

void
http_add_header(Http *http,
                const char_t *name,
                const char_t *value);
http

HTTP session.

name

The name of the header.

value

The value of the header.

Remarks

See HTTP sessions.


http_cookies_policy ()

Change the cookie policy.

void
http_cookies_policy(Http *http,
                    const cookies_t cookies);
http

HTTP session.

cookies

The policy to apply.

Remarks

The set policy will be applied in future calls to http_get or similar. By default ekCOOKIES_ALL is applied. See HTTP Cookies.


http_cookies_reload ()

Reload the cookie cache.

void
http_cookies_reload(Http *http);
http

HTTP session.

Remarks

We must call this function before accessing the content of the cookies. In ekCOOKIES_OFF policies the cache will always be empty. See HTTP Cookies.


http_cookies_size ()

Returns the number of cookies associated with this session.

uint32_t
http_cookies_size(const Http *http);
http

HTTP session.

Return

The number of cookies.

Remarks

Call before http_cookies_reload. See HTTP Cookies.


http_cookie_name ()

Returns the name of a cookie associated with this session.

const char_t*
http_cookie_name(const Http *http,
                 const uint32_t index);
http

HTTP session.

index

Cookie index (less than http_cookies_size).

Return

Cookie name.

Remarks

Call before http_cookies_reload. See HTTP Cookies.


http_cookie_value ()

Returns the value of a cookie associated with this session.

const char_t*
http_cookie_value(const Http *http,
                  const uint32_t index);
http

HTTP session.

index

Cookie index (less than http_cookies_size).

Return

Cookie value.

Remarks

Call before http_cookies_reload. See HTTP Cookies.


http_cookie_search ()

Returns the value of a cookie, given its name.

const char_t*
http_cookie_search(const Http *http,
                   const char_t *name);
http

HTTP session.

name

Cookie name.

Return

Cookie value or empty string if the cookie does not exist.

Remarks

Call before http_cookies_reload. See HTTP Cookies.


http_cookie_delete ()

Delete a cookie associated with this session.

void
http_cookie_delete(Http *http,
                   const char_t *name);
http

HTTP session.

name

Cookie name.

Remarks

See HTTP Cookies.


http_cookie_delete_all ()

Delete all cookies associated with this session.

void
http_cookie_delete_all(Http *http);
http

HTTP session.

Remarks

See HTTP Cookies.


http_get ()

Make a GET type request.

bool_t
http_get(Http *http,
         const char_t *path,
         const byte_t *data,
         const uint32_t size,
         ierror_t *error);
http

HTTP session.

path

Resource path.

data

Data to add to the body of the request. It can be NULL.

size

Size in bytes of the data block.

error

Error code if the function fails. It can be NULL.

Return

TRUE if the request has been carried out correctly. If FALSE, in error we will have the cause.

Remarks

The request is synchronous, that is, the program will be stopped until the server responds. If we want an asynchronous model we must create a parallel thread that manages the request. HTTP redirects are resolved automatically. See HTTP sessions.


http_post ()

Make a POST type request.

bool_t
http_post(Http *http,
          const char_t *path,
          const byte_t *data,
          const uint32_t size,
          ierror_t *error);
http

HTTP session.

path

Resource path.

data

Data to add to the body of the request. It can be NULL.

size

Size in bytes of the data block.

error

Error code if the function fails. It can be NULL.

Return

TRUE if the request has been carried out correctly. If FALSE, in error we will have the cause.

Remarks

See http_get.


http_put ()

Make a PUT type request.

bool_t
http_put(Http *http,
         const char_t *path,
         const byte_t *data,
         const uint32_t size,
         ierror_t *error);
http

HTTP session.

path

Resource path.

data

Data to add to the body of the request. It can be NULL.

size

Size in bytes of the data block.

error

Error code if the function fails. It can be NULL.

Return

TRUE if the request has been carried out correctly. If FALSE, in error we will have the cause.

Remarks

See http_get.


http_patch ()

Make a PATCH type request.

bool_t
http_patch(Http *http,
           const char_t *path,
           const byte_t *data,
           const uint32_t size,
           ierror_t *error);
http

HTTP session.

path

Resource path.

data

Data to add to the body of the request. It can be NULL.

size

Size in bytes of the data block.

error

Error code if the function fails. It can be NULL.

Return

TRUE if the request has been carried out correctly. If FALSE, in error we will have the cause.

Remarks

See http_get.


http_delete ()

Make a DELETE type request.

bool_t
http_delete(Http *http,
            const char_t *path,
            const byte_t *data,
            const uint32_t size,
            ierror_t *error);
http

HTTP session.

path

Resource path.

data

Data to add to the body of the request. It can be NULL.

size

Size in bytes of the data block.

error

Error code if the function fails. It can be NULL.

Return

TRUE if the request has been carried out correctly. If FALSE, in error we will have the cause.

Remarks

See http_get.


http_response_status ()

Returns the response code of an HTTP request.

uint32_t
http_response_status(const Http *http);
http

HTTP session.

Return

The server response code.

Remarks

See HTTP response.


http_response_protocol ()

Returns the protocol used by the HTTP server.

const char_t*
http_response_protocol(const Http *http);
http

HTTP session.

Return

The server protocol.

Remarks

See HTTP response.


http_response_message ()

Returns the response message from the HTTP server.

const char_t*
http_response_message(const Http *http);
http

HTTP session.

Return

The server's response message.

Remarks

See HTTP response.


http_response_size ()

Returns the number of response headers for an HTTP request.

uint32_t
http_response_size(const Http *http);
http

HTTP session.

Return

The number of headers.

Remarks

See HTTP response.


http_response_name ()

Returns the name of the response header of an HTTP request.

const char_t*
http_response_name(const Http *http,
                   const uint32_t index);
http

HTTP session.

index

The header index (0, size-1).

Return

The name of the header.

Remarks

See HTTP response.


http_response_value ()

Returns the value of the response header of an HTTP request.

const char_t*
http_response_value(const Http *http,
                    const uint32_t index);
http

HTTP session.

index

The header index (0, size-1).

Return

The value of the header.

Remarks

See HTTP response.


http_response_header ()

Returns the value of a response header from an HTTP request.

const char_t*
http_response_header(const Http *http,
                     const char_t *name);
http

HTTP session.

name

The name of the desired header.

Return

The value of the header. If the header does not exist, it will return an empty string "".

Remarks

See HTTP response.


http_response_body ()

Returns the response body of an HTTP request.

bool_t
http_response_body(const Http *http,
                   Stream *body,
                   ierror_t *error);
http

HTTP session.

body

Write stream where the content of the response will be stored.

error

Error code if the function fails. It can be NULL.

Return

TRUE if it could be read correctly. If FALSE, in error we will have the cause.

Remarks

See HTTP response.


http_dget ()

Make a direct request for a Web resource.

Stream*
http_dget(const char_t *url,
          uint32_t *result,
          ierror_t *error);
1
2
3
4
5
6
Stream *json = http_dget("http://serv.nappgui.com:80/dproducts.php", NULL, NULL);
if (json)
{
    ...
    stm_close(&json);
}
url

Resource URL.

result

Server response code. It can be NULL.

error

Error code if the function fails. It can be NULL.

Return

Stream with the result of the request.

Remarks

Use this function for direct access to an isolated resource. If you need to make multiple requests or configure headers, use http_create or http_secure.


http_exists ()

Checks if a Web resource is available/accessible.

bool_t
http_exists(const char_t *url);
url

Resource URL.

Return

TRUE if the resource (Web page, file, etc.) is accessible.

Remarks

HTTP redirects are not resolved. Will return FALSE if the URL as is is not valid.

❮ Back
Next ❯