Couchbase C Client  2.4.4
HTTP Operations

Detailed Description

Schedule HTTP requests to the server.

This includes management and view requests

Functions

lcb_http_complete_callback lcb_set_http_complete_callback (lcb_t, lcb_http_complete_callback)
 Set the HTTP completion callback for HTTP request completion. More...
 
lcb_http_data_callback lcb_set_http_data_callback (lcb_t, lcb_http_data_callback)
 Set the HTTP data stream callback for streaming responses. More...
 
lcb_error_t lcb_make_http_request (lcb_t instance, const void *command_cookie, lcb_http_type_t type, const lcb_http_cmd_t *cmd, lcb_http_request_t *request)
 Execute HTTP request matching given path and yield JSON result object. More...
 
void lcb_cancel_http_request (lcb_t instance, lcb_http_request_t request)
 Cancel ongoing HTTP request. More...
 

Typedefs

typedef void(* lcb_http_res_callback )(lcb_http_request_t request, lcb_t instance, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)
 Callback invoked for HTTP requests. More...
 
typedef lcb_http_res_callback lcb_http_data_callback
 
typedef lcb_http_res_callback lcb_http_complete_callback
 

Enumerations

enum  lcb_http_type_t
 The type of HTTP request to execute. More...
 
enum  lcb_http_method_t
 HTTP Request method enumeration These just enumerate the various types of HTTP request methods supported. More...
 

Function Documentation

lcb_http_complete_callback lcb_set_http_complete_callback ( lcb_t  ,
lcb_http_complete_callback   
)

Set the HTTP completion callback for HTTP request completion.

This callback will be invoked once when the response is complete. If the lcb_HTTPCMDv0::chunked flag was set, the lcb_HTTRESPv0::bytes will be NULL, otherwise it will contain the fully buffered response.

lcb_http_data_callback lcb_set_http_data_callback ( lcb_t  ,
lcb_http_data_callback   
)

Set the HTTP data stream callback for streaming responses.

This callback is invoked only if the lcb_HTTPCMDv0::chunked flag is true. The lcb_HTTRESPv0::bytes field will on each invocation contain a new fragment of data which should be processed by the client. When the request is complete, the the callback specified by lcb_set_http_complete_callback() will be invoked with the lcb_HTTPRESPv0::bytes field set to NULL

lcb_error_t lcb_make_http_request ( lcb_t  instance,
const void *  command_cookie,
lcb_http_type_t  type,
const lcb_http_cmd_t cmd,
lcb_http_request_t *  request 
)

Execute HTTP request matching given path and yield JSON result object.

Depending on type it could be:

  • LCB_HTTP_TYPE_VIEW

    The client should setup view_complete callback in order to fetch the result. Also he can setup view_data callback to fetch response body in chunks as soon as possible, it will be called each time the library receive a data chunk from socket. The empty bytes argument (NULL pointer and zero size) is the sign of end of response. Chunked callback allows to save memory on large datasets.

  • LCB_HTTP_TYPE_MANAGEMENT

    Management requests allow you to configure the cluster, add/remove buckets, rebalance etc. The result will be passed to management callbacks (data/complete).

Fetch first 10 docs from _design/test/_view/all view

