Soletta™ Framework
Framework for making IoT devices

Full online documentation | C API Index
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Typedefs | Functions
HTTP Client

API to perform HTTP(s) requests. More...

Data Structures

struct  sol_http_request_interface
 The HTTP request interface to use when creating a new request. More...
 

Typedefs

typedef struct
sol_http_client_connection 
sol_http_client_connection
 Opaque handler for an HTTP client connection. More...
 
typedef struct
sol_http_request_interface 
sol_http_request_interface
 The HTTP request interface to use when creating a new request. More...
 

Functions

void sol_http_client_connection_cancel (struct sol_http_client_connection *pending)
 Cancel a pending request and release its resources. More...
 
struct sol_http_client_connectionsol_http_client_request (enum sol_http_method method, const char *url, const struct sol_http_params *params, void(*cb)(void *data, struct sol_http_client_connection *connection, struct sol_http_response *response), const void *data)
 Create a request for the specified URL using the given method. More...
 
struct sol_http_client_connectionsol_http_client_request_with_interface (enum sol_http_method method, const char *url, const struct sol_http_params *params, const struct sol_http_request_interface *interface, const void *data)
 Create a request for the specified URL using the given method. More...
 

Detailed Description

API to perform HTTP(s) requests.

It will buffer whole responses in memory, so it's more suitable to perform remote API calls than file transfers.

Typedef Documentation

Opaque handler for an HTTP client connection.

It's created when a request is made with sol_http_client_request() or sol_http_client_request_with_interface().

A connection may be canceled with sol_http_client_connection_cancel().

The HTTP request interface to use when creating a new request.

It allows one to have more control over the request, notifying when data comes or when data should be sent.

See Also
sol_http_client_request_with_interface()
Note
HTTP client follows the Soletta stream design pattern, which can be found here: IO Streams

Function Documentation

void sol_http_client_connection_cancel ( struct sol_http_client_connection pending)

Cancel a pending request and release its resources.

Parameters
pendingthe object previously created with sol_http_client_request().
Examples:
/src/samples/http/download.c.

Referenced by shutdown().

struct sol_http_client_connection* sol_http_client_request ( enum sol_http_method  method,
const char *  url,
const struct sol_http_params params,
void(*)(void *data, struct sol_http_client_connection *connection, struct sol_http_response *response)  cb,
const void *  data 
)

Create a request for the specified URL using the given method.

The result of the request is obtained in cb.

One should check the response code on sol_http_response to check if the request returned success or some error.

See Also
sol_http_status_code.
static void
response_cb(void *userdata, struct sol_http_client_connection *connection,
struct sol_http_response *response)
{
uint16_t idx;
// Check if the response is correct
if (response->response_code != SOL_HTTP_STATUS_OK) {
fprintf(stderr, "Finished with error, response code: %d\n",
response->response_code);
return;
}
// Printing the response parameters ...
SOL_HTTP_PARAMS_FOREACH_IDX (&response->param, value, idx) {
switch (value->type) {
printf("[COOKIE] %.*s : %.*s\n",
SOL_STR_SLICE_PRINT(value->value.key_value.key),
SOL_STR_SLICE_PRINT(value->value.key_value.value));
break;
printf("[HEADER] %.*s : %.*s\n",
SOL_STR_SLICE_PRINT(value->value.key_value.key),
SOL_STR_SLICE_PRINT(value->value.key_value.value));
default:
break;
}
}
// Now let's show the response contents
printf("%.*s\n", (int)response->content.used, (char *)response->content.data);
}
int main(int argc, char *argv[]) {
int r;
// Some code .....
// Allows redirect
r = sol_http_params_add(&params,
SOL_INT_CHECK(r, < 0, r);
"http://www.github.com/solettaproject/soletta", &params, response_cb, NULL);
if (!pending)
fprintf(stderr, "ERROR: Failed to create the request\n");
// more code.....
return 0;
}
Parameters
methoda valid HTTP method, e. g. SOL_HTTP_METHOD_GET or SOL_HTTP_METHOD_POST
urla string containing a valid URL.
paramsthe parameters used on this request, e. g. headers, cookies and post fields.
cbcallback that will be called with the response for this request.
datauser data given as parameter on cb
Returns
a pending connection on success, NULL on error.
See Also
sol_http_client_connection_cancel
Examples:
/src/samples/http/client.c.

Referenced by startup().

struct sol_http_client_connection* sol_http_client_request_with_interface ( enum sol_http_method  method,
const char *  url,
const struct sol_http_params params,
const struct sol_http_request_interface interface,
const void *  data 
)

Create a request for the specified URL using the given method.

The result of the request is obtained in cb.

One should check the response code on sol_http_response to check if the request returned success or some error.

See Also
sol_http_status_code.
Parameters
methoda valid HTTP method, e. g. SOL_HTTP_METHOD_GET or SOL_HTTP_METHOD_POST
urla string containing a valid URL.
paramsthe parameters used on this request, e. g. headers, cookies and post fields.
interfacethe interface with the callbacks used in the request.
See Also
sol_http_request_interface
Parameters
datauser data given as parameter on cb
Returns
a pending connection on success, NULL on error.
See Also
sol_http_client_connection_cancel
sol_http_client_request
Examples:
/src/samples/http/download.c.

Referenced by startup().