簡體   English   中英

Google App Engine:HTTP錯誤400:錯誤請求

[英]Google App Engine: HTTP Error 400: Bad Request

我正在開發一個應用程序,它需要計算用戶輸入的兩個位置之間的距離。 我正在使用Google Map的距離矩陣API來實現此目的。 這是代碼:

class MainPage(Handler):
    def get(self):
        self.render('map.html')
    def post(self):
        addr1 = self.request.get("addr1")
        addr2 = self.request.get("addr2")
        url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + addr1 + '&destinations=' + addr2 + '&mode=driving&sensor=false'
        link = urllib2.urlopen(url).read()
        self.response.write(link)

map.html

<html>
<head>
<title>Fare Calculator</title>
</head>
<body>
    <form method = "post">
        Source<input type = 'text' name = "addr1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        Destination<input type = 'text' name = "addr2">
        <br><br>
        <input type = "submit" value = "Calculate Fare">
    </form>
</body>
</html>

map.html包含一個基本的HTML表單,其中包含源和目標地址的輸入。 但是,當我運行此應用程序時,我收到HTTP錯誤400:錯誤請求。 發生了什么?

您的變量需要針對API請求進行urlencoded。

...    
url = 'http://maps.googleapis.com/maps/api/distancematrix/json?origins=' + urllib.quote_plus(addr1) + '&destinations=' + urllib.quote_plus(addr2) + '&mode=driving&sensor=false'
...

你可以在這里閱讀更多關於.quote_plus 信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM