golang在http消息互動時的 “[object undefined]”
在使用
golang
作為微信開發的服務端時,遇到解析消息時字段為空的情況.這種情況下使用
encoding/json
解析會出現
[object Undefined]
字元串,而不是期望的空字元串.
自己寫了一個工具函數解析此類情況:
func GetHeadString(r *http.Request, key string) (string, error) {
if len(key) == {
return "", fmt.Errorf("empty key")
}
x := r.Header.Get(key)
if len(x) == || x == "[object Undefined]" {
return "", fmt.Errorf("empty value for key %s", key)
}
return x, nil
}