http-request 23.0.0

http-request is a lightweight, efficient library for building, sending, and handling HTTP/HTTPS requests in Rust applications. It provides a simple and intuitive API, allowing developers to easily interact with web services, whether they use the "HTTP" or "HTTPS" protocol. The library supports various HTTP methods, custom headers, request bodies, timeout, automatic handling of redirects (including detecting redirect loops), and enhanced response body decoding (both automatic and manual), enabling fast and secure communication. Whether working with secure "HTTPS" connections or standard "HTTP" requests, the library is optimized for performance, minimal resource usage, and easy integration into Rust projects.
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use super::*;

/// Inherent implementation of [`RequestBuilder`].
impl RequestBuilder {
    /// Create an empty builder (defaults: GET, no headers, default config).
    ///
    /// # Returns
    ///
    /// - `Self` - A fresh request builder with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Shortcut for `method(Method::Get)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn get<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Get);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Post)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn post<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Post);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Put)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn put<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Put);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Delete)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn delete<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Delete);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Patch)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn patch<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Patch);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Head)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn head<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Head);
        self.get_mut_request().set_url(url);
        self
    }

    /// Shortcut for `method(Method::Options)` + `url(url)`.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn options<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_method(Method::Options);
        self.get_mut_request().set_url(url);
        self
    }

    /// Set HTTP method explicitly (`Method::Get` / `Method::Post` / etc.).
    ///
    /// # Arguments
    ///
    /// - `Method` - The HTTP method to apply to the request.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn method(&mut self, method: Method) -> &mut Self {
        self.get_mut_request().set_method(method);
        self
    }

    /// Set URL.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the request URL, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn url<T>(&mut self, url: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request().set_url(url);
        self
    }

    /// Append a percent-encoded query parameter to the request URL.
    ///
    /// # Arguments
    ///
    /// - `K` - The query parameter name.
    /// - `V` - The query parameter value.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn query<K, V>(&mut self, key: K, value: V) -> &mut Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        let current: String = self.get_request().get_url().clone();
        let (base, fragment): (&str, Option<&str>) = match current.split_once(URL_FRAGMENT) {
            Some((prefix, frag)) => (prefix, Some(frag)),
            None => (current.as_str(), None),
        };
        let sep: char = if base.contains(URL_QUERY) {
            if base.ends_with(URL_QUERY) || base.ends_with('&') {
                '\0'
            } else {
                '&'
            }
        } else {
            URL_QUERY
        };
        let mut updated: String = String::with_capacity(
            current.len() + key.as_ref().len() * 3 + value.as_ref().len() * 3 + 2,
        );
        updated.push_str(base);
        if sep != '\0' {
            updated.push(sep);
        }
        push_percent_encoded(key.as_ref(), &mut updated);
        updated.push('=');
        push_percent_encoded(value.as_ref(), &mut updated);
        if let Some(frag) = fragment {
            updated.push(URL_FRAGMENT);
            updated.push_str(frag);
        }
        self.get_mut_request().set_url(updated);
        self
    }

    /// Append multiple percent-encoded query parameters to the request URL.
    ///
    /// # Arguments
    ///
    /// - `I` - Iterator yielding `(key, value)` pairs.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn query_pairs<I, K, V>(&mut self, pairs: I) -> &mut Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        for (key, value) in pairs {
            self.query(key, value);
        }
        self
    }

    /// Set a single header (last write wins on duplicate keys).
    ///
    /// # Arguments
    ///
    /// - `K` - A header name that can be borrowed as a string slice.
    /// - `V` - A header value that can be borrowed as a string slice.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        self.get_mut_request().set_header(key, value);
        self
    }

    /// Set many headers at once.
    ///
    /// # Arguments
    ///
    /// - `HashMap<K, V>` - The header name-value pairs to apply, where each name and
    ///   value can be borrowed as a string slice.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn headers<K, V>(&mut self, headers: HashMap<K, V>) -> &mut Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        for (k, v) in headers {
            self.get_mut_request().set_header(k, v);
        }
        self
    }

    /// Set an `Authorization: Bearer <token>` header.
    ///
    /// # Arguments
    ///
    /// - `T` - The bearer token string.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn bearer_auth<T>(&mut self, token: T) -> &mut Self
    where
        T: AsRef<str>,
    {
        let value: String = format!("{BEARER_PREFIX}{}", token.as_ref());
        self.get_mut_request()
            .set_header(HEADER_AUTHORIZATION, value);
        self
    }

    /// Set an `Authorization: Basic <base64(username:password)>` header.
    ///
    /// # Arguments
    ///
    /// - `U` - The username string.
    /// - `P` - The password string.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn basic_auth<U, P>(&mut self, username: U, password: P) -> &mut Self
    where
        U: AsRef<str>,
        P: AsRef<str>,
    {
        let raw: String = format!("{}:{}", username.as_ref(), password.as_ref());
        let encoded: String = crate::utils::base64_encode(raw.as_bytes());
        let value: String = format!("{BASIC_PREFIX}{encoded}");
        self.get_mut_request()
            .set_header(HEADER_AUTHORIZATION, value);
        self
    }

    /// Append a `name=value` cookie to the outgoing `Cookie` header.
    ///
    /// # Arguments
    ///
    /// - `K` - Cookie name.
    /// - `V` - Cookie value.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn cookie<K, V>(&mut self, name: K, value: V) -> &mut Self
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        let pair: String = format!("{}={}", name.as_ref(), value.as_ref());
        let existing: Option<String> = self
            .get_request()
            .get_headers()
            .iter()
            .find(|(k, _): &(&String, &String)| k.eq_ignore_ascii_case(COOKIE))
            .map(|(_, v): (&String, &String)| v.clone());
        let merged: String = match existing {
            Some(prev) if !prev.is_empty() => format!("{prev}; {pair}"),
            _ => pair,
        };
        self.get_mut_request().set_header(COOKIE, merged);
        self
    }

    /// Remove a header by key.
    ///
    /// # Arguments
    ///
    /// - `K` - The header name to remove, which can be borrowed as a string slice.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn remove_header<K>(&mut self, key: K) -> &mut Self
    where
        K: AsRef<str>,
    {
        self.get_mut_request().remove_header(key);
        self
    }

    /// Clear all headers.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn clear_headers(&mut self) -> &mut Self {
        self.get_mut_request().clear_headers();
        self
    }

    /// Set raw body bytes.
    ///
    /// # Arguments
    ///
    /// - `B` - A value convertible into the raw body bytes, such as `Vec<u8>` or
    ///   `&[u8]`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn body<B>(&mut self, bytes: B) -> &mut Self
    where
        B: Into<Vec<u8>>,
    {
        self.get_mut_request().set_body(Body::from_bytes(bytes));
        self
    }

    /// Set UTF-8 text body.
    ///
    /// # Arguments
    ///
    /// - `T` - A value convertible into the text body, such as `&str` or `String`.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn body_text<T>(&mut self, text: T) -> &mut Self
    where
        T: Into<String>,
    {
        self.get_mut_request()
            .set_body(Body::from_bytes(text.into().into_bytes()));
        self
    }

    /// Set JSON body (serialised via `serde_json`).
    ///
    /// # Arguments
    ///
    /// - `&V` - A reference to the value to serialise as the JSON body.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn body_json<V>(&mut self, value: &V) -> &mut Self
    where
        V: Serialize,
    {
        if let Ok(bytes) = to_vec(value) {
            self.get_mut_request().set_body(Body::from_bytes(bytes));
        }
        self
    }

    /// Set an `application/x-www-form-urlencoded` body and matching `Content-Type` header.
    ///
    /// # Arguments
    ///
    /// - `I` - Iterator yielding `(key, value)` form fields.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn body_form<I, K, V>(&mut self, fields: I) -> &mut Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: AsRef<str>,
        V: AsRef<str>,
    {
        let mut encoded: String = String::new();
        for (key, value) in fields {
            if !encoded.is_empty() {
                encoded.push('&');
            }
            push_percent_encoded(key.as_ref(), &mut encoded);
            encoded.push('=');
            push_percent_encoded(value.as_ref(), &mut encoded);
        }
        self.get_mut_request()
            .set_header(CONTENT_TYPE, FORM_URLENCODED_CONTENT_TYPE);
        self.get_mut_request()
            .set_body(Body::from_bytes(encoded.into_bytes()));
        self
    }

    /// Set an RFC 7578 `multipart/form-data` body from `http_type::BodyPart` entries.
    ///
    /// # Arguments
    ///
    /// - `&[BodyPart]` - Multipart fields and uploaded file parts.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn body_multipart(&mut self, parts: &[BodyPart]) -> &mut Self {
        let (content_type, bytes): (String, Vec<u8>) = encode_multipart_parts(parts);
        self.get_mut_request()
            .set_header(CONTENT_TYPE, content_type);
        self.get_mut_request().set_body(Body::from_bytes(bytes));
        self
    }

    /// Add one or more PEM-encoded root CA certificates for TLS verification.
    ///
    /// # Arguments
    ///
    /// - `&[u8]` - Raw PEM-encoded X.509 certificate bytes.
    ///
    /// # Returns
    ///
    /// - `Result<&mut Self, RequestError>` - Updated builder or an error when PEM parsing fails.
    pub fn root_cert_pem(&mut self, pem_bytes: &[u8]) -> Result<&mut Self, RequestError> {
        let roots: &mut RootCertStore = self.get_mut_request().get_mut_tmp().get_mut_root_cert();
        let _: usize = add_pem_certs_to_store(roots, pem_bytes)?;
        Ok(self)
    }

    /// Set request timeout in milliseconds.
    ///
    /// # Arguments
    ///
    /// - `u64` - The request timeout in milliseconds.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn timeout(&mut self, ms: u64) -> &mut Self {
        self.get_mut_request().get_mut_config().set_timeout(ms);
        self
    }

    /// Set dedicated TCP connect timeout in milliseconds (`0` inherits `timeout`).
    ///
    /// # Arguments
    ///
    /// - `u64` - The connect timeout in milliseconds.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn connect_timeout(&mut self, ms: u64) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_connect_timeout(ms);
        self
    }

    /// Configure automatic bounded retries for idempotent transient failures (`0` = disabled).
    ///
    /// # Arguments
    ///
    /// - `usize` - Maximum number of retry attempts.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn retry(&mut self, max_retries: usize) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_max_retries(max_retries);
        self
    }

    /// Enable or disable HTTP keep-alive connection pooling for this request.
    ///
    /// # Arguments
    ///
    /// - `bool` - `true` to reuse persistent HTTP/1.1 or HTTP/2 connections from the pool, `false` to close after use.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn keep_alive(&mut self, enabled: bool) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_keep_alive(enabled);
        self
    }

    /// Set per-read buffer size.
    ///
    /// # Arguments
    ///
    /// - `usize` - The per-read buffer size in bytes.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn buffer_size(&mut self, n: usize) -> &mut Self {
        self.get_mut_request().get_mut_config().set_buffer_size(n);
        self
    }

    /// Force HTTP/1.1.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn http1_1_only(&mut self) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_http_version(HttpVersion::Http1_1);
        self
    }

    /// Force HTTP/2.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn http2_only(&mut self) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_http_version(HttpVersion::Http2);
        self
    }

    /// Enable auto-follow of 3xx redirects.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn redirect(&mut self) -> &mut Self {
        self.get_mut_request().get_mut_config().set_redirect(true);
        self
    }

    /// Disable auto-follow of 3xx redirects (default).
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn no_redirect(&mut self) -> &mut Self {
        self.get_mut_request().get_mut_config().set_redirect(false);
        self
    }

    /// Maximum number of redirects to follow (default `DEFAULT_MAX_REDIRECT_TIMES`).
    ///
    /// # Arguments
    ///
    /// - `usize` - The maximum number of redirects to follow.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn max_redirect_times(&mut self, n: usize) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_max_redirect_times(n);
        self
    }

    /// Enable automatic response body decompression (gzip / deflate / br).
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn decode(&mut self) -> &mut Self {
        self.get_mut_request().get_mut_config().set_decode(true);
        self
    }

    /// Disable automatic response body decompression.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn no_decode(&mut self) -> &mut Self {
        self.get_mut_request().get_mut_config().set_decode(false);
        self
    }

    /// Set the proxy configuration. Pass `None` (via [`Proxy::default()`) to
    /// clear an existing proxy.
    ///
    /// Construct via [`Proxy::http`] / [`Proxy::https`] / [`Proxy::socks5`]
    /// and optionally chain `.auth(user, pass)`.
    ///
    /// # Arguments
    ///
    /// - `Proxy` - The proxy configuration to apply to the request.
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn proxy(&mut self, proxy: Proxy) -> &mut Self {
        self.get_mut_request()
            .get_mut_config()
            .set_proxy(Some(proxy));
        self
    }

    /// Clear the proxy (use direct connection).
    ///
    /// # Returns
    ///
    /// - `&mut Self` - A mutable reference to this builder for chaining.
    pub fn no_proxy(&mut self) -> &mut Self {
        self.get_mut_request().get_mut_config().set_proxy(None);
        self
    }

    /// Finalise the builder and return the [`HttpRequest`].
    ///
    /// # Returns
    ///
    /// - `HttpRequest` - The finalised request, leaving the builder empty.
    pub fn build(&mut self) -> HttpRequest {
        mem::take(self.get_mut_request())
    }
}