### Basic GET Request Example Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Demonstrates a simple asynchronous GET request using the `feignhttp::get` macro. Requires a tokio runtime. ```rust use feignhttp::get; #[get("https://api.github.com")] async fn github() -> feignhttp::Result {} #[tokio::main] async fn main() -> Result<(), Box> { let r = github().await?; println!("result: {}", r); Ok(()) } ``` -------------------------------- ### Basic GET Request Example Source: https://github.com/dxx/feignhttp/blob/dev/README.md Demonstrates a simple asynchronous GET request to an API endpoint using FeignHTTP and tokio. The result is printed to the console. ```rust use feignhttp::get; #[get("https://api.github.com")] async fn github() -> feignhttp::Result {} #[tokio::main] async fn main() -> Result<(), Box> { let r = github().await?; println!("result: {}", r); Ok(()) } ``` -------------------------------- ### Handling Path Parameters in GET Request Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use `path` to specify path values in a URL. This example shows how to extract 'owner' and 'repo' from the URL path. ```rust use feignhttp::get; #[get("https://api.github.com/repos/{owner}/{repo}")] async fn repository( #[path("owner")] user: &str, #[path] repo: String, ) -> feignhttp::Result {} #[tokio::main] async fn main() -> Result<(), Box> { let r = repository("dxx", "feignhttp".to_string()).await?; println!("repository result: {}", r); Ok(()) } ``` -------------------------------- ### Using Path Parameters Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Illustrates how to specify path values using the `path` attribute for path parameters in GET requests. ```APIDOC ## Paths Use `path` to specify path values: ```rust use feignhttp::get; #[get("https://api.github.com/repos/{owner}/{repo}")] async fn repository( #[path("owner")] user: &str, #[path] repo: String, ) -> feignhttp::Result {} #[tokio::main] async fn main() -> Result<(), Box> { let r = repository("dxx", "feignhttp".to_string()).await?; println!("repository result: {}", r); Ok(()) } ``` ``` -------------------------------- ### Configuring Trait-Based Clients with ClientConfig Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use `ClientConfig` to set global configurations like connect timeout and request timeout for trait-based clients. This example configures a GitHub API client. ```rust use feignhttp::{ClientConfig, FeignClientBuilder, feign}; #[feign(url = "https://api.github.com")] pub trait GitHub { #[get("/repos/{owner}/{repo}")] async fn repository( &self, #[path] owner: &str, #[path] repo: &str, ) -> feignhttp::Result; } #[tokio::main] async fn main() -> Result<(), Box> { let config = ClientConfig { connect_timeout: Some(5000), timeout: Some(10000), ..Default::default() }; let github = GitHub::builder().config(config).build()?; let r = github.repository("dxx", "feignhttp").await?; println!("repository: {}", r); Ok(()) } ``` -------------------------------- ### Using Constants for URLs Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Shows how to use constants for defining URLs in GET requests, making the code more maintainable. ```APIDOC ## URL Use constants for URLs: ```rust use feignhttp::get; const GITHUB_URL: &str = "https://api.github.com"; #[get(GITHUB_URL, path = "/repos/{owner}/{repo}/languages")] async fn languages( #[path] owner: &str, #[path] repo: &str, ) -> feignhttp::Result {} ``` ``` -------------------------------- ### Using URL Constants for GET Requests Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Define constants for URLs to improve readability and maintainability. This example uses a constant for the base GitHub URL. ```rust use feignhttp::get; const GITHUB_URL: &str = "https://api.github.com"; #[get(GITHUB_URL, path = "/repos/{owner}/{repo}/languages")] async fn languages( #[path] owner: &str, #[path] repo: &str, ) -> feignhttp::Result {} ``` -------------------------------- ### Adding Query Parameters to GET Request Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `query` attribute to specify query parameters for a GET request. Parameters without any attribute are query parameters by default. ```rust use feignhttp::get; #[get("https://api.github.com/repos/{owner}/{repo}/contributors")] async fn contributors( #[path] owner: &str, #[path] repo: &str, #[query] page: u32, ) -> feignhttp::Result {} ``` -------------------------------- ### Using Query Parameters Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Explains how to add query parameters to GET requests using the `query` attribute. Parameters without any attribute are treated as query parameters by default. ```APIDOC ## Query Parameters Use `query` for query parameters: ```rust use feignhttp::get; #[get("https://api.github.com/repos/{owner}/{repo}/contributors")] async fn contributors( #[path] owner: &str, #[path] repo: &str, #[query] page: u32, ) -> feignhttp::Result {} ``` **Note**: Parameters without any attribute are query parameters by default. ``` -------------------------------- ### Using Traits for Multiple Requests Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Define a trait with multiple HTTP methods to manage related requests. This example shows how to set up a context with URL path and query parameters for a GitHub API client. ```rust use feignhttp::{Context, FeignClientBuilder, feign}; #[derive(Context)] struct GithubContext { #[url_path("owner")] user: &'static str, #[url_path] repo: &'static str, #[param] accept: &'static str, } #[feign( url = "https://api.github.com/repos/{owner}/{repo}", headers = "Accept: {accept}" )] pub trait Github { #[get] async fn home(&self) -> feignhttp::Result; #[get(path = "", headers = "Accept: application/json")] async fn repository(&self) -> feignhttp::Result; } #[tokio::main] async fn main() -> Result<(), Box> { let context = GithubContext { user: "dxx", repo: "feignhttp", accept: "*/*", }; let github = Github::builder().context(context).build()?; let r = github.home().await?; println!("github result: {}", r); Ok(()) } ``` -------------------------------- ### Adding Headers to GET Request Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `header` attribute to specify individual request headers. Alternatively, use the `headers` metadata for multiple headers. ```rust use feignhttp::get; #[get("https://httpbin.org/headers")] async fn headers( #[header] accept: &str, ) -> feignhttp::Result {} ``` ```rust #[get("https://httpbin.org/headers", headers = "key1: value1; key2: value2")] async fn headers() -> feignhttp::Result {} ``` -------------------------------- ### Deserialize JSON Response Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Define a struct with `#[derive(Deserialize)]` to automatically deserialize JSON responses. This example fetches GitHub issues. ```rust use feignhttp::get; use serde::Deserialize; #[derive(Debug, Deserialize)] struct IssueItem { pub id: u32, pub number: u32, pub title: String, } const GITHUB_URL: &str = "https://api.github.com"; #[get(url = GITHUB_URL, path = "/repos/{owner}/{repo}/issues")] async fn issues( #[path] owner: &str, #[path] repo: &str, ) -> feignhttp::Result> {} ``` -------------------------------- ### Using Headers Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Demonstrates how to set request headers using the `header` attribute for individual headers or the `headers` metadata for multiple headers. ```APIDOC ## Headers Use `header` for request headers: ```rust use feignhttp::get; #[get("https://httpbin.org/headers")] async fn headers( #[header] accept: &str, ) -> feignhttp::Result {} ``` Or use `headers` metadata: ```rust #[get("https://httpbin.org/headers", headers = "key1: value1; key2: value2")] async fn headers() -> feignhttp::Result {} ``` ``` -------------------------------- ### Customize HTTP Client with Reqwest Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Demonstrates how to build a Feign client using a pre-configured Reqwest client. This allows for custom settings like User-Agent. ```rust use feignhttp::{ClientWrapper, FeignClientBuilder, feign}; #[feign(url = "https://api.github.com")] pub trait GitHub { #[get("/users/{user}")] async fn user(&self, #[path] user: &str) -> feignhttp::Result; } #[tokio::main] async fn main() -> Result<(), Box> { let client = reqwest::Client::builder() .user_agent("Feign HTTP") .build()?; let client_wrapper = ClientWrapper::with_client(client)?; let github = GitHub::builder().build_with_client(client_wrapper)?; let r = github.user("dxx").await?; println!("{}", r); Ok(()) } ``` -------------------------------- ### Making a POST request Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Demonstrates how to make a POST request with a request body using the `post` attribute macro and `#[body]` for the request body. String and &str types are sent as plain text with `content-type: text/plain`. ```APIDOC ## POST Request Use the `post` attribute macro and `#[body]` for request body: ```rust use feignhttp::post; #[post("https://httpbin.org/anything")] async fn post_data(#[body] text: String) -> feignhttp::Result {} ``` `String` and `&str` are sent as plain text with `content-type: text/plain`. ``` -------------------------------- ### Add JSON Dependencies Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md To handle JSON requests and responses, include the `serde` crate and the `reqwest-json` feature for `feignhttp`. ```toml serde = { version = "1", features = ["derive"] } feignhttp = { version = "0.6", features = ["reqwest-json"] } ``` -------------------------------- ### Making a POST Request with Body Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `post` attribute macro and `#[body]` for the request body. String and &str are sent as plain text with `content-type: text/plain`. ```rust use feignhttp::post; #[post("https://httpbin.org/anything")] async fn post_data(#[body] text: String) -> feignhttp::Result {} ``` -------------------------------- ### Add Tokio Dependency Source: https://github.com/dxx/feignhttp/blob/dev/README.md Add the tokio runtime to your project's Cargo.toml to support async/await. ```toml [dependencies] tokio = { version = "1", features = ["full"] } ``` -------------------------------- ### Upload File with Multipart Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use `#[file]` for file uploads and `#[part]` for other form fields in multipart requests. Supported file types include `PathBuf`, `std::fs::File`, and `Vec`. ```rust use feignhttp::post; use std::path::PathBuf; #[post("https://httpbin.org/post")] async fn upload_file( #[file("file")] file: PathBuf, #[part("name")] name: &str, ) -> feignhttp::Result {} ``` -------------------------------- ### JSON Request and Response Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Illustrates how to send JSON data in a request body and deserialize JSON responses. Requires `serde` and the `reqwest-json` feature. ```APIDOC ## GET /repos/{owner}/{repo}/issues (Get Issues) ### Description Retrieves a list of issues for a given repository. Responses are deserialized into a `Vec`. ### Method GET ### Endpoint https://api.github.com/repos/{owner}/{repo}/issues ### Parameters #### Path Parameters - **owner** (string) - Required - The owner of the repository. - **repo** (string) - Required - The name of the repository. #### Query Parameters None #### Request Body None ### Request Example ```rust use feignhttp::get; use serde::Deserialize; #[derive(Debug, Deserialize)] struct IssueItem { pub id: u32, pub number: u32, pub title: String, } const GITHUB_URL: &str = "https://api.github.com"; #[get(url = GITHUB_URL, path = "/repos/{owner}/{repo}/issues")] async fn issues( #[path] owner: &str, #[path] repo: &str, ) -> feignhttp::Result> {} ``` ### Response #### Success Response (200) - **Vec** - A vector of issue items. #### Response Example ```json [ { "id": 1, "number": 1, "title": "Example Issue" } ] ``` ``` ```APIDOC ## POST /anything (Send JSON) ### Description Sends a JSON-formatted request body. Feign HTTP automatically sets the `content-type` to `application/json`. ### Method POST ### Endpoint https://httpbin.org/anything ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **user** (User) - Required - The user object to send as JSON. ### Request Example ```rust use feignhttp::post; use serde::Serialize; #[derive(Debug, Serialize)] struct User { id: i32, name: String, } #[post(url = "https://httpbin.org/anything")] async fn post_user(#[body] user: User) -> feignhttp::Result {} ``` ### Response #### Success Response (200) Returns the server's response, typically including the JSON data sent. #### Response Example ```json { "json": { "id": 123, "name": "example" } } ``` ``` -------------------------------- ### Add FeignHTTP Dependency Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Add FeignHTTP to your project's Cargo.toml file. Ensure you have an async runtime like tokio. ```toml [dependencies] tokio = { version = "1", features = ["full"] } ``` ```toml feignhttp = { version = "0.6" } ``` -------------------------------- ### Send Form Parameters Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `#[form]` attribute for form parameters. Automatically adds `content-type: application/x-www-form-urlencoded`. ```rust use feignhttp::post; #[post(url = "https://httpbin.org/anything")] async fn post_user( #[form] id: i32, #[form] name: &str, ) -> feignhttp::Result {} ``` -------------------------------- ### Configure FeignHTTP with ISAHC Backend Source: https://github.com/dxx/feignhttp/blob/dev/README.md Configure FeignHTTP to use the ISAHC HTTP backend by disabling default features and enabling the 'isahc-client' feature in Cargo.toml. ```toml feignhttp = { version = "0.6", default-features = false, features = ["isahc-client"] } ``` -------------------------------- ### Add Multipart Dependency Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md To use multipart requests, add the `reqwest-multipart` feature to your `feignhttp` dependency in `Cargo.toml`. ```toml feignhttp = { version = "0.6", features = ["reqwest-multipart"] } ``` -------------------------------- ### Add FeignHTTP Dependency Source: https://github.com/dxx/feignhttp/blob/dev/README.md Add the feignhttp crate to your Cargo.toml using the default features. ```toml feignhttp = { version = "0.6" } ``` -------------------------------- ### Enable Debug Logs with Cargo.toml Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md To enable detailed request and response logs, add the `log` feature to your `feignhttp` dependency in `Cargo.toml`. Ensure your application's logging level is set to debug. ```toml feignhttp = { version = "0.6", features = ["log"] } ``` -------------------------------- ### Multipart File Upload Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Shows how to upload files and include other form parts in a multipart request. Supports specifying content type and filename for uploads. ```APIDOC ## POST /post (Multipart File Upload) ### Description Uploads a file along with other form fields using multipart encoding. ### Method POST ### Endpoint https://httpbin.org/post ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body Multipart form data containing file and other parts. ### Request Example ```rust use feignhttp::post; use std::path::PathBuf; #[post("https://httpbin.org/post")] async fn upload_file( #[file("file")] file: PathBuf, #[part("name")] name: &str, ) -> feignhttp::Result {} ``` ### Request Example with Custom Content Type and Filename ```rust #[post("https://httpbin.org/post")] async fn upload_file( #[file("file", content_type = "image/png", filename = "custom.png")] file: PathBuf, #[part("name")] name: &str, ) -> feignhttp::Result {} ``` ### Supported File Types `PathBuf`, `std::fs::File`, `Vec` ### Response #### Success Response (200) Returns the server's response, typically including the uploaded file and form data. #### Response Example ```json { "files": { "file": "..." }, "form": { "name": "example" } } ``` ``` -------------------------------- ### Specify File Content Type and Filename Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Customize the `content_type` and `filename` for file uploads within the `#[file]` attribute. ```rust #[post("https://httpbin.org/post")] async fn upload_file( #[file("file", content_type = "image/png", filename = "custom.png")] file: PathBuf, #[part("name")] name: &str, ) -> feignhttp::Result {} ``` -------------------------------- ### Handle HTTP Status Errors Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Shows how to use `feignhttp::Result` to catch and handle HTTP status errors. It checks for specific error kinds like `ErrorKind::Status`. ```rust use feignhttp::{get, ErrorKind}; #[get(url = "https://httpbin.org/123")] async fn status_error() -> feignhttp::Result<()> {} #[tokio::main] async fn main() { match status_error().await { Err(err) => { if err.is_status_error() { println!("status_error: {}", err); } if let ErrorKind::Status(status) = err.error_kind() { println!("status code: {}", status.as_u16()); } } _ => {} } } ``` -------------------------------- ### Form Parameters Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Demonstrates how to send form parameters in a POST request. Feign HTTP automatically sets the `content-type` to `application/x-www-form-urlencoded`. ```APIDOC ## POST /anything (Form Parameters) ### Description Sends form-encoded data in the request body. ### Method POST ### Endpoint https://httpbin.org/anything ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None (parameters are form-encoded) ### Request Example ```rust use feignhttp::post; #[post(url = "https://httpbin.org/anything")] async fn post_user( #[form] id: i32, #[form] name: &str, ) -> feignhttp::Result {} ``` ### Response #### Success Response (200) Returns the server's response, typically including the form data sent. #### Response Example ```json { "form": { "id": "123", "name": "example" } } ``` ``` -------------------------------- ### Configuring Request Timeout Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Set a timeout for an HTTP request using the `timeout` attribute. The timeout value is in milliseconds. ```rust use feignhttp::get; #[get(url = "https://httpbin.org/delay/5", timeout = 3000)] async fn timeout() -> feignhttp::Result {} ``` -------------------------------- ### Send JSON Request Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `#[body]` attribute with a struct that implements `#[derive(Serialize)]` to send a JSON request. Automatically adds `content-type: application/json`. ```rust use feignhttp::post; use serde::Serialize; #[derive(Debug, Serialize)] struct User { id: i32, name: String, } #[post(url = "https://httpbin.org/anything")] async fn post_user(#[body] user: User) -> feignhttp::Result {} ``` -------------------------------- ### Passing Dynamic Parameters to Timeout Source: https://github.com/dxx/feignhttp/blob/dev/docs/README.md Use the `param` attribute to pass dynamic values to request attributes like `timeout`. The parameter is defined with `#[param]` and its value is used in the attribute. ```rust use feignhttp::get; #[get(url = "https://httpbin.org/delay/5", timeout = "{time}")] async fn timeout(#[param] time: u16) -> feignhttp::Result {} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.