簡體   English   中英

無法通過傳遞REST API的參數來形成URL

[英]not able to form URL by passing parameter for REST API

我正在嘗試調用REST API,並將所需的值作為參數傳遞。

首先,我嘗試直接傳遞值並按預期​​獲得URL值。 我模擬了所有值以隱藏原始憑據,但原始請求的格式相同。

代碼:

GATE_API ='https://gateway-stage-core.milton.com/auth/oauth2/token'

payload = {'id': '8888yxy','secret':'vUz65MZ','type' : 'client','consumer_id' : '673d5881'}
print payload

r = requests.post(GATE_API, params = payload, verify=False)
print r.url

輸出:

{'secret': 'vUz65MZ', 'type': 'client', 'id': '8888yxy', 'consumer_id': '673d5881'}
https://gateway-stage-core.milton.com/auth/oauth2/token?client_secret=vUz65MZ&type=client&id=8888yxy&consumer_id=673d5881

但是在原始代碼中,我必須將這些憑據作為參數傳遞。 輸出URL帶有很多垃圾值,無法與上面的值匹配。

碼:

GATE_API ='https://gateway-stage-core.milton.com/auth/oauth2/token'
ID = '8888yxy'
SECRET = 'vUz65MZ'
TYPE = 'client'
CONSUMER_ID= '673d5881'

payload = '{' + '"' + 'id' + '"' + ':' + '"' + ID + '"' + "," + '"' + 'secret' + '"' + ':' + '"' + SECRET + '"' + "," +'"' + 'type' + '"' + ':' + '"' + TYPE + '"' + "," + '"' + 'consumer_id' + '"' + ':' + '"' + CONSUMER_ID + '"' + '}`' 
print payload

r = requests.post(GATE_API, params = payload, verify=False)
print r.url

輸出:

{"id":"8888yxy","secret":"vUz65MZ","type":"client","consumer_id":"673d5881"}
https://gateway-stage-core.milton.com/auth/oauth2/token?%7B%22id%22:%228888yxy%22,%22secret%22:%22vUz65MZ%22,%22type%22:%22client%22,%22consumer_id%22:%22673d5881%22%7D

在第二個示例中,您將有效載荷作為字符串而不是字典傳遞。 “垃圾值”是有效負載字符串中非字母數字字符的轉義字符串。

暫無
暫無

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

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