1. 程式人生 > >接入WxPusher微信推送服務出現錯誤:Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

接入WxPusher微信推送服務出現錯誤:Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

# 背景 使用WxPusher微信推送服務 ,可以及時的將服務的一些執行異常資訊,傳送到自己的微信上,方便了解服務的執行狀態(PS:這個服務是免費的)。 你可以在這裡看到WxPusher微信推送服務的接入說明文件:[https://wxpusher.zjiecode.com/docs/](https://wxpusher.zjiecode.com/docs/) 你可以在這裡體驗他的功能:[https://wxpusher.zjiecode.com/demo](https://wxpusher.zjiecode.com/demo) 真的非常好用,強烈推送用來發送提示訊息。 # 問題 看到官方的接入文件,接入的時候,出現了錯誤: ```json { "code": 1005, "msg": "伺服器錯誤:Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported", "data": null, "success": false } ``` # 解決方案 為了分析這個錯誤,我們先來看一下完整的http請求的request: header: ``` POST /api/send/message HTTP/1.1 Content-Type: application/x-www-form-urlencoded User-Agent: PostmanRuntime/7.26.5 Accept: */* Postman-Token: 454412c5-4bb9-46ec-a8f9-ea74c6b9e02a Host: wxpusher.zjiecode.com Accept-Encoding: gzip, deflate, br Content-Length: 221 Connection: keep-alive { "appToken":"AT_xxx", "content":"測試內容", "summary":"訊息摘要", "contentType":1, "topicIds":[], "uids":[ "UID_xxxx" ] } ``` 這樣咱們一眼就看出來問題了,傳送資料的Body是json格式的,這個沒毛病,但是注意觀察header裡面: **Content-Type: application/x-www-form-urlencoded** 申明的型別居然是application/x-www-form-urlencoded,明顯不對,咱們把Content-Type修改成:**application/json**。 注意是Content-Type,不是ContentType、contentType、contentType等,要寫對。 修改以後,變成了這樣,一下就對了: ``` POST /api/send/message HTTP/1.1 Content-Type: application/json User-Agent: PostmanRuntime/7.26.5 Accept: */* Postman-Token: 8b253f0e-292c-4b0d-8b37-86cc344bb199 Host: wxpusher.zjiecode.com Accept-Encoding: gzip, deflate, br Content-Length: 221 Connection: keep-alive { "appToken":"AT_xxxx", "content":"Wxpusher祝你中秋節快樂!", "summary":"訊息摘要", "contentType":1, "topicIds":[], "uids":[ "UID_xxx" ] } ``` # 總結 訊息一點,仔細分析,仔細看文件,很容易排查出來錯誤; 認真閱讀官方的說明文件,弄清楚要怎嚒傳遞資料,按照規範傳遞資料。 強烈推薦使用WxPusher微信推送服務,真心的好用。 > 轉載請註明出處,