pub trait TryIntoRequest: Sealed {
    fn try_into_request(self) -> Result<Request, Error>;
}
Expand description

Convert a typed request builder into a raw Request.

Converting a typed request builder into a raw request may be preferable in order to verify whether a request is valid prior to passing it to Client::request.

Creating raw requests is useful for unit tests and debugging.

Examples

Convert a CreateMessage builder into a Request, inspect its body and route, and then send the request:

use std::{env, str};
use twilight_http::{client::Client, request::TryIntoRequest};
use twilight_model::{channel::Message, id::Id};

let client = Client::new(env::var("DISCORD_TOKEN")?);
let channel_id = Id::new(1);
let builder = client
    .create_message(channel_id)
    .content("This is a test message!")?
    .tts(false);

let request = builder.try_into_request()?;

println!("{:?} {}", request.method(), request.path());

if let Some(body) = request.body() {
    println!("{}", str::from_utf8(body)?);
}

// Because a raw request is being performed, the output type must be
// explicit.
let response = client.request::<Message>(request).await?;

Required Methods

Try to convert a request builder into a raw Request.

Errors

Not all typed request builder conversions return an error and may instead always succeed. Refer to the documentation for each implementation for clarification.

Requests may return an error type of ErrorType::CreatingHeader if creating an audit log header value fails.

Requests may return an error type of ErrorType::Json if serializing a request body fails.

Implementors