我有很多函數成功地使用python發佈一個urlencoded body。但是,我有一個多維字典的主體。使用這本字典,我只得到一個400(壞請求)。 bodytmp(下面)是使用Fiddler工作的主體的一個例子。 (實際的url和body不能提供。)我還包括了一個遞歸的urlencode函數,我在這裏找到並正在使用,但沒有成功。使用urlencoded多維字典的Python POST
有沒有人有這種類型的POST請求經驗與urlencoded身體的多維字典?
謝謝。 (我已經縮寫代碼以使其更具可讀性,但是這是要點。)
從httplib2的進口的Http 進口httplib的 進口的urllib
高清postAgentRegister():
h = Http(disable_ssl_certificate_validation=True)
headers={'Content-Type':'application/x-www-form-urlencoded'}
bodytmp={"Username": "username",
"Password": "password",
"AccountId": "accountid",
"PublicKey": {
"Value1a": "32ab45cd",
"value1b": "10001"
},
"snId": "SN1",
"IpAddresses": {
"Value2a": ["50.156.54.45"],
"Value2b": "null"
}
}
body=recursive_urlencode(bodytmp)
try:
response,content=h.request('https://server/endpoint','POST',headers=headers,body=body)
conn = httplib.HTTPConnection(testserver)
conn.request('POST', endpoint, body, headers)
r1 = conn.getresponse()
status = r1.status
reason = r1.reason
except httplib.HTTPException, e:
status= e.code
print 'status is: ', status
高清recursive_urlencode(d): DEF遞歸(d,鹼=無): 雙= []
for key, value in d.items():
if hasattr(value, 'values'):
pairs += recursion(value, key)
else:
new_pair = None
if base:
new_pair = "%s[%s]=%s" % (base, urllib.quote(unicode(key)), urllib.quote(unicode(value)))
else:
new_pair = "%s=%s" % (urllib.quote(unicode(key)), urllib.quote(unicode(value)))
pairs.append(new_pair)
return pairs
return '&'.join(recursion(d))
您是否使用具有預期結構但內容最少的字典對此進行了測試,以確保問題不針對測試數據的長度/內容? – octern
我正在使用此代碼來測試現有的API端點。 IOW,沒有另一個端點具有類似的結構,但是您建議的內容最少。然而,所有其他端點(在json中沒有嵌套級別)按預期工作。當通過Fiddler提交時,上面的正文會起作用。 – ftg