V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
elone
V2EX  ›  Go 编程语言

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

  •  
  •   elone · 265 天前 · 997 次点击
    这是一个创建于 265 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我现在使用的是 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 会解析不了?有没有哪里是我要注意的

    luguhu
        1
    luguhu  
       265 天前
    是不是解析的 struct 有问题?
    能有字段应该能表示是收到数据了, fiber 也能成功解析
    0o0O0o0O0o
        2
    0o0O0o0O0o  
       265 天前 via iPhone
    打日志,对比返回的数据,而不是对比解析后的
    joesonw
        3
    joesonw  
       265 天前 via iPhone
    struct 定义呢? tag 对不上吧?
    Trim21
        4
    Trim21  
       265 天前
    最重要的 common.AlipayNotifyResponse 呢?
    elone
        5
    elone  
    OP
       265 天前
    忘放了

    ```
    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
        6
    joesonw  
       265 天前 via iPhone
    struct 里的 tag 用 form
    elone
        7
    elone  
    OP
       265 天前
    @joesonw 非常感谢。用 form 就可以了。我刚才打印了一下,才发现是用的 application/x-www-form-urlencoded; charset=utf-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2675 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 11:15 · PVG 19:15 · LAX 04:15 · JFK 07:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.