twilight-rs / twilight

Powerful, flexible, and scalable ecosystem of Rust libraries for the Discord API.
https://discord.gg/twilight-rs
ISC License
673 stars 132 forks source link

Example on `create_guild_sticker` doesn't work #1954

Open laralove143 opened 2 years ago

laralove143 commented 2 years ago

Running the example at https://api.twilight.rs/twilight_http/request/guild/sticker/struct.CreateGuildSticker.html#examples with a proper token and guild ID returns Error: Response error: status code 400, error: {"message": "Invalid Asset", "code": 50046}, I'm not sure what to put in the file parameter

laralove143 commented 2 years ago

Actually I think this is a bug, sending the request manually is done successful with the same file

POST https://discord.com/api/v10/guilds/903367565349384202/stickers
Content-Type: multipart/form-data; boundary=WebAppBoundary
Authorization: Bot snip

--WebAppBoundary
Content-Disposition: form-data; name="name"
Content-Type: text/plain

testing sticker
--WebAppBoundary
Content-Disposition: form-data; name="description"
Content-Type: text/plain

testing sticker description
--WebAppBoundary
Content-Disposition: form-data; name="tags"
Content-Type: text/plain

testing,sticker,tags
--WebAppBoundary
Content-Disposition: form-data; name="file"; filename="output.png"
Content-Type: image/png

< /Users/lara/Downloads/output.png
--WebAppBoundary--

but running its (apparently) counterpart in Twilight returns that error

let http = Client::new(env!("TEST_BOT_TOKEN").to_owned());
let guild_id = Id::new(903367565349384202);

http.create_guild_sticker(
    guild_id,
    "testing sticker",
    "testing sticker description",
    "testing,sticker,tags",
    "/Users/lara/Downloads/output.png".as_bytes(),
)?
.exec()
.await?
.model()
.await?;

inlining the file's content doesn't work either

let http = Client::new(env!("TEST_BOT_TOKEN").to_owned());
let guild_id = Id::new(903367565349384202);

http.create_guild_sticker(
    guild_id,
    "testing sticker",
    "testing sticker description",
    "testing,sticker,tags",
    &read("/Users/lara/Downloads/output.png")?,
)?
.exec()
.await?
.model()
.await?;
vilgotf commented 2 years ago

What's the debug output of Request? (Turn the CreateGuildSticker struct into one with the TryIntoRequest trait)

laralove143 commented 2 years ago

strings built from utf8 and \r\n's replaced:

Request {
    body: None,
    form: Some(
        Form {
            boundary: "9dD9Oho7LuPcyOe",
            buffer: "
--9dD9Oho7LuPcyOe
Content-Disposition: form-data; name=\"description\"

testing sticker description
--9dD9Oho7LuPcyOe
Content-Disposition: form-data; name=\"file\"

/Users/lara/Downloads/output.png
--9dD9Oho7LuPcyOe
Content-Disposition: form-data; name=\"name\"

testing sticker
--9dD9Oho7LuPcyOe
Content-Disposition: form-data; name=\"tags\"

testing,sticker,tags
--9dD9Oho7LuPcyOe",
        },
    ),
    headers: None,
    method: Post,
    path: "guilds/903367565349384202/stickers",
    ratelimit_path: GuildsIdStickers(
        903367565349384202,
    ),
    use_authorization_token: true,
}

so i think the bug is the file form doesn't mention the filename or content type

7596ff commented 2 years ago

I was unable to test this at the time and it went untested. So it indeed is untested. I can have a look at it sometime soon

laralove143 commented 2 years ago

Is there any updates on this?

zeylahellyer commented 1 year ago

I have a fix for this, I will PR it soon.