gpt4 book ai didi

go - 从 golang 调用时无法从调用 GetConfiguration 获取 itemCategory 信息

转载 作者:数据小太阳 更新时间:2023-10-29 03:22:56 26 4
gpt4 key购买 nike

API调用

GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/257/getConfiguration?objectMask=mask[itemCategory]

返回一个对象,[]datatypes.Product_Package_Order_Configuration 其中填充了 ItemCategory。示例输出:

{
"id": 7167,
"isRequired": 0,
"itemCategoryId": 390,
"orderStepId": 1,
"packageId": 257,
"sort": 0,
"itemCategory": {
"categoryCode": "trusted_platform_module",
"id": 390,
"name": "Server Security",
"quantityLimit": 1,
"sortOrder": 0
}
},

当我在 go 中尝试相同的调用时,itemCategory 没有被填充,即使我在掩码中声明了它。请指教。

package main

import (
"fmt"
"github.com/softlayer/softlayer-go/services"
"github.com/softlayer/softlayer-go/session"
"encoding/json"
)

func main() {
// SoftLayer API username and key
username := "set-me"
apikey := "set-me"

// Create a session
sess := session.New(username, apikey)

// Enable debug
sess.Debug = true

// Get SoftLayer_Account service
service := services.GetProductPackageService(sess)

// Object-Mask to get specific Vlan's information
mask := "itemCategory"

// Call to getNetworkVlans in order to retrieve vlans according to filter.
result, err := service.Mask(mask).Id(257).GetConfiguration()
if err != nil {
fmt.Printf("\n Unable to retrieve config:\n - %s\n", err)
return
}

// Following helps to print the result in json format.
jsonFormat, jsonErr := json.MarshalIndent(result,""," ")
if jsonErr != nil {
fmt.Println(jsonErr)
return
}
fmt.Println(string(jsonFormat))
}

示例输出:

->>>Request:
2018/05/09 09:24:36 POST /xmlrpc/v3.1/SoftLayer_Product_Package HTTP/1.1
Host: api.softlayer.com
User-Agent: Go-http-client/1.1
Content-Length: 1055
Content-Type: text/xml
Accept-Encoding: gzip

<?xml version="1.0" encoding="UTF-8"?><methodCall><methodName>getConfiguration</methodName><params><param><value><struct><member><name>headers</name><value><struct><member><name>User-Agent</name><value><string>softlayer-go/v0.1.0-alpha (go1.9.2;amd64;darwin)</string></value></member><member><name>authenticate</name><value><struct><member><name>username</name><value><string>[[SL_USER]]</string></value></member><member><name>apiKey</name><value><string>[[SL_APIKEY]]</string></value></member></struct></value></member><member><name>SoftLayer_Product_PackageInitParameters</name><value><struct><member><name>id</name><value><int>257</int></value></member></struct></value></member><member><name>SoftLayer_Product_PackageObjectMask</name><value><struct><member><name>mask</name><value><struct><member><name>itemCategory</name><value><array><data></data></array></value></member></struct></value></member></struct></value></member></struct></value></member></struct></value></param></params></methodCall>
2018/05/09 09:24:36


<<<-Response:
2018/05/09 09:24:36 HTTP/1.1 200 OK
Connection: close
Transfer-Encoding: chunked
Cache-Control: private
Content-Type: text/xml
Date: Wed, 09 May 2018 14:24:36 GMT
Server: Apache
Softlayer-Total-Items: 39
Strict-Transport-Security: max-age=31536000
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN

然后返回一串html,数据如下示例条目:

{
"id": 7167,
"isRequired": 0,
"itemCategoryId": 390,
"orderStepId": 1,
"packageId": 257,
"sort": 0
},

最佳答案

默认端点是 REST,但在您的环境中您使用的是 xmlrpc,这可能是由于存在 ~/.softlayer 文件,并且在该文件中它被配置为 XMLRPC enpoint。

有关更多信息,您可以查看以下文档: https://github.com/softlayer/softlayer-go

此问题发生在 XMLRPC enpoint 上,您可以在 go GitHub 中报告它。 https://github.com/softlayer/softlayer-go/issues/

在您的代码中尝试使用 REST 端点,就像这个例子:

endpoint := "https://api.softlayer.com/rest/v3"

// Create a session
sess := session.New(username, apikey, endpoint)

关于go - 从 golang 调用时无法从调用 GetConfiguration 获取 itemCategory 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50241215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com