POST data in Lua - post

I'm learning Lua at the moment. I need to be able to access post and get data. I'm trying to find out how the equivalent of PHP $_POST and $_GET in Lua.

This depends on the web server you are running in, and any intermediary libraries you are using.
In Apache 2.3, using the included mod_lua, it would be
function my_handler(r)
-- URI params
local simple, full = r:parseargs()
-- POST body
local simple, full = r:parsebody()
end
Where simple is a table of key -> value (what you want most of the time) and full is key -> [value1, value2, ...] for cases of duplicately named params.
Fuller examples are available at http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/test/htdocs/test.lua?revision=728494&view=markup

There are many web-frameworks for Lua, each with its own way of accessing GET and POST.
Probably easiest way to learn Lua for the web-development is to use WSAPI.
To get GET and POST, use wsapi.request in your handler:
require 'wsapi.request'
local handler = function(env)
local request = wsapi.request.new(env)
local GET = wsapi.request.GET
local POST = wsapi.request.POST
...
end

There is no equivalent as Lua is not designed as a web scripting language. In what context are you using this (CGI, FCGI, Apache module)? You'll probably need to look into the CGI specification and accessing environment variables and stdin from Lua.

You could always check out Lua4Web https://github.com/schme16/Lua4Web

Reading POST data in traditional html-form or url-encoded format is a mess in Lua. Better you try to use AJAX forms javascript library, so you get your data sent in JSON back to the server where you can easily parse and use.

Related

Accept/Content-Type header based processing in Quart and Quart-Schema

Because I am rewriting a legacy app, I cannot change what the clients either send or accept. I have to accept and return JSON, HTML, and an in-house XML-like serialization.
They do, fortunately set headers that describe what they are sending and what they accept.
So right now, what I do is have a decoder module and an encoder module with methods that are basically if/elif/else chains. When a route is ready to process/return something, I call the decoder/encoder module with the python object and the header field, which returns the formatted object as a string and the route processes the result or returns Response().
I am wondering if there is a more Quart native way of doing this.
I'm also trying to figure out how to make this work with Quart-Schema. I see from the docs that one can do app.json_encoder = <class> and I suppose I could sub in a different processor there, but it seems application global, there's no way to set it based on what the client sends. Optimally, it would be great if I could just pass the results of a dynamically chosen parser to Quart-Schema and let it do it's thing on python objects.
Thoughts and suggestions welcome. Thanks!
You can write your own decorator like the quart-schema #validation_headers(). Inside the decorator, check the header for the Content-Type, parse it, and pass the parsed object to the func(...).

Ruby on Rails basic use of RiotGames API (need explanation, solution already found)

First you must know I'm a total beginner, I'm trying to learn so I almost don't know anything.
On the basic page of the API, there is a curl command used as an example to show us how to make requests.
I'm using Ruby on Rails so I used "curl-to-ruby" website to translate it, but it did not work as expected.
I wanted it to show me this :
uri = URI.parse("REQUEST_URL")
response = JSON.parse(Net::HTTP.get(uri))
Instead I got this :
uri = URI.parse("REQUEST_URL")
response = Net:HTTP.get_response(uri)
I don't understand any of this, I thought I wouldn't need to and just use "curl-to-ruby", but apparently I really need to get this.
Would you please try to explain me ?
Or give me links ?
Or matters to read (curl, API, http) ?
Thank you very much, have a nice day.
It's because that command doesn't return just the content, it returns the whole HTTP response object including headers and body. You need to extract the response body and parse that using JSON.parse(), e.g.
JSON.parse(response.body)
See documentation here: https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response
(Also, there is nothing in the cURL command which would hint to the converter that the content-type of the response was expected to be JSON (e.g. perhaps an "accepts" header or something), so even if it were able to produce extra code adding the JSON.parse part, it has no way of knowing that it would be appropriate to do so in this case.)

Why params is less common used than query strings in HTTP URL?

I'm reading the book, HTTP - The Definitive Guide, from which I get the URL general format:
<scheme>://<user>:<password>#<host>:<port>/<path>;<params>?<query>#<frag>
The <params> part said,
The path component for HTTP URLs can be broken into path segments. Each segment can have its own params. For example:
http://www.joes-hardware.com/hammers;sale=false/index.html;graphics=true
In my opinion, path params can also be used to query resources like query strings, but why it's barely seen?
And I'm a Rails developer, and I haven't seen its usage or specification in Rails. Does Rails not support it?
You ask several questions
Why do we not see ;params=value much?
Because query parameters using ?=& are widely supported, like in PHP, .net, ruby etc.. with convenient functions like $_GET[].
While params delimited by ; or , do not have these convenient helper functions. You do encounter them at Rest api's, where they are used in the htaccess or the controller to get relevant parameters.
Does Ruby support params delimited with ;?
Once you obtain the current url, you can get all parameters with a simple regex call. This is also why they are used in htaccess files, because they are easily regexed (is that a word?).
Both parameter passing structures are valid and can be used, the only clear reason why one is used more often than the other is because of preference and support in the different languages.

Lua & Lighttpd - $_GET equivalent

What's Lua's equivalent to php's $_GET for a web application?
Also if the url is something like index.cgi?thisisatest how can I get everything after the question mark?
In the context of lighttpd and mod_magnet, query strings are not parsed automatically so you need to do it yourself. You can find an example here, look for "flv-streaming.lua" in the page.
As for your second question, Lorenzo gave you a generic answer, but in mod_magnet you can also use lighty.env["uri.query"] as seen in the same example.
Lua in itself is not a language for web development. There are some libraries for that. You can try luasocket.
As for your second question:
local url = "index.cgi?thisisatest"
local suffix = string.match( url, "^[^?]+?([^?]-)$" )
print( suffix )
If you are running your Lua code in Ophal, then you can use the functions: request_uri() and request_path()
If you're using lua as a CGI script, os.getenv("QUERY_STRING") will return everything after the question mark/

Get request URI in jinja2?

I'm working on a Pylons project using Jinja2 templating. I want to test for the request URI and/or controller inside the Jinja2 templates - is there an equivalent to a getRequestUri() call? I can set a context variable as a flag inside all the controller methods to do what I want, but that seems a bit like writing my home address on each and every one of my house keys... i.e. not quite the right way to do it.
Solution: not quite a function call, but I can test against url.environ.PATH_INFO. It only gives me the URL path, not the hostname, and I don't know that it would give me the query string, but it gives me what I need.

Resources