我现在使用的是 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 会解析不了?有没有哪里是我要注意的
1
luguhu 2023-08-06 18:21:34 +08:00
是不是解析的 struct 有问题?
能有字段应该能表示是收到数据了, fiber 也能成功解析 |
2
0o0O0o0O0o 2023-08-06 18:24:48 +08:00 via iPhone
打日志,对比返回的数据,而不是对比解析后的
|
3
joesonw 2023-08-06 18:25:08 +08:00 via iPhone
struct 定义呢? tag 对不上吧?
|
4
Trim21 2023-08-06 18:31:01 +08:00
最重要的 common.AlipayNotifyResponse 呢?
|
5
elone OP 忘放了
``` 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 |
6
joesonw 2023-08-06 18:44:48 +08:00 via iPhone
struct 里的 tag 用 form
|