/
README.Rmd
138 lines (95 loc) · 6.28 KB
/
README.Rmd
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
---
output: rmarkdown::github_document
editor_options:
chunk_output_type: console
---
[![Build Status](https://travis-ci.org/hrbrmstr/curlconverter.svg?branch=master)](https://travis-ci.org/hrbrmstr/curlconverter)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/hrbrmstrcurlconverter?branch=master&svg=true)](https://ci.appveyor.com/project/hrbrmstr/curlconverter)
[![CRAN_Status_Badge](https://www.r-pkg.org/badges/version/curlconverter)](https://cran.r-project.org/package=curlconverter)
[![](http://cranlogs.r-pkg.org/badges/curlconverter)](https://cran.r-project.org/package=curlconverter)
[![Coverage Status](https://img.shields.io/codecov/c/github/<USERNAME>/<REPO>/master.svg)](https://codecov.io/github/<USERNAME>/<REPO>?branch=master)
# curlconverter
Tools to Transform 'cURL' Command-Line Calls to 'httr' Requests
## Description
Deciphering web/'REST' 'API' and 'XHR' calls can be tricky, which is one reason why internet browsers provide "Copy as cURL" functionality within their "Developer Tools"pane(s). These 'cURL' command-lines can be difficult to wrangle into an 'httr' 'GET' or 'POST' request, but you can now "straighten" these 'cURLs' either from data copied to the system clipboard or by passing in a vector of 'cURL' command-lines and getting back a list of parameter elements which can be used to form 'httr' requests. These lists can be passed to another function to automagically make 'httr' functions.
### WIP
This is the path back to CRAN and to 1.0.0.
The V8 dependency has been removed and the package now uses `docopt`. This will make it easier to install on many systems and enable rapid addition of support for new `cURL` command-line parameters.
The "HAR" functions are not working well with the new methods but will be for full release.
## What's Inside The Tin
The following functions are implemented:
- `straighten`: convert one or more _"Copy as cURL"_ command lines into useful data
- `parse_query`: parse URL query parameters into a named list
- `make_req`: turn parsed cURL command lines into a `httr` request functions (i.e. returns working R functions)
The following functions are implemented:
## Installation
```{r eval=FALSE}
devtools::install_github("hrbrmstr/curlconverter")
```
```{r message=FALSE, warning=FALSE, error=FALSE, include=FALSE}
options(width=120)
```
## Usage
```{r message=FALSE, warning=FALSE, error=FALSE}
library(curlconverter)
library(jsonlite)
library(httr)
# current verison
packageVersion("curlconverter")
```
Simple example using a call to <https://httpbin.org/headers>:
```{r}
httpbinrhcurl <- "curl 'https://httpbin.org/headers' -H 'pragma: no-cache' -H 'accept-encoding: gzip, deflate, sdch' -H 'accept-language: en-US,en;q=0.8' -H 'upgrade-insecure-requests: 1' -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.39 Safari/537.36' -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' -H 'cache-control: no-cache' -H 'referer: https://httpbin.org/' --compressed"
straight <- straighten(httpbinrhcurl)
res <- make_req(straight)
# or
straighten(httpbinrhcurl) %>%
make_req() -> res
toJSON(content(res[[1]](), as="parsed"), auto_unbox = TRUE, pretty=TRUE)
```
Slightly more complex one:
```{r}
toJSON(straighten("curl 'http://financials.morningstar.com/ajax/ReportProcess4HtmlAjax.html?&t=XNAS:MSFT®ion=usa&culture=en-US&cur=&reportType=is&period=12&dataType=A&order=asc&columnYear=5&curYearPart=1st5year&rounding=3&view=raw&r=973302&callback=jsonp1454021128757&_=1454021129337' -H 'Cookie: JSESSIONID=5E43C98903E865D72AA3C2DCEF317848; sfhabit=asc%7Craw%7C3%7C12%7CA%7C5%7Cv0.14; ScrollY=0' -H 'DNT: 1' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36' -H 'Accept: text/javascript, application/javascript, */*' -H 'Referer: http://financials.morningstar.com/income-statement/is.html?t=MSFT®ion=usa&culture=en-US' -H 'X-Requested-With: XMLHttpRequest' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' --compressed"), auto_unbox = TRUE, pretty=TRUE)
```
There are some built-in test files you can play with:
```{r}
(curl_line <- readLines(system.file("extdata/curl5.txt", package="curlconverter"), warn=FALSE))
toJSON(straighten(curl_line, quiet=TRUE), auto_unbox = TRUE, pretty=TRUE)
(curl_line <- readLines(system.file("extdata/curl8.txt", package="curlconverter"), warn=FALSE))
# example with query parameters in the body
req <- straighten(curl_line, quiet=FALSE)
# ugh
(req[[1]]$data)
#yay!
toJSON(parse_query(req[[1]]$data), auto_unbox = TRUE, pretty=TRUE)
```
Spinning straw into gold
```{r}
curl_line <- c('curl "http://anasim.iet.unipi.it/moniqa/php/from_js.php" -H "Origin: http://anasim.iet.unipi.it" -H "Accept-Encoding: gzip, deflate" -H "Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4" -H "User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Accept: */*" -H "Referer: http://anasim.iet.unipi.it/moniqa/" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" --data "deviceid=65&function_name=extract_measurements" --compressed')
straighten(curl_line) %>%
make_req() -> get_data
cat(capture.output(get_data[[1]]), sep="\n")
```
That also sends this to the console and clipboard:
```{r eval=FALSE}
httr::VERB(
verb = "GET", url = "http://anasim.iet.unipi.it/moniqa/php/from_js.php",
httr::add_headers(
Origin = "http://anasim.iet.unipi.it",
`Accept-Encoding` = "gzip, deflate",
`Accept-Language` = "it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4",
`User-Agent` = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36",
Accept = "*/*", Referer = "http://anasim.iet.unipi.it/moniqa/",
`X-Requested-With` = "XMLHttpRequest",
Connection = "keep-alive"
),
body = list(
deviceid = "65",
function_name = "extract_measurements"
),
encode = "form"
)
```
### Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CONDUCT.md).
By participating in this project you agree to abide by its terms.