http-sf

raw JSON →
1.2.0 verified Mon Apr 27 auth: no python

A Python library for parsing and serializing HTTP Structured Fields (RFC 8941). Current version 1.2.0, released 2025-01-13. Cadence: irregular, ~1 minor per year. Requires Python >=3.10.

pip install http-sf
error TypeError: argument should be bytes, buffer or ASCII string, not 'str'
cause Passing a Python str instead of bytes to parse functions.
fix
Use parse_item(b'...') instead of parse_item('...').
error TypeError: cannot import name 'parse_item' from 'http_sf'
cause Wrong module name; often confused with 'http-sf' (hyphen) or 'http_sf' not installed.
fix
Install with 'pip install http-sf' and import as 'from http_sf import parse_item'.
error AttributeError: 'BareItem' object has no attribute 'value'
cause Using older version (<1.0) where BareItem was a string.
fix
Upgrade to >=1.2.0: pip install --upgrade http-sf.
breaking Version 1.0 changed the return type from strings to BareItem objects. Old code relying on string returns may break.
fix Upgrade to >=1.2.0 and use .value accessor.
deprecated The module name is http_sf (underscore), not http-sf. Import errors occur if using hyphen.
fix Use import http_sf (underscore).
gotcha Input must be bytes, not str, for parsing functions. Passing str will raise TypeError.
fix Encode string to bytes before parsing: parse_item(b'"value"').
gotcha Serialize functions return bytes, not str. May need .decode() for string output.
fix Use serialized.decode() if you need a string.

Parses and serializes HTTP Structured Fields (RFC 8941). Functions for item, list, and dictionary types. All return BareItem objects with .value and .params.

from http_sf import parse_item, serialize_item

# Parse a structured field string
parsed = parse_item('"hello world"')
print(parsed.value)  # hello world

# Serialize back to string
serialized = serialize_item(parsed)
print(serialized)  # b'"hello world"'

# With parameters: parse_item(b'i=1;a=true')