How can i send a multipart(file) to TestRequest? #2512
Unanswered
lpf763827726
asked this question in
Q&A
Replies: 2 comments 3 replies
-
the client doesn't have a way to construt multipart requests yet in the tests for actix-multipart we use a helper function like this: https://github.com/actix/actix-web/blob/HEAD/actix-multipart/src/server.rs#L963 |
Beta Was this translation helpful? Give feedback.
3 replies
-
It seems the testing API still doesn't provide any facility for Multipart file uploads as of version 4.4.0. I've added the following function to my test helper module: pub fn build_multipart_payload_and_header(file_name: &str, file_contents: &str) -> (String, (HeaderName, HeaderValue)) {
let boundary = "-----------------------------202022185716362916172375148227";
let payload = format!("{boundary}\r\n\
Content-Disposition: form-data; name=\"file\"; filename=\"{file_name}\"\r\n\
Content-Type: text/csv\r\n\
\r\n\r\n\
{file_contents}\r\n\r\n\
{boundary}--");
let header = (
actix_web::http::header::CONTENT_TYPE,
HeaderValue::from_static("multipart/form-data; boundary=---------------------------202022185716362916172375148227"),
);
(payload, header)
} and use it with: let file_name = "upload.json";
let file_contents = include_str!("../resources/files/upload.json");
let (payload, content_type_header) = build_multipart_payload_and_header(file_name, file_contents);
let req = test::TestRequest::post()
.uri("/YOUR_SERVICE")
.set_payload(payload.clone())
.insert_header(content_type_header)
.to_request();
let resp = test::call_service(&mut app, req).await; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
i try many way, but they are not success, i try to use
set_payload
, still no good, i need an example pleaseBeta Was this translation helpful? Give feedback.
All reactions