lcb_http_request_t req;
lcb_http_cmd_t *cmd = calloc(1, sizeof(lcb_http_cmd_t));
cmd->version = 0;
cmd->v.v0.path = "_design/test/_view/all?limit=10";
cmd->v.v0.npath = strlen(item->v.v0.path);
cmd->v.v0.body = NULL;
cmd->v.v0.nbody = 0;
cmd->v.v0.method = LCB_HTTP_METHOD_GET;
cmd->v.v0.chunked = 1;
cmd->v.v0.content_type = "application/json";
lcb_error_t err = lcb_make_http_request(instance, NULL,
LCB_HTTP_TYPE_VIEW, &cmd, &req);
if (err != LCB_SUCCESS) {
.. failed to schedule request ..

The same as above but with POST filter

lcb_http_request_t req;
lcb_http_cmd_t *cmd = calloc(1, sizeof(lcb_http_cmd_t));
cmd->version = 0;
cmd->v.v0.path = "_design/test/_view/all?limit=10";
cmd->v.v0.npath = strlen(item->v.v0.path);
cmd->v.v0.body = "{\"keys\": [\"test_1000\", \"test_10002\"]}"
cmd->v.v0.nbody = strlen(item->v.v0.body);
cmd->v.v0.method = LCB_HTTP_METHOD_POST;
cmd->v.v0.chunked = 1;
cmd->v.v0.content_type = "application/json";
lcb_error_t err = lcb_make_http_request(instance, NULL,
LCB_HTTP_TYPE_VIEW, &cmd, &req);
if (err != LCB_SUCCESS) {
.. failed to schedule request ..
Delete bucket via REST management API
lcb_http_request_t req;
cmd->version = 0;
cmd.v.v0.path = query.c_str();
cmd.v.v0.npath = query.length();
cmd.v.v0.body = NULL;
cmd.v.v0.nbody = 0;
cmd.v.v0.method = LCB_HTTP_METHOD_DELETE;
cmd.v.v0.chunked = false;
cmd.v.v0.content_type = "application/x-www-form-urlencoded";
lcb_error_t err = lcb_make_http_request(instance, NULL,
if (err != LCB_SUCCESS) {
.. failed to schedule request ..
Parameters
instanceThe handle to lcb
command_cookieA cookie passed to all of the notifications from this command
typeThe type of the request needed.
cmdThe struct describing the command options
requestWhere to store request handle
Stability
Committed:
void lcb_cancel_http_request ( lcb_t  instance,
lcb_http_request_t  request 
)

Cancel ongoing HTTP request.

This API will stop the current request. Any pending callbacks will not be invoked any any pending data will not be delivered. Useful for a long running request which is no longer needed

Parameters
instanceThe handle to lcb
requestThe request handle
Stability
Committed:

Data Structure Documentation

struct lcb_HTTPCMDv0
Data Fields
const char * path A view path string with optional query params (e.g.

skip, limit etc.)

lcb_SIZE npath Length of the path.

Mandatory

const void * body The POST body for HTTP request.
lcb_SIZE nbody Length of the body.

Mandatory if body != NULL

lcb_http_method_t method
int chunked If true the client will use lcb_http_data_callback to notify about response and will call lcb_http_complete with empty data eventually.
const char * content_type The Content-Type header for request.

For view requests it is usually application/json, for management – application/x-www-form-urlencoded.

struct lcb_HTTPCMDv1

v1 is used by the raw http requests.

It is exactly the same layout as v0, but it contains an extra field; the hostname & port to use....

Data Fields
const char * path
See also
lcb_HTTPCMDv0::path
lcb_SIZE npath
const void * body
See also
lcb_HTTPCMDv0::body
lcb_SIZE nbody
lcb_http_method_t method
int chunked
const char * content_type
const char * host
const char * username
const char * password
struct lcb_http_cmd_t

Wrapper structure for lcb_make_http_request.

See also
lcb_HTTPCMDv0
lcb_HTTPCMDv1
Data Fields
int version
union lcb_http_cmd_t v
struct lcb_HTTPRESPv0

Response structure received for HTTP requests.

The headers field is a list of key-value headers for HTTP, so it may be traversed like so:

const char ** cur = resp->headers;
for (; *cur; cur+=2) {
printf("Header: %s:%s\n", cur[0], cur[1]);
}
Data Fields
lcb_http_status_t status HTTP status code.
const char * path Path used for request.
lcb_SIZE npath
const char *const * headers List of headers.
const void * bytes Body (if any)
lcb_SIZE nbytes

Typedef Documentation

typedef void(* lcb_http_res_callback)(lcb_http_request_t request, lcb_t instance, const void *cookie, lcb_error_t error, const lcb_http_resp_t *resp)

Callback invoked for HTTP requests.

Parameters
requestOriginal request handle
instanceThe instance on which the request was issued
cookieCookie associated with the request
errorError code for request. Note that more information may likely be found within the response structure itself, specifically the lcb_HTTPRESPv0::status and lcb_HTTPRESPv0::bytes field
respThe response structure

Enumeration Type Documentation

The type of HTTP request to execute.

Enumerator
LCB_HTTP_TYPE_VIEW 

Execute a request against the bucket.

The handle must be of LCB_TYPE_BUCKET and must be connected.

LCB_HTTP_TYPE_MANAGEMENT 

Execute a management API request.

The credentials used will match those passed during the instance creation time. Thus is the instance type is LCB_TYPE_BUCKET then only bucket-level credentials will be used.

LCB_HTTP_TYPE_RAW 

Execute an arbitrary request against a host and port.

HTTP Request method enumeration These just enumerate the various types of HTTP request methods supported.

Refer to the specific cluster or view API to see which method is appropriate for your request