如何调试smartystreets api调用?(How to debug smartystreets api call?)

编程入门 行业动态 更新时间:2024-10-15 18:23:21
如何调试smartystreets api调用?(How to debug smartystreets api call?)

我想通过Python使用SmartyStreets API。

这是我正在使用的脚本:

# ss.py import requests import pdb authid = 'jaf110af-b6ae-3ea2-829a-035dcb246392' token = 'kAyGAA0rD0lNykcktJpg' payload2 = {'auth-id':authid, 'auth-token':token} add_s = '1600+Amphitheatre+Parkway,+Mountain+View,+CA' site2 = 'https://api.smartystreets.com/'+add_s req2 = requests.get(site2,params=payload2) pdb.set_trace() req2.status_code #req2.json() req2.text 'bye'

API服务器正在发出404,所以也许我有语法错误。

请帮助我了解如何调试我对API服务器的API调用。

以下是有关我的设置的一些信息:

dan@h81:~/ks/c/lodgiq $ python Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Dec 7 2015, 11:16:01) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> dan@h81:~/ks/c/lodgiq $ dan@h81:~/ks/c/lodgiq $ dan@h81:~/ks/c/lodgiq $ python ss.py > /home/dan/ks/c/lodgiq/ss.py(16)<module>() -> req2.status_code (Pdb) req2.status_code 404 (Pdb)

I want to use SmartyStreets API via Python.

Here is the script I am using:

# ss.py import requests import pdb authid = 'jaf110af-b6ae-3ea2-829a-035dcb246392' token = 'kAyGAA0rD0lNykcktJpg' payload2 = {'auth-id':authid, 'auth-token':token} add_s = '1600+Amphitheatre+Parkway,+Mountain+View,+CA' site2 = 'https://api.smartystreets.com/'+add_s req2 = requests.get(site2,params=payload2) pdb.set_trace() req2.status_code #req2.json() req2.text 'bye'

The API server is issuing a 404, so perhaps I have a syntax error.

Please help me understand how to debug my API calls to the API server.

Here is some info about my setup:

dan@h81:~/ks/c/lodgiq $ python Python 3.5.1 |Anaconda 4.0.0 (64-bit)| (default, Dec 7 2015, 11:16:01) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> dan@h81:~/ks/c/lodgiq $ dan@h81:~/ks/c/lodgiq $ dan@h81:~/ks/c/lodgiq $ python ss.py > /home/dan/ks/c/lodgiq/ss.py(16)<module>() -> req2.status_code (Pdb) req2.status_code 404 (Pdb)

最满意答案

这里发生了两件事:

您的网址不正确。 您的代码显示您正在请求URL https://api.smartystreets.com/1600+Amphitheatre+Parkway,+Mountain+View,+CA 。 您需要请求URL https://api.smartystreets.com/?street=1600%20Amphitheatre%20Pkwy&city=Mountain%20View&state=CA&zipcode= ,它为我返回了200 OK响应。 请注意,地址是作为查询参数发送的,而不是URL路径本身。 另请注意,地址中的特殊字符(即+和)是URL编码的。 您传入的params (即payload2的值)意味着是查询参数,如上面的city , state和zipcode ,但您发送的是HTTP标头的值,而不是查询参数。 根据请求文档将payload2 headers作为标头发送。

希望有所帮助。

There are two things happening here:

Your URL is incorrect. Your code shows that you are requesting the URL https://api.smartystreets.com/1600+Amphitheatre+Parkway,+Mountain+View,+CA. You need to request the URL https://api.smartystreets.com/?street=1600%20Amphitheatre%20Pkwy&city=Mountain%20View&state=CA&zipcode=, which returned a 200 OK response for me. Notice that the address is sent as query parameters, not in the URL path, itself. Also notice that the special characters in your address (i.e. + and ,) are URL encoded. The params you are passing in (i.e. the value of payload2) are meant to be query parameters, like city, state, and zipcode above but you are sending in the value of HTTP headers, not query parameters. Send in the headers of payload2 as headers as per the Requests documentation.

Hope that helps.

更多推荐

本文发布于:2023-08-06 05:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:api   smartystreets   call   debug

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!