31 releases

Uses old Rust 2015

0.13.0 Oct 1, 2020
0.12.3 Mar 28, 2019
0.12.2 Sep 20, 2017
0.12.1 Mar 11, 2017
0.5.0 Jul 9, 2015

#1401 in Parser implementations

Download history 111/week @ 2023-12-18 32/week @ 2023-12-25 19/week @ 2024-01-01 51/week @ 2024-01-08 31/week @ 2024-01-15 30/week @ 2024-01-22 11/week @ 2024-01-29 19/week @ 2024-02-05 31/week @ 2024-02-12 110/week @ 2024-02-19 141/week @ 2024-02-26 79/week @ 2024-03-04 129/week @ 2024-03-11 129/week @ 2024-03-18 66/week @ 2024-03-25 781/week @ 2024-04-01

1,129 downloads per month
Used in 13 crates (12 directly)

MIT license

32KB
548 lines

formdata

Build Status MIT licensed

Documentation is available at https://mikedilger.github.io/formdata

This library provides a type for storing multipart/form-data data, as well as functions to stream (read or write) such data over HTTP.

multipart/form-data format as described by RFC 7578. HTML forms with enctype=multipart/form-data POST their data in this format. This enctype is typically used whenever a form has file upload input fields, as the default application/x-www-form-urlencoded cannot handle file uploads.

Whether reading from a stream or writing out to a stream, files are never stored entirely in memory, but instead streamed through a buffer.


lib.rs:

This library provides a type for storing multipart/form-data data, as well as functions to stream (read or write) such data over HTTP.

multipart/form-data format as described by RFC 7578. HTML forms with enctype=multipart/form-data POST their data in this format. This enctype is typically used whenever a form has file upload input fields, as the default application/x-www-form-urlencoded cannot handle file uploads.

Whether reading from a stream or writing out to a stream, files are never stored entirely in memory, but instead streamed through a buffer.

Read Example

extern crate mime;
extern crate hyper;
extern crate formdata;

use hyper::server::{Server, Request, Response};

fn main() {
  let server = Server::http("0.0.0.0:0").unwrap().handle(handler).unwrap();
}

fn handler(hyper_request: Request, res: Response) {
  let (_, _, headers, _, _, mut reader) = hyper_request.deconstruct();
  let form_data = formdata::read_formdata(&mut reader, &headers).unwrap();

  for (name, value) in form_data.fields {
    println!("Posted field name={} value={}", name, value);
  }

  for (name, file) in form_data.files {
    println!("Posted file name={} path={:?}", name, file.path);
  }
}

Write Example

extern crate mime;
extern crate hyper;
extern crate formdata;

use std::path::Path;
use hyper::header::{Headers, ContentType};
use mime::{Mime, TopLevel, SubLevel};
use formdata::{FormData, FilePart};

fn main() {
  let mut stream = ::std::io::stdout();
  let mut photo_headers = Headers::new();
  photo_headers.set(ContentType(Mime(TopLevel::Image, SubLevel::Gif, vec![])));
  // no need to set Content-Disposition (in fact it will be rewritten)

  let formdata = FormData {
    fields: vec![ ("name".to_owned(), "Baxter".to_owned()),
                  ("age".to_owned(), "1 month".to_owned()) ],
    files: vec![ ("photo".to_owned(), FilePart::new(
         photo_headers, Path::new("/tmp/puppy.gif"))) ],
  };

  let boundary = formdata::generate_boundary();
  let count = formdata::write_formdata(&mut stream, &boundary, &formdata).unwrap();
  println!("COUNT = {}", count);
}

Dependencies

~8–19MB
~276K SLoC