我尝试使用urllib通过API订购磁盘,但我收到Bad请求作为回复。我无法找出可能出错的地方。有什么想法吗?
url = "https://username:apikey@api.softlayer.com/rest/v3/SoftLayer_Product_Order/placeOrder"
data = urllib.urlencode({
"parameters": [{
"virtualGuests": [{"id": idofvirtualguest}],
"prices": [{
"id": 113031,
"categories": [{
"categoryCode": "guest_disk1",
"complexType": "SoftLayer_Product_Item_Category"
}],
"complexType": "SoftLayer_Product_Item_Price"
},
{
"id": 112707,
"categories": [{
"categoryCode": "guest_disk2",
"complexType": "SoftLayer_Product_Item_Category"
}],
"complexType": "SoftLayer_Product_Item_Price"
}
],
"properties": [
{"name": "NOTE_GENERAL", "value": "adding disks"},
{"name": "MAINTENANCE_WINDOW", "value": "now"}
],
"complexType": "SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade"
}]
})
response = urllib.urlopen(url, data)
答案 0 :(得分:0)
尝试使用:
json.dumps 而不是 urllib.urlencode
注意:您需要导入json,所以它应该如下所示:
import urllib
import json
url = "https://username:apiKey@api.softlayer.com/rest/v3/SoftLayer_Product_Order/placeOrder"
idofvirtualguest = 111122233
data = json.dumps({
"parameters": [{
"virtualGuests": [{"id": idofvirtualguest}],
"prices": [{
"id": 113031,
"categories": [{
"categoryCode": "guest_disk1",
"complexType": "SoftLayer_Product_Item_Category"
}],
"complexType": "SoftLayer_Product_Item_Price"
},
{
"id": 112707,
"categories": [{
"categoryCode": "guest_disk2",
"complexType": "SoftLayer_Product_Item_Category"
}],
"complexType": "SoftLayer_Product_Item_Price"
}
],
"properties": [
{"name": "NOTE_GENERAL", "value": "adding disks"},
{"name": "MAINTENANCE_WINDOW", "value": "now"}
],
"complexType": "SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade"
}]
})
response = urllib.urlopen(url, data)