--- type: "resource" title: "Binary" resource: "Binary" --- # Binary ## Introduction ## Scope and Usage There are situations where it is useful or required to handle pure binary content using the same framework as other resources. Typically, this is when the binary content is referred to from other FHIR Resources. Using the same framework means that the existing servers, security arrangements, code libraries, etc. can handle additional related content. Typically, Binary resources are used for handling content such as: - [CDA](http://www.hl7.org/implement/standards/product_brief.cfm?product_id=7) Documents (i.e. with XDS) - PDF Documents - Images A binary resource can contain any content, whether text, image, pdf, zip archive, etc. These resources are served in their native form on the rest interface, but can also be represented in XML, JSON, or other formats, such as when including these resources in a Bundle (used when it is convenient to include these in the response directly rather than leaving them by reference). ## Boundaries and Relationships When a FHIR server finds it convenient to manage the content within the same overall REST framework as the other resources, the Binary resource is generally used as the target in the [Attachment](datatypes#Attachment) data type to reference content via the `.url` element, such as in the DocumentReference resources. Consequently, the Binary resource can be a target wherever the Attachment data type is used such as in the DocumentReference resource. The [DocumentReference](documentreference) resource allows conveying binary content (via attachment) or pointing to one (as a `Binary` or non-FHIR URI) along with the metadata around that resource, and as such are searchable. Binary resources do not support 'search'. While CDA and PDF documents are conveyed as Binary (because they cannot be expressed natively in FHIR), [FHIR Documents](documents) do not need to be similarly encoded and can be sent natively in FHIR using [Bundle](bundle). However, in some situations FHIR Documents may be sent as a Binary if there is a need to treat them the same as other types of documents or binary files. The Binary resource does not convey context of the file. If the context (information such as author, procedure, technique, etc.) is needed it should be conveyed in a DocumentReference resource. The Binary resource may be used to convey actual binary file content conveyed by the DocumentReference resource. See some uses of Binary and DocumentReferece in the [IHE XDS](https://profiles.ihe.net/ITI/HIE-Whitepaper/index.html) Implementation Guide. ## Notes ### Content Optionality `Binary.data` is optional because it is excluded when a summary view of the Binary resource is requested (this can be useful when the binary resource is part of a wider request e.g. a search \_include). Otherwise, the data is not optional; a binary resource without any specified content is not useful. ### Serving Binary Resources using the RESTful API Binary resources behave slightly differently from all other resources on the RESTful API: - When a read request is made with a FHIR type in the Accept header (e.g. "`application/fhir+xml`" or "`application/fhir+json`") the Binary resource is returned in the requested FHIR format. This applies even when the binary data itself has a FHIR mime type - The Binary resource is always represented in the native FHIR format when wrapped in a [Bundle](bundle) - The [\_format](http#format) overrides the accept header and SHALL be interpreted as using the standard FHIR mime types, even if the more generic mime types are given as a value. - When the read request has some other type in the `Accept` header, then the content should be returned with the content type stated in the resource in the `Content-Type` header. E.g. if the content type in the resource is "application/pdf", then the content should be returned as a PDF directly. The \_summary parameter does not apply in this case. - due to the way the web infrastructure works, it is not possible to make blanket rules about the relationship between the "Accept" field in the HTTP request, and the return type, which is why there is no hard rule about this. However, the intent is that unless specifically requested, the FHIR XML/JSON representation is not returned - When binary data is written to the server ([create](http#create)/[update](http#update) - POST or PUT), the data is accepted as is and treated as the content of a `Binary`, including when the content type is "application/fhir+xml" or "application/fhir+json", except for the special case where the content is actually a `Binary` resource. - Note that when client requests a Binary resource using a generic mime type (application/xml, text/xml, or application/json), the server SHOULD return the content directly if the content-type format matches the requested mime type (e.g. if the Accept header is application/json, and the `contentType` is vnd.xacml+json). However, servers might not always be able to interpret mime types correctly, and clients SHOULD be prepared to receive either format. Note that in the native binary representation, the normal resource [metadata](resource#meta) is not available directly, though some of it is replicated in the HTTP response headers. Specifically, the following elements of the resource have matching HTTP Headers: - **Binary.meta.lastUpdated**: `Last-Modified` - **Binary.meta.versionId**: `ETag header` - **Binary.contentType**: `Content-Type` - **Binary.securityContext**: `X-Security-Context` - this is a FHIR specific extension, primarily intended to allow the security context for a Binary resource to be specified when it is posted in native form. The content of the header is `Binary.securityContext.reference` ### Image source for Narrative Binary resources may be the target of `img.src` references in XHTML narrative, and also in markdown. See [Image References](narrative#id) for further details. ### Security Considerations Binary resources are not constrained to any list of safe content types (content types without active elements such as scripting or executable code), and therefore can be of any content type and encoding. Therefore, extra care needs to be taken to validate the content of the Binary resource against malicious or malformed content. For more details see [Security of Narrative](security#narrative), since the security issues are similar. Very often, the content of a `Binary` resource is sensitive, and the server should apply appropriate access control to the content. When the server itself generates the content, it implicitly knows what access control to apply. When the client provides the binary to the server itself, it uses the `securityContext` element (or the matching `X-Security-Context` HTTP header) to inform the server that the Binary resource should be treated as if it was the other resource. Typically, the other resource is a DocumentReference or similar resource that refers directly to the Binary resource, but that is not mandatory. ### Read This specification makes no rules concerning advanced read functionality for the Binary resource, such as: - retrieving byte ranges - Finding the size of the content before retrieving it (though this can be achieved using a [HEAD request](http#head)) - Stream content from Binary? - generating content dynamically on the fly Servers are free to support these features, but they are not required. ### Search This specification does not support searching on Binary resources. ### Patch The semantics of a PATCH operation on a Binary resource are not currently specified. ## StructureDefinition ### Elements (Simplified) - **[Binary](/binary-definitions#Binary)** [0..*]: - Pure binary content defined by a format other than FHIR - **[Binary.contentType](/binary-definitions#Binary.contentType)** [1..1]: [code](/code) required:[mimetypes](/valueset-mimetypes) MimeType of the binary content - **[Binary.securityContext](/binary-definitions#Binary.securityContext)** [0..1]: Reference([Resource](/Resource)) Identifies another resource to use as proxy when enforcing access control - **[Binary.data](/binary-definitions#Binary.data)** [0..1]: [base64Binary](/base64Binary) The actual content ## Mappings - [Binary Mappings](/binary-mappings) — 5 mapping entries ## Resource Packs ### list-Binary-packs.xml ```xml ``` ## Examples - [binary-example](/binary-example-binary-example) — binary-example - [binary-examples-header](/binary-example-binary-examples-header) — binary-examples-header - [example](/binary-example-example) — binary-example — Binary Example - a PDF document - [f006](/binary-example-f006) — binary-f006 — Example Patient photo as a binary [Full Examples](/binary-examples)