Convert curl to Lua

curl2code converts curl commands to Lua code using the http library. The conversion runs in your browser via WebAssembly for complete privacy. For Python, check our curl to Python converter. For Ruby, see curl to Ruby. Below are practical Lua examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is Lua http?

The lua-http (or socket.http from LuaSocket) is the standard way to make HTTP requests in Lua. It provides basic GET, POST, and custom request methods. For more features, lua-resty-http is popular in OpenResty/Nginx environments. Install with luarocks install http. curl2code generates code using the http module.

socket.http vs lua-resty-http vs lua-curl — which to use?

socket.http (LuaSocket) is the standard for general Lua. lua-resty-http is for OpenResty/Nginx with non-blocking I/O. lua-curl wraps libcurl for full protocol support. Use socket.http for standalone scripts, resty-http for web servers. For similar scripting, see curl to Python or curl to Ruby.

How to handle authentication in Lua?

Set the Authorization header in the headers table: headers = { Authorization = "Bearer token" }. For Basic auth, encode with a base64 library: "Basic " .. base64.encode(user .. ":" .. pass). Pass headers in the request options table. curl2code handles -u and header flags.

How to send multipart form data in Lua?

Construct the multipart body manually with boundary strings, or use a helper library like multipart-post. Set the Content-Type header with the boundary. LuaSocket's http.request accepts a source function for streaming large uploads. curl2code converts -F flags to Lua syntax.

How to handle HTTP errors in Lua?

LuaSocket's http.request returns multiple values: body, code, headers, status = http.request(url). Check code for the HTTP status (200, 404, etc.) or nil on network failure. Use Lua's assert() or if not body then pattern for error checking.

How to parse JSON in Lua?

Use the cjson module (fast, C-based): local data = cjson.decode(json_string). For encoding: cjson.encode(table). In OpenResty, cjson is pre-installed. For standalone Lua, install with luarocks install lua-cjson. Alternatively, use pure-Lua dkjson.

How to set timeouts in Lua?

With LuaSocket: set http.TIMEOUT = 30 globally, or create a custom connection and call :settimeout(30). In lua-resty-http, use httpc:set_timeout(30000) (milliseconds). Timeouts prevent scripts from hanging indefinitely on unresponsive servers.

How to use a proxy in Lua?

With LuaSocket, set the proxy field in the request options: http.request{ url = target, proxy = "http://proxy:8080" }. For lua-resty-http, connect to the proxy directly and use the CONNECT method for HTTPS tunneling. curl2code converts -x flags to the appropriate Lua proxy setup.

How to send a POST request with JSON body in Lua?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to Lua using curl2code. The generated code uses http.request(url, json_body, headers). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the http code.

How to add Bearer token authorization in Lua?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates http code with the auth header: headers['Authorization'] = 'Bearer YOUR_TOKEN'. curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in Lua?

curl2code converts curl -H "Content-Type: application/json" URL to http code with the proper header: headers['Content-Type'] = 'application/json'. For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct http API.

Useful Links

curl Guides

Convert curl to Other Languages