Use network.request "POST" to send data with form-data

Hello!

Lately I’ve been trying to send infos using form-data, as the mail server setup doesn’t recognize parameters in url (name=me&size=3).

I finally managed to make it work, so I thought I’d share my solution with you, in case anyone need it one day.

You’ll need

luasocket

lua-multipart-post (https://github.com/catwell/lua-multipart-post)

Server url

Init

local http = require "socket.http" local ltn12 = require "ltn12" local socket = require("socket") local enc = (require "yourPath.multipart-post").encode

Request

local url = “your\_url” local email = “sender\_email@provider.com” --(if you want to get your player’s email) local message = “hello world, this game is great” local bodyReq, boundary = enc{ email = email, message = tostring(message), device = system.getInfo("name"), version = system.getInfo("appVersionString"), lang = “fr”, country = “FR”, os = tostring(system.getInfo("platformVersion")), app\_name = system.getInfo("appName"), os\_name = system.getInfo("platformName") } local headers = {} headers["Content-Type"] = "multipart/form-data; charset=utf-8; boundary="..boundary network.request( url, "POST", networkListener, {body=bodyReq, headers = headers})

The “boundary” part is explained in details here => http://stackoverflow.com/a/30194096

But to put it simply, you specify with string is the separator between the fields in your form.

Listener

local function networkListener( event )    if ( event.isError ) then        print( "Network error: ", event.response )    else        print ( "RESPONSE: " .. tostring(event.response))    end end

Hope this can help, have a great day!

Thanks for sharing!

Thanks for sharing!