【问题标题】:convert curl notification command to urllib2 -kaaproject将 curl 通知命令转换为 urllib2 -kaaproject
【发布时间】:2016-09-04 11:33:10
【问题描述】:

我正在尝试将以下 curl 命令转换为通过 urllib2 发送通知

curl -v -S -u devuser:devuser123 -F'notification={"applicationId":"4","schemaId":"14","topicId":"3","type":"USER"};type=application/json' -F'endpointKeyHash=lsXnYUbE31aCgN5NSsbcRMAZTgM=;type=text/plain' -F file={"message" : "Hello world!"} "http://localhost:8080/kaaAdmin/rest/api/sendUnicastNotification" | python -mjson.tool

我为发送通知编写此代码

import urllib2, base64
data = 'notification={"applicationId":"4","schemaId":"14","topicId":"3","type":"USER"};type=application/json&endpointKeyHash=xQXJ7OzFX4M6W5tl4sVTuWLTuzc=;type=text/plain&file={"message":"Hello world!"}'

url="http://localhost:8080/kaaAdmin/rest/api/sendUnicastNotification"

request = urllib2.Request(url, data)
#authentication part
base64string = base64.encodestring('%s:%s' % ('devuser', 'devuser123')).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)   
result = urllib2.urlopen(request)
the_page = result.read()

但是当我运行这段代码时,我鼓励出现以下错误

line 527, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 415: Unsupported Media Type

问题可能是关于通过 urllib2 发送多个数据,但我如何通过 urllib2 将这些参数发送到 kaa 服务器? 谢谢!

【问题讨论】:

    标签: curl urllib2 iot kaa


    【解决方案1】:

    您需要为您的请求将内容类型标头设置为 multipart/form-data。这就是您收到“不支持的媒体类型”错误的原因。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 1970-01-01
      • 2011-03-31
      • 2019-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多