0

I'm making an api on Rust (rocket = "0.5.0-rc.3"), and when I start I see this: "limits: bytes = 8KiB, data-form = 2MiB, file = 1MiB, form = 32KiB, json = 1MiB, msgpack = 1MiB, string = 30MiB" I was interested in "file = 1MiB". I wanted to make this api able to accept files but couldn't. example of my api:

#[macro_use] extern crate rocket;
#[macro_use] extern crate rocket_contrib;

use rand::Rng;

mod generate_from_string;
mod generate_from_list;

#[get("/")]
fn index() -> String {
    json!({"message": "Hello, world!"}).to_string()
}

#[post("/one-line-generate", data = "<data>")]
fn generate(data: String) -> String {
    let num_words = rand::thread_rng().gen_range(1..=5);
    let buffer = String::from(data);
    let selected_words = generate_from_string::generate_message_from_file("", num_words, &buffer);
    json!({"data": selected_words}).to_string()
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index, generate])
}

I asked in ChatGPT but he did not help me

Duxtaa
  • 15
  • 3
  • I'm seeing a JSON interface in your code so far. If you're only writing a REST API, rocket may not be the best fit, as is is more focused on generating full HTML pages, though it also can send and receive JSON. Not sure if you're planning on writing templates and generating HTML pages server-side. If you are, rocket is a good fit. Otherwise, you might consider a framework like [`actix`](https://actix.rs/). Anyways, this is how you do files in rocket - https://rocket.rs/v0.5-rc/guide/upgrading/#multipart – Ross Rogers Jul 13 '23 at 16:20
  • @RossRogers That seems like an odd take, what makes Actix-web's handling of JSON superior to Rocket? OP isn't using the way you would normally use JSON in Rocket (you would use [`Json`](https://api.rocket.rs/v0.5-rc/rocket/serde/json/struct.Json.html)), but if they did the developer interface is nearly identical. – kmdreko Jul 13 '23 at 16:56
  • @kmdreko, Rocket is a fine and reasonable choice -- I simply think you'll find it easier going in a framework where folks are doing exactly what you're trying to do. – Ross Rogers Jul 13 '23 at 17:05
  • @RossRogers Yes, I'm using JSON because I didn't know that it's possible to send files in an API. But if it's possible, it would be very convenient. Which library is best suited for this purpose? – Duxtaa Jul 13 '23 at 20:21
  • [Rocket works](https://rocket.rs/v0.5-rc/guide/upgrading/#multipart). [actix web works.](https://docs.rs/actix-form-data/latest/actix_form_data/) – Ross Rogers Jul 13 '23 at 21:25

0 Answers0