1 # Civetweb API Reference
3 ### `mg_response_header_start( conn, status );`
4 ### `mg_response_header_add( conn, header, value, value_len );`
5 ### `mg_response_header_add_lines( conn, http1_headers );`
6 ### `mg_response_header_send( conn );`
10 | Parameter | Type | Description |
11 | :--- | :--- | :--- |
12 |**`conn`**|`struct mg_connection *`|The server is sending a response to this connection|
13 |**`status`**|`int`|HTTP status code to send (100-599)|
14 |**`header`**|`const char *`|HTTP header name|
15 |**`value`**|`const char *`|HTTP header value|
16 |**`value_len`**|`int`|Length of HTTP header value. -1 will use the entire value string.|
17 |**`http1_headers`**|`const char *`|String in format "Key: Value\r\nKey2: Value2"|
21 | Type | Description |
22 |`int`| An integer indicating success (>=0) or failure (<0). |
23 |`-1`| Error: Invalid parameter (e.g., NULL pointer or value out of range) |
24 |`-2`| Error: Invalid connection type. These functions must only be used in web server request handlers. |
25 |`-3`| Error: Invalid connection status. These functions must be used before `mg_send()` or `mg_printf()` functions. |
26 |`-4`| Error: Too many headers. The total number of header lines is limited to MG_MAX_HEADERS (default: 64). |
27 |`-5`| Error: Out of memory. |
32 The `mg_response_header_*` family of functions can be used to send HTTP response headers from a web server handler function.
33 Do not use it for HTTP clients or websockets. When using `mg_response_header_*` functions, they must be used to send all HTTP headers - combining `mg_response_header_*` functions and `mg_send_*`/`mg_write`/`mg_printf` to send HTTP headers is not possible.
34 After sending HTTP headers, HTTP body data is still sent using `mg_write`/`mg_printf`.
36 All `mg_response_header_*` functions must be used prior to any call to `mg_write`/`mg_printf`, in the following order:
38 1) `mg_response_header_start` (once)
39 2) `mg_response_header_add` (0 - 64 times), alternatively `mg_response_header_add_lines`. The preferred function is `mg_response_header_add`. The function `mg_response_header_add_lines` can process HTTP/1.x formatted header lines and is used for compatibility.
40 3) `mg_response_header_send` (once)
41 4) `mg_write` or `mg_printf` (multiple) to send the HTTP body
43 Using `mg_response_header_*` functions will allow a request handler to process HTTP/1.x and HTTP/2 requests, in contrast to sending HTTP headers directly using `mg_printf`/`mg_write`.
48 * [`mg_send_http_ok();`](mg_send_http_ok.md)
49 * [`mg_send_http_error();`](mg_send_http_error.md)
50 * [`mg_send_http_redirect();`](mg_send_http_redirect.md)
51 * [`mg_write();`](mg_write.md)
52 * [`mg_send_chunk();`](mg_send_chunk.md)