Produce a header value and ``key=value`` parameters separated by semicolons ``;``. For example, the ``Content-Type`` header. .. code-block:: python dump_options_header("text/html", {"charset": "UTF-8"}) 'text/html; charset=UTF-8' This is the reverse of :func:`parse_opt
(header: str | None, options: t.Mapping[str, t.Any])
| 194 | |
| 195 | |
| 196 | def dump_options_header(header: str | None, options: t.Mapping[str, t.Any]) -> str: |
| 197 | """Produce a header value and ``key=value`` parameters separated by semicolons |
| 198 | ``;``. For example, the ``Content-Type`` header. |
| 199 | |
| 200 | .. code-block:: python |
| 201 | |
| 202 | dump_options_header("text/html", {"charset": "UTF-8"}) |
| 203 | 'text/html; charset=UTF-8' |
| 204 | |
| 205 | This is the reverse of :func:`parse_options_header`. |
| 206 | |
| 207 | If a value contains non-token characters, it will be quoted. |
| 208 | |
| 209 | If a value is ``None``, the parameter is skipped. |
| 210 | |
| 211 | In some keys for some headers, a UTF-8 value can be encoded using a special |
| 212 | ``key*=UTF-8''value`` form, where ``value`` is percent encoded. This function will |
| 213 | not produce that format automatically, but if a given key ends with an asterisk |
| 214 | ``*``, the value is assumed to have that form and will not be quoted further. |
| 215 | |
| 216 | :param header: The primary header value. |
| 217 | :param options: Parameters to encode as ``key=value`` pairs. |
| 218 | |
| 219 | .. versionchanged:: 2.3 |
| 220 | Keys with ``None`` values are skipped rather than treated as a bare key. |
| 221 | |
| 222 | .. versionchanged:: 2.2.3 |
| 223 | If a key ends with ``*``, its value will not be quoted. |
| 224 | """ |
| 225 | segments = [] |
| 226 | |
| 227 | if header is not None: |
| 228 | segments.append(header) |
| 229 | |
| 230 | for key, value in options.items(): |
| 231 | if value is None: |
| 232 | continue |
| 233 | |
| 234 | if key[-1] == "*": |
| 235 | segments.append(f"{key}={value}") |
| 236 | else: |
| 237 | segments.append(f"{key}={quote_header_value(value)}") |
| 238 | |
| 239 | return "; ".join(segments) |
| 240 | |
| 241 | |
| 242 | def dump_header(iterable: dict[str, t.Any] | t.Iterable[t.Any]) -> str: |