Git Product home page Git Product logo

lua-multipart-post's Introduction

multipart-post

Presentation

HTTP Multipart Post helper that does just that.

Dependencies

The module itself only depends on luasocket (for ltn12).

Tests require cwtest, a JSON parser and the availability of httpbin.org.

Usage

local mp = (require "multipart-post").gen_request
local H = (require "socket.http").request
local rq = mp{myfile = {name = "myfilename", data = "some data"}}
rq.url = "http://httpbin.org/post"
local b,c,h = H(rq)

to return response

local mp = (require "multipart-post").gen_request
local H = (require "socket.http").request
local rq = mp{myfile = {name = "myfilename", data = "some data"}}
rq.url = "http://httpbin.org/post"
local res = {}
rq.sink = (require "ltn12").sink.table(res)
local b,c,h = H(rq)
-- response is res
print(table.concat(res))

See LuaSocket's http.request (generic interface) for more information.

If you only need to get the multipart/form-data body use encode:

local enc = (require "multipart-post").encode
local body, boundary = enc{foo="bar"}
-- use `boundary` to build the Content-Type header

Advanced Usage

Example using ltn12 streaming via file handles

local file = io.open("myfilename", "r")
local file_length = file:seek("end")
file:seek("set", 0)

local rq = mp{
	myfile = {name = "myfilename", data = file, len = file_length},
}

Example using ltn12 source streaming

ltn12 = require("socket.ltn12")

local rq = mp{
	myfile = {name = "myfilename", data = ltn12.source.string("some data"), len = string.len("some data")}
}
rq.url = "http://httpbin.org/post"
local b,c,h = H(rq)

Bugs

Non-ASCII part names are not supported. According to RFC 2388:

Note that MIME headers are generally required to consist only of 7- bit data in the US-ASCII character set. Hence field names should be encoded according to the method in RFC 2047 if they contain characters outside of that set.

Note that non-ASCII file names are supported since version 1.2.

Contributors

  • Pierre Chapuis (@catwell)
  • Cédric Deltheil (@deltheil)
  • TJ Miller (@teejaded)
  • Rami Sabbagh (@RamiLego4Game)

Copyright

  • Copyright (c) 2012-2013 Moodstocks SAS
  • Copyright (c) 2014-2020 Pierre Chapuis

lua-multipart-post's People

Contributors

catwell avatar kute avatar rami-sabbagh avatar teejaded avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.