Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set Byte payload config #1469

Closed
stevemk14ebr opened this issue Apr 22, 2020 · 8 comments
Closed

How to set Byte payload config #1469

stevemk14ebr opened this issue Apr 22, 2020 · 8 comments

Comments

@stevemk14ebr
Copy link
Contributor

stevemk14ebr commented Apr 22, 2020

I cannot figure out how to set the payload limit for the Bytes type. I'm trying to use

async fn view_index_features(state: ActixAppState, _: HttpRequest, body: Bytes) -> Result<HttpResponse, Error> {
 ...blah...

}

App::new()
     .wrap(middleware::Logger::default())
     .app_data(app_state.clone())
     .service(web::resource("/index/")
         .app_data(web::PayloadConfig::new(1000000 * 250))
         .route(web::put().to(view_index_features)))

But this gives an app_data is not configured error. If i do .data() on the app itself the payload limit is not respected and i get a 413: A payload reached size limit. I'm on the latest actix 2 version

@stevemk14ebr
Copy link
Contributor Author

stevemk14ebr commented Apr 22, 2020

Wait... can you not have a stateful app, and also configure route upload settings? I got it working if i do the app_data at the App level but it doesn't work at the service level. How do i get different configs at the service level

@Jonathas-Conceicao
Copy link
Member

Could you try using FromRequest::configure for it? Something like:

.app_data(actix_web::web::Bytes::configure(|cfg| {
    cfg.limit(1000000 * 250) // Set max file size to 6 Mib
}))

Documentation for JsonConfigure has a more complete example for it.

@stevemk14ebr
Copy link
Contributor Author

I get the same error about app data not being configured if i do that. I believe this is because service app_data overrides app level app_data but i am not sure. I have to add the config as an app level config but that effects all routes.

@stevemk14ebr
Copy link
Contributor Author

stevemk14ebr commented May 26, 2020

@robjtede i know you did some work with this data layer stuff. Is the behavior i am seeing expected. A working configuration is:

    let srv = HttpServer::new(move || {
            App::new()
                .wrap(NormalizeSlash)
                .wrap(middleware::Logger::default())
                .wrap(Cors::default())
                .app_data(app_state.clone())
                .app_data(web::PayloadConfig::new(1000000 * 250))
                .app_data(web::Json::<ViewMultFeatureSamplesInput>::configure(|cfg| {
                    cfg.limit(1000000 * 25).error_handler(json_error_handler) 
                }))

The json config must be specified at the top level .app_data. If i move it to any route below i get a runtime error about data not being configured and it appears to 'unregister' my app_state.clone() data line.

@cybuch
Copy link

cybuch commented Jun 4, 2020

If you call HTTP client that will have response larger than 256kb then you should also set limit for that response e.g.

            request.send()
                .await
                .map_err(Error::from)?
                .body()
                .limit(1024)

@robjtede
Copy link
Member

Just following up @stevemk14ebr, this might be solved by the changes made to data stacking in v3 beta.

@robjtede
Copy link
Member

I believe this issue is fixed. Please open a new issue if further problems arise.

@kpcyrd
Copy link

kpcyrd commented Apr 5, 2022

I've opened a discussion on how to do this with actix-web 4: #2722

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants