Title: | Utility Functions for Developing Web Applications |
---|---|
Description: | Parses http request data in application/json, multipart/form-data, or application/x-www-form-urlencoded format. Includes example of hosting and parsing html form data in R using either 'httpuv' or 'Rhttpd'. |
Authors: | Jeroen Ooms [aut, cre] |
Maintainer: | Jeroen Ooms <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.2.2 |
Built: | 2024-11-02 05:43:33 UTC |
Source: | https://github.com/jeroen/webutils |
Starts the httpuv web server and hosts a simple form including a file upload to demo the multipart parser.
demo_httpuv(port = 9359)
demo_httpuv(port = 9359)
port |
which port number to run the http server |
Other demo:
demo_rhttpd()
Starts the Rhttpd web server and hosts a simple form including a file upload to demo the multipart parser.
demo_rhttpd()
demo_rhttpd()
Other demo:
demo_httpuv()
Parse the body of a http request, based on the Content-Type
request
header. Currently supports the three most important content types:
application/x-www-form-urlencoded
with parse_query()
,
multipart/form-data
with parse_multipart()
, and application/json
with jsonlite::fromJSON()
.
parse_http(body, content_type, ...)
parse_http(body, content_type, ...)
body |
request body of the http request |
content_type |
content-type http request header as specified by the client |
... |
additional arguments passed to parser function |
# Parse json encoded payload: parse_http('{"foo":123, "bar":true}', 'application/json') # Parse url-encoded payload parse_http("foo=1%2B1%3D2&bar=yin%26yang", "application/x-www-form-urlencoded") ## Not run: use demo app to parse multipart/form-data payload demo_rhttpd() ## End(Not run)
# Parse json encoded payload: parse_http('{"foo":123, "bar":true}', 'application/json') # Parse url-encoded payload parse_http("foo=1%2B1%3D2&bar=yin%26yang", "application/x-www-form-urlencoded") ## Not run: use demo app to parse multipart/form-data payload demo_rhttpd() ## End(Not run)
Parse a multipart/form-data request, which is usually generated from a HTML form
submission. The parameters can include both text values as well as binary files.
They can be distinguished from the presence of a filename
attribute.
parse_multipart(body, boundary)
parse_multipart(body, boundary)
body |
body of the HTTP request. Must be raw or character vector. |
boundary |
boundary string as specified in the |
A multipart/form-data request consists of a single body which contains one or more
values plus meta-data, separated using a boundary string. This boundary string
is chosen by the client (e.g. the browser) and specified in the Content-Type
header of the HTTP request. There is no escaping; it is up to the client to choose
a boundary string that does not appear in one of the values.
The parser is written in pure R, but still pretty fast because it uses the regex engine.
## Not run: example form demo_rhttpd() ## End(Not run)
## Not run: example form demo_rhttpd() ## End(Not run)
Parse http parameters from a query string. This includes unescaping of url-encoded values.
parse_query(query)
parse_query(query)
query |
a url-encoded query string |
For http GET requests, the query string is specified
in the URL after the question mark. For http POST or PUT requests, the query
string can be used in the request body when the Content-Type
header
is set to application/x-www-form-urlencoded
.
q <- "foo=1%2B1%3D2&bar=yin%26yang" parse_query(q)
q <- "foo=1%2B1%3D2&bar=yin%26yang" parse_query(q)