Mailgun api keeps returning a 401

77 Views Asked by At

I need to make a request of this type:

curl -s --user 'api:YOUR_API_KEY' \
    https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
    -F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
    -F [email protected] \
    -F subject='Hello' \
    -F text='Testing some Mailgun awesomeness!

I'm using reqwest's basic_auth() method for the authentication and form() for the data but i'm getting a 401 error and i don't actually know what the problem is.

The api key is correct since i'm able to make requests by using the mailgun_sdk crate.

Here's my code:

async fn foo(configuration: &Settings) {
    let client=reqwest::Client::new();

    let form=HashMap::from([
        ("from", "[email protected]"),
        ("to"  , "[email protected]"),
        ("subject", "test"),
        ("text","OK")
    ]);

    let _=client.post("https://api.eu.mailgun.net/v3/<sandbox>/messages")
        .basic_auth("api",Some(&configuration.email_client.api_key))
        .form(&form)
        .send()
        .await
        .unwrap();


    // THIS WORKS!
    // let params= mailgun_sdk::send_message::SendMessageParamList::default()
    //     .add(SendMessageParam::From("[email protected]"))
    //     .add(SendMessageParam::To("[email protected]"))
    //     .add(SendMessageParam::Text("OK"))
    //     .add(SendMessageParam::Subject("Test"));
    //
    // let _= mailgun_sdk::Client::new(&configuration.email_client.api_key,"<sandbox>")
    //     .send_message(params);
}
0

There are 0 best solutions below