webrequest get/post

API -

GET

return function()
    local exampleHeaders = { myHeader = 'theHeaderValue' };
    local dataExample = "hello this string gets turned into a byte array behind the scenes."
    
    -- If we were using headers and body:
    -- local response = getWebRequest("https://type.fit/api/quotes", exampleHeaders, dataExample);
    
    -- without headers or body - set them as nil
    local response = getWebRequest("https://type.fit/api/quotes", nil, nil);
    local jsonObject = json.parse(response);
    
    --this website returns a json blob of quotes and authors. here we are logging the first quote.
    log(jsonObject[1].text); 
end

POST

return function()
    local headers = {};
    headers["Content-Type"] = "application/json";
    local body = '{ "Id": 78912 }';
    local responseText, responseHeaders = postWebRequest(
    'https://reqbin.com/sample/post/json',
    headers, body);
    
    -- log the response as string
    --{"success":"true"}
    log(responseText);
    local parseJson = json.parse(responseText);
    log(parseJson.success);
    local backToJson = json.serialize(parseJson);
    log(backToJson);
    
    
    -- uncomment below log all response headers
    --[[
    for k, v in pairs(responseHeaders) do
        log(k .. ': ' .. v);
    end
    ]]
end

Last updated