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

背景

使用WxPusher微信推送服務 ,可以及時的將服務的一些運行異常信息,發送到自己的微信上,方便了解服務的運行狀態(PS:這個服務是免費的)。

你可以在這裡看到WxPusher微信推送服務的接入說明文檔://wxpusher.zjiecode.com/docs/

你可以在這裡體驗他的功能://wxpusher.zjiecode.com/demo

真的非常好用,強烈推送用來發送提示消息。

問題

看到官方的接入文檔,接入的時候,出現了錯誤:

{
    "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微信推送服務,真心的好用。

轉載請註明出處,謝謝。