接入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微信推送服务,真心的好用。

转载请注明出处,谢谢。