[英]API Query Doesn't return results when "params" is encoded into a get request but API call works when URL isn't encoded
[英]Force encoded period in API request
我正在嘗試使用無法正確處理期間的REST API。
它將在http://api.com/endpoint?myparameter=includes%20a上失敗。
但是,當將句點編碼為%2E時,它可以正常工作:
http://api.com/endpoint?myparameter=includes%20a%2E
工作正常。
我可能會嘗試,在使用請求時,%2E似乎總是被轉換回“。”。
有什么辦法可以避免這種行為?
您可以使用准備請求得到這個工作,因為默認情況下,請求將使用requote_uri(uri)
功能requests.util
來幫忙,采取unquoting毫無保留的字符為你的照顧。 如果您已經自己解析並准備了URL,則可以執行以下操作並覆蓋url
字段:
from requests import Session, Request
s = Session()
req = Request('GET', 'http://localhost:8008?name=kevin%2Eemckinsey')
# This will use `requote_uri` to unquote unreserved characters so %2E becomes a `.`
prepped = req.prepare()
# Forcing the `url` field to be a URL we specified.
prepped.url = 'http://localhost:8008?name=kevin%2Emckinsey'
resp = s.send(prepped)
print(resp.url)
print(resp.json())
# http://localhost:8008?name=kevin%2Emckinsey
# PHP's $_SERVER['REQUEST_URI'] returns:
# {'name': '/?name=kevin%2Emckinsey'}
對我來說,這似乎是個骯臟的把戲,但AFAIK不能告訴請求不要取消引用特定字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.