cjoudrey / gluahttp

gluahttp: an http module for gopher-lua
MIT License
58 stars 23 forks source link

How to add `Content-Type` header #30

Closed wipsel closed 11 months ago

wipsel commented 11 months ago

Maybe this is a silly question but I'm trying to use this and I'm having trouble setting the request headers.

I used the example in the readme:

import "github.com/yuin/gopher-lua"
import "github.com/cjoudrey/gluahttp"

func main() {
    L := lua.NewState()
    defer L.Close()

    L.PreloadModule("http", NewHttpModule(&http.Client{}).Loader)

    if err := L.DoString(`

        local http = require("http")

        response, error_message = http.request("GET", "http://example.com", {
            query="page=1",
            timeout="30s",
            headers={
                Accept="*/*",
                Content-Type="application/json", // THIS DOES NOT WORK? 
            }
        })

    `); err != nil {
        panic(err)
    }
}

The headers that are passed in are of type table. But for a tables field name I can't use the - character. For http header like Content-Type this character is necessary. Is there a way to work around this.

wipsel commented 11 months ago

Apparantly you can write lua fields for tables as ["Content-Type"] = "application/json"