poem icon indicating copy to clipboard operation
poem copied to clipboard

poem_openapi post request with form data always returns 400

Open SaculRennorb opened this issue 1 year ago • 2 comments

Expected Behavior

A normal response

Actual Behavior

see title

Steps to Reproduce the Problem

Set up something along the lines of

#[poem_openapi::OpenApi]
impl Api {
    #[oai(path = "/items", method = "post")]
    async fn get_items_f(&self, data : Form<IdsRequest>) -> Json<Vec<&Item>> {
          Json(vec![]) // empty dummy response
    }
}

#[derive(Debug, serde::Deserialize, poem_openapi::Object)]
struct IdsRequest {
    pub ids : Vec<u32>
}

// dummy return object
#[derive(Debug, poem_openapi::Object)] struct Item { }

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_service = poem_openapi::OpenApiService::new(Api, "API", "0.0.0")
        .server("http://localhost:3000");

    let route = poem::Route::new().nest("/", api_endpoint);

    poem::Server::new(poem::listener::TcpListener::bind("127.0.0.1:3000")).run(route).await?;

    Ok(())
}

Disclaimer: This is taken in chunks form a much larger project and therefore might not actually compile like this although it looks good to me.

If you now send an request to this (for example with

curl -X 'POST' \
  'http://localhost:3000/items' \
  -H 'accept: application/json; charset=utf-8' \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -d 'ids=14214'

) it will just spit back a 400 error.

Ive tried playing around with tracing_subscriber and the Tracing middleware, but to no avail.

Hopefully you can make something of this.

I also want to add that i couldn't find an official example for how to even use the Form wrapper, so this might just be a case of user error.

Specifications

  • Version: poem = "1.3.58"; poem-openapi = 3.0.5
  • Platform: Win10-x86_64 (uncertain if it affects others aswell, but likely)
  • Subsystem: poem-openapi

SaculRennorb avatar Sep 06 '23 10:09 SaculRennorb

I don't recommend you to use Form type in Poem-openapi, because it is currently not well supported, I will improve it later. 🙂

sunli829 avatar Sep 06 '23 11:09 sunli829

Ah, very well. Maybe you should add a disclaimer to it for now? Either way, thank you for the quick response ^^

SaculRennorb avatar Sep 06 '23 11:09 SaculRennorb