File: req_headers.Rd

package info (click to toggle)
r-cran-httr2 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,684 kB
  • sloc: sh: 13; makefile: 2
file content (79 lines) | stat: -rw-r--r-- 2,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/req-headers.R
\name{req_headers}
\alias{req_headers}
\alias{req_headers_redacted}
\title{Modify request headers}
\usage{
req_headers(.req, ..., .redact = NULL)

req_headers_redacted(.req, ...)
}
\arguments{
\item{.req}{A \link{request}.}

\item{...}{<\code{\link[rlang:dyn-dots]{dynamic-dots}}> Name-value pairs of headers
and their values.
\itemize{
\item Use \code{NULL} to reset a value to httr2's default.
\item Use \code{""} to remove a header.
\item Use a character vector to repeat a header.
}}

\item{.redact}{A character vector of headers to redact. The Authorization
header is always redacted.}
}
\value{
A modified HTTP \link{request}.
}
\description{
\code{req_headers()} allows you to set the value of any header.

\code{req_headers_redacted()} is a variation that adds "redacted" headers, which
httr2 avoids printing on the console. This is good practice for
authentication headers to avoid accidentally leaking them in log files.
}
\examples{
req <- request("http://example.com")

# Use req_headers() to add arbitrary additional headers to the request
req |>
  req_headers(MyHeader = "MyValue") |>
  req_dry_run()

# Repeated use overrides the previous value:
req |>
  req_headers(MyHeader = "Old value") |>
  req_headers(MyHeader = "New value") |>
  req_dry_run()

# Setting Accept to NULL uses curl's default:
req |>
  req_headers(Accept = NULL) |>
  req_dry_run()

# Setting it to "" removes it:
req |>
  req_headers(Accept = "") |>
  req_dry_run()

# If you need to repeat a header, provide a vector of values
# (this is rarely needed, but is important in a handful of cases)
req |>
  req_headers(HeaderName = c("Value 1", "Value 2", "Value 3")) |>
  req_dry_run()

# If you have headers in a list, use !!!
headers <- list(HeaderOne = "one", HeaderTwo = "two")
req |>
  req_headers(!!!headers, HeaderThree = "three") |>
  req_dry_run()

# Use `req_headers_redacted()`` to hide a header in the output
req_secret <- req |>
  req_headers_redacted(Secret = "this-is-private") |>
  req_headers(Public = "but-this-is-not")

req_secret
req_secret |> req_dry_run()
}