2012-09-20 115 views
0

最低例如:標題不正確設置

# -*- coding: utf-8 -*- 
import requests 

xml = """<?xml version='1.0' encoding='utf-8'?> 
<a>б</a>""" 

print requests.post('http://httpbin.org/post', data=xml, headers={'Authorization': 'a', 'developerToken': 'b', 'clientCostumerID': 'c'}).headers 

標題是未設置。

+1

'.headers'是* response *的值,而不是傳出的請求。 –

+0

我如何檢查它們是否設置正確? –

+1

只需打印出整個響應,httpbin.org會告訴你它收到了什麼標題。 –

回答

1

你已經在使用httpbin.org服務,它返回你一個JSON結構,包括它收到的所有標題:

import requests 

xml = """<?xml version='1.0' encoding='utf-8'?> 
<a>б</a>""" 

data = requests.post('http://httpbin.org/post', data=xml, headers={'Authorization': 'a', 'developerToken': 'b', 'clientCostumerID': 'c'}).json 

for headername, headervalue in data['headers'].iteritems(): 
    print '%s: %s' % (headername, headervalue) 

當我運行上面的代碼,我得到:

Content-Length: 48 
Developertoken: b 
Accept-Encoding: identity, deflate, compress, gzip 
Connection: keep-alive 
Clientcostumerid: c 
Accept: */* 
User-Agent: python-requests/0.14.0 CPython/2.7.3 Darwin/11.4.0 
Host: httpbin.org 
Content-Type: 
Authorization: a 
+0

爲什麼標題改變了?我的意思是,爲什麼它是Clientcostumerid而不是clientCostumerID'? –

+1

HTTP標頭不區分大小寫。 –