为什么 go fiber 无法解析 alipay 的 notify 回调的参数?

277 天前
 elone

我现在使用的是 go fiber ,我知道的是这家伙用的是 fasthttp 。

是这样的,我现在要解析支付宝返回来的回调。我发现 fiber 解析出来都是空的,但其它有两三个字段是有的:


{
        "notify_time": null,
        "notify_type": null,
        "notify_id": null,
        "app_id": null,
        "charset": [
                "utf-8"
        ],
        "version": [
                "1.0"
        ],
        "sign_type": null,
        "sign": [
                "CPwn0wkSwa7qjV+k8/RHywHyPpL7kdmadp6QSZuw+NJe5vQ8vgKJHtLKMOA8RxPTY+BWhwEqN86F3RrDU0x6gj2UttN6qOauL4ZOer7KanWmmDEjnIR5YLqPibAbo8FyuCwtNQsUXsC8A8DxjfmJg5bTyEXxcF4UMFCvDfcC55eY+UVkcu0dt6KjLag+mUOMj/+PA5KBARkegfDi0yW4J78pv5Fgb9lD1u5bM9xEHDxOt1Xo6Jf63cnsWm5ccZ+cBQFQFKr4mSUAp0gIP1o1kzvUVJqr9ISK2HFGK8ED26OjuH8bWrtcepd08I8MxvZ170oRP1in9Hpk8wEwDZjyqw=="
        ],
        "trade_no": null,
        "out_trade_no": null,
        "out_biz_no": null,
        "buyer_id": null,
        "buyer_logon_id": null,
        "trade_status": null
}

fiber 的代码如下:

var input common.AlipayNotifyResponse

	if err := c.BodyParser(&input); err != nil {
		fmt.Println("failed to parse JSON input")
		return errors.New("failed to parse JSON input")
	}

然后我就怀疑是 fiber 的问题,试了一下用原生的 net/http 写:

func handlePost(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
	w.Header().Set("Access-Control-Allow-Headers", "*")
	r.ParseForm()

	fmt.Println(utils.PrettyJson(r.Form))
}

发现是可以解析的:

{
        "app_id": [
                "902100sdff"
        ],
        "auth_app_id": [
                "902100sdff"
        ],
        "buyer_id": [
                "902100sdff"
        ],
        "buyer_pay_amount": [
                "8.00"
        ],
        "charset": [
                "utf-8"
        ],
        "fund_bill_list": [
                "[{\"amount\":\"8.00\",\"fundChannel\":\"ALIPAYACCOUNT\"}]"
        ],
        "gmt_create": [
                "2023-08-06 17:48:28"
        ],
        "gmt_payment": [
                "2023-08-06 17:48:33"
        ],
        "invoice_amount": [
                "8.00"
        ],
        "notify_id": [
                "202308060122217483409dd7380500601571"
        ],
        "notify_time": [
                "2023-08-06 17:48:34"
        ],
       }

现在已经有好几个接口用 fiber 写好了,不太想大改。就想问问为什么 fiber 会解析不了?有没有哪里是我要注意的

1029 次点击
所在节点    Go 编程语言
7 条回复
luguhu
277 天前
是不是解析的 struct 有问题?
能有字段应该能表示是收到数据了, fiber 也能成功解析
0o0O0o0O0o
277 天前
打日志,对比返回的数据,而不是对比解析后的
joesonw
277 天前
struct 定义呢? tag 对不上吧?
Trim21
277 天前
最重要的 common.AlipayNotifyResponse 呢?
elone
277 天前
忘放了

```
type AlipayNotifyResponse struct {
NotifyTime string `json:"notify_time"`
NotifyType string `json:"notify_type"`
NotifyId string `json:"notify_id"`
AppId string `json:"app_id"`
Charset string `json:"charset"`
Version string `json:"version"`
SignType string `json:"sign_type"`
Sign string `json:"sign"`
TradeNo string `json:"trade_no"`
OutTradeNo string `json:"out_trade_no"`
OutBizNo string `json:"out_biz_no"`
BuyerId string `json:"buyer_id"`
BuyerLogonId string `json:"buyer_logon_id"`
TradeStatus string `json:"trade_status"`
}

字段类型看的支付宝的文档:
https://opendocs.alipay.com/open/203/105286/


```
@joesonw
@Trim21
joesonw
277 天前
struct 里的 tag 用 form
elone
277 天前
@joesonw 非常感谢。用 form 就可以了。我刚才打印了一下,才发现是用的 application/x-www-form-urlencoded; charset=utf-8

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/962831

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX