2016-08-31 45 views
0

我一直在運行腳本時在我的終端中收到<Response [400]>如何在Python中進行POST時修復<Response [400]>?

我已經試過

import requests 
import json 

url = 'http://172.19.242.32:1234/vse/account' 

data = '{ 
    "account_id": 1008, 
    "email_address": "[email protected]", 
    "password": "qqq", 
    "account_type": "customer", 
    "name_prefix": "", 
    "first_name": "Beverly", 
    "middle_names": "", 
    "last_name": "Hills", 
    "name_suffix": "", 
    "non_person_name": false, 
    "DBA": "", 
    "display_name": "BeverlyHills", 
    "address1": "4984 Beverly Dr", 
    "address2": "4984 Beverly Dr", 
    "address3": "", 
    "city": "Beverly Hills", 
    "state": "CA", 
    "postal_code": "90210", 
    "nation_code": "90210", 
    "phone1": "3105554984", 
    "phone2": "", 
    "phone3": "", 
    "time_zone_offset_from_utc": -5, 
    "customer_type": "2", 
    "longitude": -118.4104684, 
    "latitude": 34.1030032, 
    "altitude": 0 
}' 

headers = {'content-type': 'application/json'} 

r = requests.post(url, data=json.dumps(data), headers=headers) 

print r 

我做了什麼錯?

+0

什麼是響應正文? – Kos

+0

我怎麼看? – ihue

+0

[有一些屬性和方法,如'.text','.content','.json()'](http://docs.python-requests.org/en/master/user/quickstart/#response-內容) – Kos

回答

4

變化

r = requests.post(url, data=json.dumps(data), headers=headers) 

r = requests.post(url, data=data, headers=headers) 

因爲數據不是必須轉換成JSON,但已經是JSON的字典。

+0

是不是'json = data'?如果你傳遞'data = data',那麼它將被編碼爲'x-www-form-urlencoded'而不是'application/json'。 – Kos

+0

改變它,似乎爲我工作得很好。 – ihue

+0

@Kos理想情況下,數據將是一個字典,他會使用json參數而不是數據來傳遞它。 'requests.post(url,json = data)' –

相關問題