我目前遇到一个真正让我感到沮丧的问题,我绝对看不到我的代码有任何问题。我想要实现的是将 http POST 消息发送到我编写的 iDrac 模型(这两个软件都是用 golang 编写的)以控制模型的电源状态,但无论我为请求配置什么,模型总是收到 get带有空体的请求。我创建并发送请求的功能:func (iDrac *IDrac) SetPowerstate(powerstate string) error { //Create reset type json string var jsonStr = `{"ResetType":"` + powerstate + `"}` //Create the request with auth header req, reqErr := iDrac.buildHttpRequestWithAuthorizationHeader(http.MethodPost, "https://"+iDrac.hostAddress+apiBaseURL+apiChassisURL+apiChassisResetAction, jsonStr) if reqErr != nil { return fmt.Errorf("COULD_NOT_CREATE_REQUEST: " + reqErr.Error()) } req.Header.Set("Content-Type", "application/json; charset=UTF-8") //Make the request resp, doErr := http.DefaultClient.Do(req) if doErr != nil { return fmt.Errorf("COULD_NOT_SEND_POST_REQUEST_TO_IDRAC_API" + doErr.Error()) } //Check if the request was successful if resp.StatusCode != 200 { return fmt.Errorf("COULD_NOT_CHANGE_SERVER_POWER_STATUS_OVER_IDRAC HTTP:" + resp.Status) } return nil}我用来构建请求的辅助函数:func (iDrac *IDrac) buildHttpRequestWithAuthorizationHeader(method string, url string, content string) (*http.Request, error) { req, err := http.NewRequest(method, url, bytes.NewBuffer([]byte(content))) if err != nil { return nil, err } req.Header.Add("Authorization", "Basic "+iDrac.hashedCredentials) return req, nil}最后是模型处理请求的函数:func handlePerformReset(w http.ResponseWriter, r *http.Request) { garbagelog.Log("Handling reset perform request from " + r.RemoteAddr) if r.Method != http.MethodPost { garbagelog.Log("Invalid http method! Got " + r.Method + " expected POST") w.WriteHeader(405) return } if !checkAuthorization(r.BasicAuth()) { w.WriteHeader(401) return }
2 回答
千万里不及你
TA贡献1784条经验 获得超9个赞
我发现了问题:
我构建 url 的常量定义如下:
const (
apiBaseURL = "/redfish/v1/"
apiChassisURL = "/Systems/System.Embedded.1"
apiChassisResetAction = "/Actions/ComputerSystem.Reset"
)
导致一个看起来像这样的 url:
https://host/redfish/v1//Systems/System.Embedded.1/Actions/ComputerSystem.Reset (注意 v1 和 Systems 之间的双//)
所以我已经修好了:
const (
apiBaseURL = "/redfish/v1"
apiChassisURL = "/Systems/System.Embedded.1"
apiChassisResetAction = "/Actions/ComputerSystem.Reset"
)
一切正常: 测试结果显示每个测试都成功
我感谢大家的意见,帮助我没有完全失去理智。
- 2 回答
- 0 关注
- 100 浏览
添加回答
举报
0/150
提交
取消