fhir_post-methods: POST to a FHIR server

fhir_postR Documentation

POST to a FHIR server

Description

This function is a convenience wrapper around httr::POST().

Usage

fhir_post(
  url,
  body,
  username = NULL,
  password = NULL,
  token = NULL,
  add_headers = NULL,
  verbose = 1,
  log_errors = NULL
)

## S4 method for signature 'ANY,fhir_resource'
fhir_post(
  url,
  body,
  username = NULL,
  password = NULL,
  token = NULL,
  add_headers = NULL,
  verbose = 1,
  log_errors = NULL
)

## S4 method for signature 'ANY,fhir_bundle_xml'
fhir_post(
  url,
  body,
  username = NULL,
  password = NULL,
  token = NULL,
  add_headers = NULL,
  verbose = 1,
  log_errors = NULL
)

## S4 method for signature 'ANY,fhir_body'
fhir_post(
  url,
  body,
  username = NULL,
  password = NULL,
  token = NULL,
  add_headers = NULL,
  verbose = 1,
  log_errors = NULL
)

Arguments

url

An object of class fhir_url or a character vector of length one containing the url to POST to.

body

An object of class fhir_resource, fhir_bundle_xml or fhir_body. See details for how to generate them.

username

A character vector of length one containing the username for basic authentication.

password

A character vector of length one containing the password for basic authentication.

token

A character vector of length one or object of class httr::Token, for bearer token authentication (e.g. OAuth2). See fhir_authenticate() for how to create this.

add_headers

A named character vector of custom headers to add to the HTTP request, e.g. c(myHeader = "somevalue") or c(firstHeader = "value1", secondHeader = "value2").

verbose

An integer vector of length one. If 0, nothing is printed, if > 0 success message is printed. Defaults to 1.

log_errors

Either NULL or a character vector of length one indicating the name of a file in which to save http errors. NULL means no error logging. When a file name is provided, the errors are saved in the specified file. Defaults to NULL. Regardless of the value of log_errors the most recent http error message within the current R session is saved internally and can be accessed with fhir_recent_http_error().

Details

fhir_post() accepts four classes for the body:

  1. A fhir_resource as created by fhir_build_resource(). This is used when just a single resource should be POSTed to the server. In this case url must contain the base url plus the resource type, e.g. http://hapi.fhir.org/baseR4/Patient.

  2. A fhir_bundle_xml representing a transaction or batch bundle as created by fhir_build_bundle().

  3. A fhir_body as created by fhir_body(). This is the most flexible approach, because within the fhir_body object you can represent any kind of content as a string and set the type accordingly. See examples.

For examples of how to create the different body types see the respective help pages. For an example of the entire workflow around creating and POSTing resources, see the package vignette on recreating resources.

Examples

## Not run: 
### 1. POST transaction bundle
#unserialize example bundles
bundle <- fhir_unserialize(transaction_bundle_example)

#have a look at the bundle
cat(toString(bundle))

#post
fhir_post(url = "http://hapi.fhir.org/baseR4", body = bundle)


### 2. POST single resouce
#unserialize example resource
resource <- fhir_unserialize(example_resource1)

#have a look at the resource
resource

#post
url <- fhir_url(url = "http://hapi.fhir.org/baseR4", resource = "Patient")
fhir_post(url = url, body = resource)


### 3. POST arbitrary body
#define body
body <- fhir_body(content = "<Patient> <gender value='female'/> </Patient>", type = "xml")

#post
url <- fhir_url(url = "http://hapi.fhir.org/baseR4", resource = "Patient")
fhir_post(url = url, body = body)

## End(Not run)

POLAR-fhiR/fhircrackr documentation built on Dec. 24, 2024, 3:06 a.m.