簡體   English   中英

GET請求的URL參數

[英]URL parameters for GET request

我必須用get參數“ scope”“ scope:priavte scope”來做

使用python的requets模塊

注意scope參數的值必須以空格分隔。

url_1 = "https://www.testapp.com/oauth/authorize/"
payload = {"scope": "scope:private scope:public", "client_id"=xxx}

>> req = requests.get(url_1, params=payload)
>> url
>>https://www.testapp.com/oauth/authorize/?scope=scope%3Aprivate+scope%3Apublic&client_id=XXX
>> req.status_code
400

而此網址有效

 url_2 = "https://www.testapp.com/oauth/authorize/?client_id=xxx&scope=scope:public%20scope:private"
>> req = requests.get(url_2)
>> req.status_code
200

這兩個網址之間的區別在於范圍如何對參數值"scope:private scope:public"進行編碼。

url_1使用%3A for : and + for space

而url_2使用: is not encoded and %20 for space

如何使用url_1發出請求?

我不知道是否有一種聰明的方法來做...我想也許有些帶有請求掛鈎的東西,但似乎無法將其插入到請求中。

你可以,但是,做手工用urllib.quote()規定:作為一個安全的字符,然后把它傳遞給requests.get()作為一個字符串:

import urllib
import requests

payload = {"scope": "scope:private scope:public", "client_id": "xxx"}
query_string = '&'.join(['='.join((k, urllib.quote(v, ':'))) for k,v in payload.items()])
req = requests.get(url_1, params=query_string)

結果查詢字符串如下所示:

>>> print query_string
scope=scope:private%20scope:public&client_id=xxx

暫無
暫無

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

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