daurnimator / lua-http

HTTP Library for Lua. Supports HTTP(S) 1.0, 1.1 and 2.0; client and server.
https://daurnimator.github.io/lua-http/
MIT License
778 stars 80 forks source link

lua-http with OAuth2 example #171

Closed rshawnkeller closed 3 years ago

rshawnkeller commented 3 years ago

does anyone have an example of making a simple API endpoint call , with an Oauth2 Bearer token?

daurnimator commented 3 years ago

What is the difficulty?/What have you tried so far?

rshawnkeller commented 3 years ago

although this library has a good set of documentation, i have not found any examples that show how to grab a token or use it.

daurnimator commented 3 years ago

grab a token from what? and you'd use the token exactly how you would in any other http client...

rshawnkeller commented 3 years ago

trying to get an oauth 2 token (grant_type = password) from an API that requires it, using lua, unfortunately. yes, most clients like ruby, java, postman, etc., it is easy, but lua is not where those other languages or have the community behind them. just looking for a code sample

jprjr commented 3 years ago
local request = require('http.request')
local util = require('http.util')

local req = request.new_from_uri('https://your-oauth-endpoint')
req.headers:upsert(':method','POST')
req.headers:upsert('somekind-of-header','some-kind-of-header-value')
req:set_body(util.dict_to_query({some_param = some_value, some_other_param = some_other_value}))

local headers, stream = req:go()
local body, err = stream:get_body_as_string()
do_stuff_with(body)

At the end of the day it's just making an HTTP request, to an endpoint, with some headers

Akkilah commented 3 years ago

@jprjr Thanks for the example, I have been looking all day now, wondering where I can find an article or tutorial to show how an HTTP request is constructed to do REST API, I mean I know how to use client and such, but how the REST client composed it and make the HTTP request.

I hope if you can help me find a good tutorial, article, or give me the right terminology to look for this stuff