我在 Go 中创建了一个 Azure 函数。该功能在具有 GET 和 POST 请求的本地机器上正常工作。发布后,通过 POST 发送的请求有效负载在请求对象中不可用。这是我的代码:
# Main function
mux := http.NewServeMux()
mux.HandleFunc("/hello", helloWorld)
logrus.Fatal(http.ListenAndServe(":"+httpInvokerPort, mux))
# helloWorld function
func helloWorld(w http.ResponseWriter, r *http.Request) {
logrus.Info("hello: inside helloWorld")
bodyBuffer, err := ioutil.ReadAll(r.Body)
if err != nil {
logrus.Info("error while reading:" + err.Error())
w.Write([]byte("GO: error in reading request body"))
} else {
logrus.Info("body : " + string(bodyBuffer))
w.Write([]byte("GO: return hello"))
}
}
部署后,我使用以下 JSON 正文调用 POST api 请求:
{
"a": "b"
}
在日志中,我看到:
time="2020-07-31T01:14:11Z" level=info msg="body :"
函数.json:
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": ["get", "post"]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
我的代码托管在这里:https ://github.com/mpurusottamc/azurefunc-go
关于如何调试这个的任何建议?
凤凰求蛊
相关分类