向 UrbanAirship 发送 POST 请求时的 HTTP 400 响应

发布于 2024-12-08 19:12:52 字数 1320 浏览 1 评论 0原文

我正在使用 Web2Py 创建一个简单的应用程序,通过 UrbanAirship 发送推送通知。由于某种原因,当我尝试通过我的代码发送它时收到 400 响应。它的 UA API 使用 REST 客户端可以正常工作。这是我的代码:

url = 'https://go.urbanairship.com/api/push/'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager

passman.add_password(None, url, username, password)
# because we have put None at the start it will always
# use this username/password combination for  urls
# for which `theurl` is a super-url

authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.

values = {"device_tokens": ["<DEVICE TOKEN>"], "aps": {"alert": "Hello!"}}

data = urllib.urlencode(values)
headers = {'Content-Type': 'application/json'}

req = urllib2.Request(url, data, headers)

try:
    response = urllib2.urlopen(req)
    return response
except IOError, e:
    if e.code == 200:
        return "Push sent!"
    else:
        return 'The server couldn\'t fulfill the request. Error: %d' % e.code

据我所知,问题出在发送数据的格式上。我哪里错了?

I am using Web2Py to create a simple app which sends Push notifications through UrbanAirship. For some reason, I am getting a 400 response when I try to send it through my code. It UA API works fine using REST client. This is my code:

url = 'https://go.urbanairship.com/api/push/'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
# this creates a password manager

passman.add_password(None, url, username, password)
# because we have put None at the start it will always
# use this username/password combination for  urls
# for which `theurl` is a super-url

authhandler = urllib2.HTTPBasicAuthHandler(passman)
# create the AuthHandler

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
# All calls to urllib2.urlopen will now use our handler
# Make sure not to include the protocol in with the URL, or
# HTTPPasswordMgrWithDefaultRealm will be very confused.
# You must (of course) use it when fetching the page though.

values = {"device_tokens": ["<DEVICE TOKEN>"], "aps": {"alert": "Hello!"}}

data = urllib.urlencode(values)
headers = {'Content-Type': 'application/json'}

req = urllib2.Request(url, data, headers)

try:
    response = urllib2.urlopen(req)
    return response
except IOError, e:
    if e.code == 200:
        return "Push sent!"
    else:
        return 'The server couldn\'t fulfill the request. Error: %d' % e.code

As far as I can understand, the problem is in the format of data being sent. Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2024-12-15 19:12:52

urllib.urlencode 函数用于制作 URL 编码的参数主体 (Content-Type: application/x-www-form-urlencoded)。对于 JSON,这显然是您想要的,请使用 json.dumps代替。

The urllib.urlencode function is for making a URL-encoded parameter body (Content-Type: application/x-www-form-urlencoded). For JSON, which is apparently what you want, use json.dumps instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文