2016-11-01 175 views
0

我正在發送HTTP POST請求。HTTP發佈請求錯誤HTTPError:錯誤的請求

我的數據由一個字典的兩個條目:在詞典

  • 一個值是一個字符串
  • 另一個值是一個字符串數組。

我知道URL是有效的,但我不斷收到:

HTTPError: Bad Request 

任何人都可以找出我要去的地方錯了嗎?

from urllib.parse import urlencode 
from urllib.request import Request, urlopen 

array_of_strings = ["yrrt", "rtgh", "erte", "eregee"]   
url_4 = 'http:// someurl' 
data_4 ={"key": "value", "array": array_of_strings} 

request_4 = Request(url_4, urlencode(data_4).encode()) 
response = urlopen(request_4).read().decode() 

print(response) 

回答

0

錯誤的請求可能是由無效的參數或不正確的Content-Type造成的。確保所有的請求參數都是正確的,你的代碼看起來不正確。

順便說一句,我建議你改用的urllib的請求:

這是使用請求代碼:

import requests 

array_of_strings=["yrrt", "rtgh", "erte", "eregee"]........ 

url_4= 'https://login.locaweb.com.br/v1/tickets' 
data_4={"key": "value", "array": array_of_strings} 

response = requests.post(url_4, data=data_4) 
print(response.text) 

它有一個簡單的語法,更容易調試。