2015-02-06 120 views
0

我需要對服務器進行一些測試,以便我想對內容長度進行硬編碼,而不希望請求庫計算內容長度。設置請求庫內容長度

我寫至今的代碼看起來是這樣的:

date = time.strftime("%a, %d %b %Y %H:%M:%S GMT",time.gmtime(time.time())) 
sample_header = { 
    "Host": "xyz.xyz.zyz.com", 
    "Content-Type": "application/json; charset=utf-8", 
    "Date": date   
    # "Authorization": "not_specified" 
    } 

sample_payload = { 
    "ci":{ 
     "pev":1, 
     "affid":"243324", 
     "sdkv":"My SDK Version", 
     "prn":"My Security Product", 
     "pv":"1.0.1", 
     "cliid":"4fe836859578e81ae5b0a061d6949634", 
     "rid":234343 
     }, 
    "q":[{"op":"url","url":"http://www.yahoo.com"}] 
} 

    def test_1677(self): 
     self.tc_id="gtirest-1677" 
     s = requests.Session() 
     req = requests.Request('POST', url, headers=sample_header, data=json.dumps(sample_payload)) 
     prepared = req.prepare() 
     # del prepared.headers['Content-Lenght'] 

     del prepared.headers['Content-Length'] 

     prepared.headers['Content-Lenght'] = 99999 
     response = s.send(prepared) 
     print "Response code" 
     print response.status_code 
     print response.text 
     print response.request.headers 

它給400錯誤的請求,但是當我發送類似內容長度它給413實體過大,這是對的,我怎麼能acheive的請求庫中也一樣。

我做Wireshark的捕獲,它顯示了以下內容:

隨着請求庫: Wireshark Capture request

,捲曲:

Wireshark Capture curl

爲什麼在請求庫它給400錯誤的請求。我可以看到有效載荷現在正在提出正確的請求。

+0

爲什麼在請求庫中它提供了400個錯誤的請求。我可以看到有效載荷現在正在提出正確的請求。 – 2015-02-06 14:33:22

回答

1

拼錯頭:

prepared.headers['Content-Lenght'] = 99999 
#        ^^ 

它的拼寫Lengtht然後h

如果您正在設置標頭值,則不需要先刪除標頭;如果你沒有先刪除Content-Length標題,你會發現有兩個標題,並且錯字會立即跳出。

+0

是的,這個問題出現在我的腦海裏,但我想請求可能會做一些事情。謝謝彼得。我剛剛做了一個錯字,從最近的120分鐘裏打破了我的頭。 – 2015-02-06 14:36:19

相關問題