---
name: "coap_attribute"
section: "3"
platform: "linux"
package: "libcoap3t64"
version: "4.3.4-1.1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/coap_attribute"
---

# coap\_attribute(3)

Work with CoAP attributes

## NAME

coap\_attribute, coap\_add\_attr, coap\_find\_attr, coap\_attr\_get\_value - Work with CoAP attributes

## SYNOPSIS

**#include \<coap3/coap.h\>**

**coap\_attr\_t \*coap\_add\_attr(coap\_resource\_t \****resource***, coap\_str\_const\_t \****name***, coap\_str\_const\_t \****value***, int** *flags***);**

**coap\_attr\_t \*coap\_find\_attr(coap\_resource\_t \****resource***, coap\_str\_const\_t \****name***);**

**coap\_str\_const\_t \*coap\_attr\_get\_value(coap\_attr\_t \****attribute***);**

For specific (D)TLS library support, link with **-lcoap-3-notls**, **-lcoap-3-gnutls**, **-lcoap-3-openssl**, **-lcoap-3-mbedtls** or **-lcoap-3-tinydtls**. Otherwise, link with **-lcoap-3** to get the default (D)TLS library support.

## DESCRIPTION

CoAP Resources on a CoAP Server need to be created, updated etc. The URI in the request packet defines the resource to work with, with possibly the Query referring to a sub-resource.

When resources are configured on the CoAP server, the URI to match against is specified. Callback Handlers are then added to the resource to handle the different request methods.

Adding Attributes allows textual information to be added to the resource which can then be reported back to any client doing a Resource Discovery using a "GET /.well-known/core" request with an optional query. See "RFC6690 5. Examples" for some examples of resource discovery usage. Common attribute names are rt, if, sz, ct, obs, rel, anchor, rev, hreflang, media, title and type. href cannot be an attribute name.

Attributes are automatically deleted when a Resource is deleted.

## FUNCTIONS

**Function: coap\_add\_attr()**

The **coap\_add\_attr**() function registers a new attribute called *name* for the *resource*. The value of the attribute is *value* which can be NULL.

*flags* can be zero or more of the following or’d together, which, if set, defines what is to be internally freed off when the attribute is deleted with **coap\_delete\_resource**().

| | |
| --- | --- |
| **COAP\_ATTR\_FLAGS\_RELEASE\_NAME** | Free off *name* when attribute is deleted with **coap\_delete\_resource**(). |
| **COAP\_ATTR\_FLAGS\_RELEASE\_VALUE** | Free off *value* when attribute is deleted with **coap\_delete\_resource**(). |

**Function: coap\_find\_attr()**

The **coap\_find\_attr**() function returns the attribute with the *name*, if found, associated with *resource*.

**Function: coap\_attr\_get\_value()**

The **coap\_attr\_get\_value**() function returns the *value* associated with the specified *attribute*.

## RETURN VALUES

**coap\_add\_attr**() returns a pointer to the attribute that was created or NULL if there is a malloc failure.

**coap\_find\_attr**() returns a pointer to the first matching attribute or NULL if the *name* was not found.

**coap\_attr\_get\_value**() returns a pointer to the value held within the attribute. The pointer can be NULL if the *value* id NULL, or NULL if *attribute* does not exist.

## EXAMPLE

**Initialize Resources**

```
#include <coap3/coap.h>
void hnd_get_index(coap_resource_t *resource, coap_session_t *session,
                   const coap_pdu_t *request,
                   const coap_string_t *query,
                   coap_pdu_t *response);
void hnd_get_time(coap_resource_t *resource, coap_session_t *session,
                  const coap_pdu_t *request,
                  const coap_string_t *query,
                  coap_pdu_t *response);
void hnd_put_time(coap_resource_t *resource, coap_session_t *session,
                  const coap_pdu_t *request,
                  const coap_string_t *query,
                  coap_pdu_t *response);
void hnd_delete_time(coap_resource_t *resource, coap_session_t *session,
                     const coap_pdu_t *request,
                     const coap_string_t *query,
                     coap_pdu_t *response);
static void
init_resources(coap_context_t *ctx) {
  coap_resource_t *r;
  /* Create a resource to return general information */
  r = coap_resource_init(NULL, 0);
  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_index);
  /* Document resource for '.well-known/core' request */
  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
  coap_add_attr(r, coap_make_str_const("title"),
                coap_make_str_const("\"General Info\""), 0);
  coap_add_resource(ctx, r);
  /* Create a resource to return return or update time */
  r = coap_resource_init(coap_make_str_const("time"),
                         COAP_RESOURCE_FLAGS_NOTIFY_CON);
  coap_resource_set_get_observable(r, 1);
  coap_register_request_handler(r, COAP_REQUEST_GET, hnd_get_time);
  coap_register_request_handler(r, COAP_REQUEST_PUT, hnd_put_time);
  coap_register_request_handler(r, COAP_REQUEST_DELETE, hnd_delete_time);
  /* Document resource for 'time' request */
  coap_add_attr(r, coap_make_str_const("ct"), coap_make_str_const("0"), 0);
  coap_add_attr(r, coap_make_str_const("title"),
                coap_make_str_const("\"Internal Clock\""), 0);
  coap_add_attr(r, coap_make_str_const("rt"), coap_make_str_const("\"secs\""),
                0);
  coap_add_attr(r, coap_make_str_const("if"), coap_make_str_const("\"clock\""),
                0);
  coap_add_resource(ctx, r);
}
```

## SEE ALSO

[**coap\_resource**(3)](/coap_resource/3.md) and [**coap\_handler**(3)](/coap_handler/3.md)

## FURTHER INFORMATION

See

"RFC7252: The Constrained Application Protocol (CoAP)"

"RFC6690: Constrained RESTful Environments (CoRE) Link Format"

for further information.

## BUGS

Please report bugs on the mailing list for libcoap: libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at https://github.com/obgm/libcoap/issues

## AUTHORS

The libcoap project \<libcoap-developers@lists.sourceforge.net\>
