{"id":13896687,"url":"https://github.com/JakobGreen/lua-requests","last_synced_at":"2025-07-17T13:30:53.640Z","repository":{"id":29614829,"uuid":"33155338","full_name":"JakobGreen/lua-requests","owner":"JakobGreen","description":"Requests for Lua!","archived":false,"fork":false,"pushed_at":"2020-12-02T17:26:07.000Z","size":62,"stargazers_count":212,"open_issues_count":11,"forks_count":35,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-08-07T18:40:41.588Z","etag":null,"topics":["digest-authentication","http","lua","luarocks"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JakobGreen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-31T00:17:53.000Z","updated_at":"2024-08-04T05:59:58.000Z","dependencies_parsed_at":"2022-08-03T13:15:28.857Z","dependency_job_id":null,"html_url":"https://github.com/JakobGreen/lua-requests","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JakobGreen%2Flua-requests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JakobGreen%2Flua-requests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JakobGreen%2Flua-requests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JakobGreen%2Flua-requests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JakobGreen","download_url":"https://codeload.github.com/JakobGreen/lua-requests/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226265522,"owners_count":17597223,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["digest-authentication","http","lua","luarocks"],"created_at":"2024-08-06T18:03:05.245Z","updated_at":"2024-11-25T02:31:13.026Z","avatar_url":"https://github.com/JakobGreen.png","language":"Lua","readme":"# lua-requests\n\n[![License](http://img.shields.io/badge/License-MIT-brightgreen.svg)](LICENSE.md)\n[![Coverage Status](https://coveralls.io/repos/github/JakobGreen/lua-requests/badge.svg?branch=master)](https://coveralls.io/github/JakobGreen/lua-requests?branch=master)\n[![Build Status](https://travis-ci.org/JakobGreen/lua-requests.svg?branch=master)](https://travis-ci.org/JakobGreen/lua-requests)\n\nThe same friendly Python Requests interface for Lua!\n\n## Basic Usage\n\n\t\u003e requests = require('requests')\n\t\u003e response = requests.get('http://httpbin.org/get')\n\t\u003e print(response.status_code)\n\t200\n\t\u003e\n\t\u003e response = requests.post{'http://httpbin.org/post', data='random data'}\n\t\u003e json_data = response.json()\n\t\u003e print(json_data.data)\n\trandom data\n\n## Contents\n\n[API](#api)\n+ [Simple requests](#simple-requests)\n+ [HTTPS](#https)\n+ [Basic Response](#basic-response)\n+ [URL parameters](#url-parameters)\n+ [Sending Data](#sending-data)\n+ [Custom headers](#custom-headers)\n+ [Timeout](#timeout)\n+ [Basic Authentication](#basic-authentication)\n+ [Digest Authentication](#digest-authentication)\n+ [Cookies](#cookies)\n+ [JSON Response](#json-response)\n+ [XML Response](#xml-response)\n+ [Proxy](#proxy)\n+ [Redirects](#redirects)\n\n[Dependencies](#dependencies)\n\n[Tests](#tests)\n\n[License](#licensing)\n\n## Dependencies\n\n- [LuaSocket](http://w3.impa.br/~diego/software/luasocket/ \"LuaSocket homepage\")\n- [LuaSec](https://github.com/brunoos/luasec \"LuaSec github\")\n- [md5](https://github.com/kikito/md5.lua \"md5 github\")\n- [lbase64](https://github.com/LuaDist/lbase64 \"lbase64 github\")\n- [lua-cjson](http://www.kyne.com.au/~mark/software/lua-cjson.php)\n- [xml](https://lubyk.github.io/lubyk/xml.html)\n\nThe HTTP backend can be swapped out for anything that has the same API as LuaSocket's `socket.http`. This is done by setting the value of `requests.http_socket`. Swapping the HTTPS backend can be done by swapping out `requests.https_socket`.\n\n## Tests\n\nTests are located in the tests directory and are written using [busted](http://olivinelabs.com/busted/ \"Busted home page\").\n\nInstall `busted`:\n\n\t$ luarocks install busted\n\nRun Tests:\n\n\t$ busted -p _tests tests\n\n## Licensing\n\n`lua-requests` is licensed under the MIT license. See LICENSE.md for details on the MIT license.\n\n\n## API\n\n### Simple requests\n\nImporting the lua-requests is quite simple.\n\n    \u003e requests = require('requests')\n\nMaking a GET request is not much more difficult.\n\n    \u003e response = requests.get('http://httpbin.org/get')\n\nOther methods like POST, HEAD, OPTIONS, TRACE, PATCH, PUT, and DELETE are just as simple.\n\n    \u003e response = requests.post('http://httpbin.org/post')\n    \u003e response = requests.put('http://httpbin.org/put')\n    etc...\n\nThe second argument of a request is a table that can be used to make more advanced requests.\nAny request can be made with either a second argument or as a table.\n\n    \u003e response = requests.post{url = 'http://httpbin.org/post', data = 'random data'}\n\nor\n\n    \u003e response = requests.post{'http://httpbin.org/post', data = 'random data'}\n\nor\n\n    \u003e response = requests.post('http://httpbin.org/post', {data = 'random data'})\n\n\nNOTE: This documentation mostly uses two parameters instead of just one table because the single table feature was added later.\n\nThere is also a general request call. The first parameter is the method.\n\n    \u003e response = requests.request(\"GET\", 'http://httpbin.org/get')\n    \n### HTTPS\n\nUsing HTTPS is as simple as changing the URL to be 'https' instead of 'http'\n\n    \u003e response = requests.get('https://httpbing.org/get')\n\n### Basic Response\n\nThe http response contains all of the response data in different fields.\n\nThe response body is contained in `response.text`\n\n    \u003e response = response.get('http://httpbin.org/robots.txt')\n    \u003e print(response.text)\n    User-agent: *\n    Disallow: /deny\n\nThe response headers are contained in `response.headers`\n\n    \u003e response = requests.get('http://httpbin.org/robots.txt')\n    \u003e print(inspect(response.headers))\n    {\n      [\"access-control-allow-credentials\"] = \"true\",\n      [\"access-control-allow-origin\"] = \"*\",\n      connection = \"close\",\n      [\"content-length\"] = \"30\",\n      [\"content-type\"] = \"text/plain\",\n      date = \"Tue, 07 Apr 2015 01:43:26 GMT\",\n      server = \"nginx\"\n    }\n\nThe response status code is contained in `response.status_code`\n\n    \u003e response = requests.get('http://httpbin.org/robot.txt')\n    \u003e print(response.status_code)\n    200\n\n### URL Parameters\n\nIt is common for URL's that need to have some sort of query string. \nFor example, `http://httpbin.org/response-headers?key1=val1\u0026key2=val2`. \nAdding parameters to a URL query is as simple as passing a table into the params field of the second argument.\n\n\t\u003e query_parameters = { key1 = 'val1', key2 = 'val2' }\n\t\u003e response = requests.get{'http://httpbin.org/response-headers', params = query_parameters}\n\t\u003e print(response.url)\n\thttp://httpbin.org/response-headers?key1=val1\u0026key2=val2\n\nFor keys that contain a list of values just make the value into a table.\n\n\t\u003e query_parameters = { key1 = 'val2', key2 = {'val2', 'val3'}}\n\t\u003e response = requests.get{'http://httpbin.org/response-headers', params = query_parameters}\n\t\u003e print(response.url)\n\thttp://httpbin.org/response-headers?key1=val1\u0026key2=val2,val3\n\n### Sending Data\n\nSending data is possible with any command. Just pass the data you want to send into the data field of the second argument.\n\n\t\u003e data = \"Example data\"\n\t\u003e response = requests.post{'http://httpbin.org/post', data = data}\n\nIf a table is passed in to data it is automatically encoded as JSON.\n\n\t\u003e data = {Data = \"JSON\"}\n\t\u003e response = requests.post{'http://httpbin.org/post', data = data}\n\n### Custom headers\n\nCustom headers can be added to any request method. Just pass a table into the headers field of the second argument.\n\n\t\u003e headers = {['Content-Type'] = 'application/json'}\n\t\u003e response = requests.get{'http://httpbin.org/headers', headers = headers}\n\n### Timeout\n\nTimeout in seconds can be passed as a parameter. If the host has not responded in timeout seconds then through an error.\n\n\t\u003e url = 'http://httpbin.org/delay/2'\n\t\u003e response = requests.get{url, timeout = 1}\n\trequests.lua:261: error in GET request: timeout\n\n### Basic Authentication\n\nBasic authentication can be added to any request.\n\n\t\u003e auth = requests.HTTPBasicAuth('user', 'passwd')\n\t\u003e response = requests.get{'http://httpbin.org/basic-auth/user/passwd', auth = auth}\n\t\u003e print(response.status_code)\n\t200 \n\n### Digest Authentication\n\nDigest authentication can be added to any request.\n\n\t\u003e auth = requests.HTTPDigestAuth('user', 'passwd')\n\t\u003e response = requests.get{'http://httpbin.org/digest-auth/auth/user/passwd', auth = auth}\n\t\u003e print(response.status_code)\n\t200 \n\nTo continue using the same digest authentication just pass `response.auth` into the next request.\n\n\t\u003e response = requests.get{'http://httpbin.org/digest-auth/auth/user/passwd', auth = response.auth}\n\t\u003e print(response.status_code)\n\t200\n\nBy reusing the `response.auth` you can save time by not needing to reauthenticate again.\n`response.cookies` contains cookies that the server requested to be set for authentication.\n\n### Cookies\n\nCookies can be added to any request by setting the `cookies` field.\n\n\t\u003e response = requests.get{'http://httpbin.org/get', cookies = 'cookie!'}\n\n### JSON Response\n\nJSON response's can be parsed into a Lua table using `response.json()`. \nJSON encoding and decoding is done with `lua-cjson`. \n\n\t\u003e response = requests.get{'http://httpbin.org/get', params =  {stuff=true}}\n\t\u003e json_body, error = response.json()\n\t\u003e print(json_body.args.stuff)\n\ttrue\t\n\n### XML Response\n\nXML response's can be parsed into a Lua table using `response.xml()`.\nXML encoding and decoding is done with `xml` which is based on RapidXML.\n\n\t\u003e response = requests.get('http://httpbin.org/xml')\n\t\u003e xml_body, error = response.xml()\n\t\u003e print(xml_body[1][1][1])\n\tWake up to WonderWidgets!\n\nThe returned xml table can be tricky to parse. \nI recommend using `inspect` to help the first time to help see the table structure.\n\n### Proxy\n\nA proxy server can be added as an argument to a request.\n\n\t\u003e response = requests.get{'http://httpbin.org/get', proxy = '8.8.8.8:9001'}\n\n### Redirects\n\n301 and 302 redirects are enabled by default for most requests. To disable redirects set `allow_redirects = false`.\n\n\t\u003e response = requests.get('http://httpbin.org/redirect-to?url=google.com', {allow_redirects = false})\n\n","funding_links":[],"categories":["Lua"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJakobGreen%2Flua-requests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FJakobGreen%2Flua-requests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FJakobGreen%2Flua-requests/lists"}