[当我使用Go的标准http
库将发布请求发送到itop rest api时,出现此错误。
{"code":5,"message":"Error: Missing parameter 'auth_user'"}
这是请求数据模型:
package main
// UserRequest structure
type Base struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Fileds struct {
RequestType string `json:"request_type"`
ServiceSubcategoryName string `json:"servicesubcategory_name"`
Urgency string `json:"urgency"`
Origin string `json:"origin"`
CallerIdFriendlyName string `json:"caller_id_friendlyname"`
Impact string `json:"impact"`
Title string `json:"title"`
Description string `json:"description"`
}
type ResponseContent struct {
Code int `json:"code"`
Message string `json:"message"`
Class string `json:"class"`
Key string `json:"key"`
Filed Fileds `json:"fields"`
}
type Response struct {
Base
Object map[string]ResponseContent `json:"objects"`
}
// Request api data struct
type RequestData struct {
Operation string `json:"operation"`
Class string `json:"class"`
Key string `json:"key"`
OutPutFields string `json:"output_fields"`
}
type RequestAuth struct {
AuthUser string `json:"auth_user"`
AuthPwd string `json:"auth_pwd"`
// JsonData RequestData `json:"json_data"`
JsonData string `json:"json_data"`
}
主要功能:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
const (
ITOP_URL = `http://192.168.17.22:8096/itop/webservices/rest.php?version=1.3`
)
func main() {
request_auth := new(RequestAuth)
request_data := new(RequestData)
request_auth.AuthUser = "admin"
request_auth.AuthPwd = "goodjob@123"
request_data.Operation = "core/get"
request_data.Class = "UserRequest"
request_data.Key = "SELECT UserRequest WHERE operational_status = \"ongoing\""
request_data.OutPutFields = "request_type,servicesubcategory_name,urgency,origin,caller_id_friendlyname,impact,title,description"
req_data, err := json.Marshal(request_data)
if err != nil {
panic(err)
}
request_auth.JsonData = string(req_data)
jsonData, err := json.Marshal(request_auth)
if err != nil {
panic(err)
}
reader := bytes.NewReader(jsonData)
result := Post(ITOP_URL, reader)
fmt.Println(string(result))
}
func Post(url string, reader *bytes.Reader) []byte {
request, err := http.NewRequest("POST", url, reader)
if err != nil {
panic(err)
}
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Content-Length", strconv.Itoa(reader.Len()))
client := http.Client{}
resp, err := client.Do(request)
if err != nil {
log.Fatal(err.Error())
}
defer resp.Body.Close()
respBytes, _ := ioutil.ReadAll(resp.Body)
return respBytes
}
但是当我使用Python的请求库时,我没有这个问题。我认为请求已进行了某些处理?
#!/usr/bin/python3
import requests, json
HOST = "http://192.168.17.22:8096/itop/webservices/rest.php?version=1.3"
json_str = json.dumps({
"operation":
"core/get",
"class":
"UserRequest",
"key":
"SELECT UserRequest WHERE operational_status = 'ongoing'",
"output_fields":
"request_type,servicesubcategory_name,urgency,origin,caller_id_friendlyname,impact,title,description",
})
json_data = {
"auth_user": "admin",
"auth_pwd": "goodjob@123",
"json_data": json_str
}
# secure_rest_services
def get():
r = requests.post(HOST, data=json_data)
return r
if __name__ == "__main__":
result = get()
print(result.json())
输出:
{'objects': {'UserRequest::7': {'code': 0, 'message': '', 'class': 'UserRequest', 'key': '7', 'fields': {'request_type': 'service_request', 'servicesubcategory_name': '钉钉权限开通', 'urgency': '4', 'origin': 'portal', 'caller_id_friendlyname': 'x 阿里合作项目负责人', 'impact': '1', 'title': '溫江|A-222|wb-xxxxxxxx', 'description': '<p>this is a test approve...</p>'}}, 'UserRequest::6': {'code': 0, 'message': '', 'class': 'UserRequest', 'key': '6', 'fields': {'request_type': 'service_request', 'servicesubcategory_name': '钉钉权限开通', 'urgency': '3', 'origin': 'portal', 'caller_id_friendlyname': 'x 阿里合作项目负责人', 'impact': '1', 'title': '高朋|Xa-111|wb-xx111111', 'description': '<p>這是一個測試用的用戶需求</p>'}}}, 'code': 0, 'message': 'Found: 2'}
当我重写Python请求的默认标头时,我返回与Go相同的结果。
headers = {"Content-Type":"application/json"}
requests.post(url, data=json_data, headers=headers)
也许请求** headers **有问题?
使用url.Values将数据发布到api,python requests
库默认使用from模式。
Itop仅接受表单格式