Coder Social home page Coder Social logo

easy-http's Introduction

easy-http

Easily create HTTP requests with lua

Installation

luarocks install easy-http

Sync Usage

Simple GET

local easyhttp = require("easyhttp")

local response, code, headers = easyhttp.request("https://httpbin.org/get")
print(response)
print(code)
print(headers)

Output:

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "***"
  },
  "origin": "***",
  "url": "https://httpbin.org/get"
}
200
table: 0x7f81d390a850

POST with custom headers

local easyhttp = require("easyhttp")

local response, code, headers = easyhttp.request("https://httpbin.org/post", {
    method = "POST",
    headers = {
        ["Content-Type"] = "application/json"
    },
    body = '{"key": "value"}'
})

print(response)
print(code)
print(headers)

Response:

{
  "args": {},
  "data": "{\"key\": \"value\"}",
  "files": {},
  "form": {},
  "headers": {
    "Accept": "*/*",
    "Content-Length": "16",
    "Content-Type": "application/json",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "***"
  },
  "json": {
    "key": "value"
  },
  "origin": "***",
  "url": "https://httpbin.org/post"
}

200
table: 0x7f81d2f44910

Timeout

local easyhttp = require("easyhttp")

local response, code, headers = easyhttp.request("https://httpbin.org/delay/5", {
    timeout = 2,

    --redirects are supported
    follow_redirects = true, --by default true
    max_redirects = 5, --by default, -1 (infinite)
})

print(response)
print(code)
print(headers)

Output:

nil
failed to perform request: Timeout was reached
nil

Output to file

local easyhttp = require("easyhttp")

local f = assert(io.open("output.json", "w+b"))
local response, code, headers = easyhttp.request("https://httpbin.org/get", {
    output_file = f
})

print(response)
print(code)
print(headers)

Output:

true
200
table: 0x7fe98630b4c0

Async Usage

Simple GET

local easyhttp = require("easyhttp")

local request = assert(easyhttp.async_request("https://httpbin.org/get"))
print(request)
io.write '\27[?25l'
while not request:is_done() do
    io.write '.':flush()
    local t0 = os.clock()
    while os.clock() - t0 <= 0.025 do end
end
io.write '\27[?25h'

local response, code, headers = request:response() --Response will wait until the request is done

print(response)
print(code)
print(headers)

Output:

easyhttp.AsyncRequest: 0x7fbd03722a88
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "X-Amzn-Trace-Id": "***"
  },
  "origin": "***",
  "url": "https://httpbin.org/get"
}

200
table: 0x7f81ef10a7e0

Cancel request

local easyhttp = require("easyhttp")
local request = assert(easyhttp.async_request("https://httpborg/delay/5"))

assert(request:cancel()) --Will return false, <message> if the request is already done

local response, code = request:response()
print(response)
print(code)

Output:

nil
request was cancelled

easy-http's People

Contributors

frityet avatar

Stargazers

Yakup Cemil KAYABAŞ avatar  avatar  avatar Komo avatar Andrew Chou avatar Bouchard Justin avatar Mateus Moutinho Queiroz avatar

Watchers

 avatar

easy-http's Issues

lua5.1 and lua5.3 error

requiring easyhttp from lua5.3 outputs this

	./easyhttp.lua:3: too many C levels (limit is 200) in main function near '"output.json"'

and requiring it from luajit returns

./easyhttp.lua:1: loop or previous error loading module 'easyhttp'
stack traceback:

I said lua5.1 and lua5.3 as those are the only lua versions i have that are setup to work with luarocks on my device, but it probably will return the same on 5.2 and 5.4

I am on macbook air intel 2020 version 12.6.6

It does work at requiring it when building it from source with both lua5.4 and lua5.1 (those are the two ones i tried)

sudo luarocks build

But when i run some sample code with it

	[C]: in ?
justinbouchard@MacBook-Air-de-justin ~ % lua5.4 easyhttp.lua 
lua5.4: error loading module 'easyhttp' from file './easyhttp.lua':
	C stack overflow

with both luajit and lua5.4 and lua5.1

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.