2012-11-09 68 views
0
$ curl -F [email protected] -F 'data={"title":"some title","otherinfo" : "aabcdef"}' https://someurl 

以上是從終端進行工作和正確的方法。Python請求 - 根據給定的捲曲請求格式數據

我試着使用要求這樣蟒蛇來實現這一點:

files = {'myfile': open('myfilename', 'rb')} 
data = {} 

data['data'] = { 
    'title' : 'some title', 
    'otherinfo' : 'other info' 
} 

r = requests.post(url, files=files, data=data, auth=auth) 

這裏,數據不能正常到達目的地,在那裏我錯了?

+0

這是什麼意思*沒有正確到達目的地*?服務器在使用curl時會收到什麼,以及在使用請求時會收到什麼? –

+0

使用curl時,正確到達,但使用請求時,服務器不識別數據bcoz它希望數據處於正確的形式。如果你從捲曲中看到數據就像-F'data = ...'。我想要它的等價使用請求。我試着發送確切的字符串'data = ..',就像data =「與curl strng相同」,但是這個請求不允許這樣,我試過json.dumps(data),但是沒有用 – user1767962

回答

0

當您使用-F說法捲曲發送數據與Content-Type設置爲multipart/form-data而當你使用data參數要求使用Content-Type設置爲application/x-www-form-urlencoded。您應該指示請求使用正確的Content-Type發送數據。請參閱How to send a "multipart/form-data" with requests in python?如何執行此操作。