File: curl.md

package info (click to toggle)
r-cran-httr2 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,604 kB
  • sloc: sh: 21; makefile: 2
file content (183 lines) | stat: -rw-r--r-- 4,367 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# must be call to curl

    Code
      curl_translate("echo foo")
    Condition
      Error in `curl_translate()`:
      ! Expecting call to "curl" not to "echo".

# must have cmd argument if non-interactive

    Code
      curl_translate()
    Condition
      Error in `curl_translate()`:
      ! Must supply `cmd`.

# common headers can be removed

    Code
      print(curl_simplify_headers(headers, simplify_headers = TRUE))
    Output
      <httr2_headers>
      Accept: application/vnd.api+json
      user-agent: agent
    Code
      print(curl_simplify_headers(headers, simplify_headers = FALSE))
    Output
      <httr2_headers>
      Sec-Fetch-Dest: empty
      Sec-Fetch-Mode: cors
      sec-ch-ua-mobile: ?0
      Accept: application/vnd.api+json
      referer: ref
      user-agent: agent

# can translate to httr calls

    Code
      curl_translate("curl http://x.com")
    Output
      request("http://x.com/") |> 
        req_perform()
    Code
      curl_translate("curl http://x.com -X DELETE")
    Output
      request("http://x.com/") |> 
        req_method("DELETE") |> 
        req_perform()
    Code
      curl_translate("curl http://x.com -H A:1")
    Output
      request("http://x.com/") |> 
        req_headers(
          A = "1",
        ) |> 
        req_perform()
    Code
      curl_translate("curl http://x.com -H 'A B:1'")
    Output
      request("http://x.com/") |> 
        req_headers(
          `A B` = "1",
        ) |> 
        req_perform()
    Code
      curl_translate("curl http://x.com -u u:p")
    Output
      request("http://x.com/") |> 
        req_auth_basic("u", "p") |> 
        req_perform()
    Code
      curl_translate("curl http://x.com --verbose")
    Output
      request("http://x.com/") |> 
        req_perform(verbosity = 1)

# can translate query

    Code
      curl_translate("curl http://x.com?string=abcde&b=2")
    Output
      request("http://x.com/") |> 
        req_url_query(
          string = "abcde",
          b = "2",
        ) |> 
        req_perform()

# can translate data

    Code
      curl_translate("curl http://example.com --data abcdef")
    Output
      request("http://example.com/") |> 
        req_body_raw("abcdef", "application/x-www-form-urlencoded") |> 
        req_perform()
    Code
      curl_translate(
        "curl http://example.com --data abcdef -H Content-Type:text/plain")
    Output
      request("http://example.com/") |> 
        req_body_raw("abcdef", "text/plain") |> 
        req_perform()

# can translate ocokies

    Code
      curl_translate("curl 'http://test' -H 'Cookie: x=1; y=2;z=3'")
    Output
      request("http://test/") |> 
        req_cookies_set(
          x = "1",
          y = "2",
          z = "3",
        ) |> 
        req_perform()

# can translate json

    Code
      curl_translate(
        "curl http://example.com --data-raw '{\"a\": 1, \"b\": \"text\"}' -H Content-Type:application/json")
    Output
      request("http://example.com/") |> 
        req_body_json(
          data = list(a = 1L, b = "text"),
        ) |> 
        req_perform()
    Code
      curl_translate("curl http://example.com --json '{\"a\": 1, \"b\": \"text\"}'")
    Output
      request("http://example.com/") |> 
        req_body_json(
          data = list(a = 1L, b = "text"),
        ) |> 
        req_perform()

# content type stays in header if no data

    Code
      curl_translate("curl http://example.com -H Content-Type:text/plain")
    Output
      request("http://example.com/") |> 
        req_headers(
          `Content-Type` = "text/plain",
        ) |> 
        req_perform()

# can read from clipboard

    Code
      curl_translate()
    Message
      v Copying to clipboard:
    Output
      request("http://example.com/") |> 
        req_headers(
          A = "1",
          B = "2",
        ) |> 
        req_perform()
    Code
      writeLines(clipr::read_clip())
    Output
      request("http://example.com/") |> 
        req_headers(
          A = "1",
          B = "2",
        ) |> 
        req_perform()

# encode_string2() produces simple strings

    Code
      curl_translate(cmd)
    Output
      request("http://example.com/") |> 
        req_method("PATCH") |> 
        req_body_json(
          data = list(data = list(x = 1L, y = "a", nested = list(z = list(1L, 2L, 3L)))),
        ) |> 
        req_perform()