27 lines
2.0 KiB
Markdown
27 lines
2.0 KiB
Markdown
# Handy-Http Data
|
|
[DDoc Reference](ddoc/data/index.html)
|
|
|
|
The [Data](https://git.andrewlalis.com/Handy-Http/data) component provides support for some data formats that are common in web servers.
|
|
|
|
## JSON
|
|
|
|
Support for reading and writing JSON is provided via the [ASDF](https://code.dlang.org/packages/asdf) library. Two methods are provided to help with JSON I/O for HTTP requests:
|
|
|
|
* [`readJsonBodyAs`](ddoc/data/handy_http_data.json.readJsonBodyAs.html) for deserializing a JSON request body into some type.
|
|
* [`writeJsonBody`](ddoc/data/handy_http_data.json.writeJsonBody.html) for serializing some data into a JSON response body.
|
|
|
|
## Multipart Formdata
|
|
|
|
The [`multipart.d`](ddoc/data/handy_http_data.multipart.html) module provides support for reading HTTP request bodies defined as `multipart/form-data`. The main function to use here is [`readBodyAsMultipartFormData`](ddoc/data/handy_http_data.multipart.readBodyAsMultipartFormData.html), which takes in a reference to a `ServerHttpRequest`, and returns a [`MultipartFormData`](ddoc/data/handy_http_data.multipart.MultipartFormData.html) struct containing the parsed contents.
|
|
|
|
The parsed form data simply contains a list of elements of type [`MultipartElement`](ddoc/data/handy_http_data.multipart.MultipartElement.html), and you can read the underlying data from the element's `content` attribute.
|
|
|
|
In practice, multipart formdata is most often used when one would like to send multiple types of data to an API at once, such as when a user is submitting a form that includes basic fields as well as file uploads.
|
|
|
|
## XML
|
|
|
|
Support for XML is provided via the [DXML](https://code.dlang.org/packages/dxml) library. Two methods are provided to help with XML I/O for HTTP requests:
|
|
|
|
* [`readXMLBody`](ddoc/data/handy_http_data.xml.readXMLBody.html) for reading a request body as an XML `DOMEntity`.
|
|
* [`writeXMLBody`](ddoc/data/handy_http_data.xml.writeXMLBody.html) for writing a response body by supplying a delegate function that builds the XML tag tree that you'd like to write.
|