{"id":98790,"date":"2020-04-04T19:24:30","date_gmt":"2020-04-04T11:24:30","guid":{"rendered":"http:\/\/4563.org\/?p=98790"},"modified":"2020-04-04T19:24:30","modified_gmt":"2020-04-04T11:24:30","slug":"%e6%b1%82%e5%8a%a9%ef%bc%8c-golang-http-post-%e8%af%b7%e6%b1%82%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=98790","title":{"rendered":"\u6c42\u52a9\uff0c golang http post \u8bf7\u6c42\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u52a9\uff0c golang http post \u8bf7\u6c42\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Aumujun <\/span>  <span><i><\/i> 49<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u671f\u671b\u7ed3\u679c:<\/p>\n<ul>\n<li>\u6b63\u786e\u83b7\u53d6 itop rest api \u8fd4\u56de\u7684\u5de5\u5355\u6570\u636e<\/li>\n<\/ul>\n<p>\u5f53\u6211\u4f7f\u7528 Python \u7684 requests \u5e93\u65f6\uff0c\u8fd9\u4e00\u5207\u90fd\u5f88\u6b63\u5e38\uff0c\u83b7\u53d6\u6570\u636e\u6210\u529f\uff08\u5982\u679c\u6211\u8986\u76d6\u9ed8\u8ba4 headers\uff0c\u83b7\u53d6\u7684\u7ed3\u679c\u5982 golang \u7248\u672c\u7684\u4e00\u6837\u3002<\/p>\n<p>\u8fd9\u662f Python \u7684\u4ee3\u7801:<\/p>\n<pre><code>#!\/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\": \"[email&#160;protected]\",     \"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()) <\/code><\/pre>\n<p>\u8f93\u51fa<\/p>\n<pre><code>{'objects': {'UserRequest::7': {'code': 0, 'message': '', 'class': 'UserRequest', 'key': '7', 'fields': {'request_type': 'service_request', 'servicesubcategory_name': '\u9489\u9489\u6743\u9650\u5f00\u901a', 'urgency': '4', 'origin': 'portal', 'caller_id_friendlyname': 'x \u963f\u91cc\u5408\u4f5c\u9879\u76ee\u8d1f\u8d23\u4eba', 'impact': '1', 'title': '\u6eab\u6c5f|A-222|wb-xxxxxxxx', 'description': '&lt;p&gt;this is a test approve...&lt;\/p&gt;'}}, 'UserRequest::6': {'code': 0, 'message': '', 'class': 'UserRequest', 'key': '6', 'fields': {'request_type': 'service_request', 'servicesubcategory_name': '\u9489\u9489\u6743\u9650\u5f00\u901a', 'urgency': '3', 'origin': 'portal', 'caller_id_friendlyname': 'x \u963f\u91cc\u5408\u4f5c\u9879\u76ee\u8d1f\u8d23\u4eba', 'impact': '1', 'title': '\u6210\u90fd|Xa-111|wb-xx111111', 'description': '&lt;p&gt;\u9019\u662f\u4e00\u500b\u6e2c\u8a66\u7528\u7684\u7528\u6236\u9700\u6c42&lt;\/p&gt;'}}}, 'code': 0, 'message': 'Found: 2'} <\/code><\/pre>\n<p>\u4e0b\u9762\u65f6 golang \u7248\u672c\u7684 post \u8bf7\u6c42<\/p>\n<p>\u4e3b\u51fd\u6570\uff1a<\/p>\n<pre><code>package main  import (  \"bytes\"  \"encoding\/json\"  \"fmt\"  \"io\/ioutil\"  \"log\"  \"net\/http\"  \"strconv\" )  \/\/ \u91d8\u91d8\u61c9\u7528\u7a0b\u5e8f\u7684 agentid 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 = \"[email&#160;protected]\"   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 } <\/code><\/pre>\n<p>post \u643a\u5e26\u7684\u6570\u636e\u6a21\u578b<\/p>\n<pre><code>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\"` } <\/code><\/pre>\n<p>\u8f93\u51fa:<\/p>\n<pre><code>{\"code\":5,\"message\":\"Error: Missing parameter 'auth_user'\"} <\/code><\/pre>\n<p>\u6211\u731c\u8fd9\u5e94\u8be5\u662f itop \u9700\u8981 post \u8bf7\u6c42\u643a\u5e26\u67d0\u4e2a header \uff1f\u4f46\u6211\u6298\u817e\u4e86\u592a\u4e45\uff0c\u76f4\u5230\u5b9e\u5728\u6ca1\u6709\u529e\u6cd5\u624d\u53d1\u5e16\u6c42\u52a9\u3002<\/p>\n<p>\u6c42\u5927\u4f6c\u4eec\u6307\u70b9\uff0c(\u62dc\u8c22<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"728547\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BlackBerry999 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             jsonData, err := json.Marshal(request_auth)<br \/>\u628a\u8fd9\u91cc\u7684 jsonData \u6253\u5370\u51fa\u6765 fmt.Println(string(jsonData )) \u770b\u662f\u5426\u5305\u542b [auth_user]                                                             <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728548\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Aumujun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BlackBerry999 <\/p>\n<p>&#8220;`json<br \/>{&#8220;auth_user&#8221;:&#8221;admin&#8221;,&#8221;auth_pwd&#8221;:&#8221;[email&#160;protected]&#8221;,&#8221;json_data&#8221;:&#8221;{&#8220;operation&#8221;:&#8221;core\/get&#8221;,&#8221;class&#8221;:&#8221;UserRequest&#8221;,&#8221;key&#8221;:&#8221;SELECT UserRequest WHERE operational_status = \\&#8221;ongoing\\&#8221;&#8221;,&#8221;output_fields&#8221;:&#8221;request_type,servicesubcategory_name,urgency,origin,caller_id_friendlyname,impact,title,description&#8221;}&#8221;}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728549\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : virusdefender <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             wireshark \u6293\u5305\u770b\u4e0b\u53d1\u51fa\u53bb\u7684\u662f\u5565\u6837\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728550\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BlackBerry999 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Aumujun \u6211\u89c9\u5f97\u662f json_data \u5185\u7684\u5b57\u7b26\u4e32\u95ee\u9898 \u4e8c\u6b21 JSON \u5e8f\u5217\u5316 \u5e26\u4e0a\u4e86\u5f88\u591a \u5bfc\u81f4\u670d\u52a1\u7aef\u5bf9\u8bf7\u6c42\u89e3\u6790\u9519\u8bef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728551\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Aumujun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @virusdefender hi\uff0c\u521a\u521a\u6211\u6293\u5305\u4e86\u4e00\u4e0b\uff0c\u53d1\u9001\u7684\u503c\u548c 2 \u6211\u53d1\u7684\u4e00\u6a21\u4e00\u6837\uff0c\u6709\u53ef\u80fd\u50cf\u56db\u8bf4\u7684\u90a3\u6837\u3002<\/p>\n<p>@BlackBerry999 \u5982\u679c\u662f\u8fd9\u6837\u6709\u4ec0\u4e48\u63a8\u8350\u7684\u89e3\u6cd5\u5417                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728552\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : useben <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             type RequestData struct {<br \/> Operation string `json:&#8221;operation&#8221;`<br \/> Class string `json:&#8221;class&#8221;`<br \/> Key string `json:&#8221;key&#8221;`<br \/> OutPutFields string `json:&#8221;output_fields&#8221;`<br \/>}<\/p>\n<p>type RequestAuth struct {<br \/> AuthUser string `json:&#8221;auth_user&#8221;`<br \/> AuthPwd string `json:&#8221;auth_pwd&#8221;`<br \/> \/\/ JsonData RequestData `json:&#8221;json_data&#8221;`<br \/> JsonData RequestData `json:&#8221;json_data&#8221;`<br \/>}<\/p>\n<p>\u4e0d\u9700\u8981\u4e8c\u6b21\u5e8f\u5217\u5316                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728553\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : index90 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684\u670d\u52a1\u5668\u63a5\u6536\u7684 post \u8bf7\u6c42\uff0c\u662f form data \u683c\u5f0f\u7684\u5427\uff0c\u4e0d\u662f json \u5427\u3002<br \/>r = requests.post(HOST, data=json_data)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728554\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kidtest <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u540c\u610f\u4e0a\uff0cpost \u9ed8\u8ba4\u7684 content-type \u5e94\u8be5\u662f&#8221;application\/x-www-form-unlencoded&#8221;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728555\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : index90 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             py \u91cc\uff0c\u5982\u679c\u60f3\u53d1\u9001 json \u6570\u636e\uff0c\u6b63\u786e\u7684\u65b9\u6cd5\u662f\uff1a<br \/>&#8220;`<br \/>r = requests .post(HOST, data=json_dump(json_data))<br \/>&#8220;`<br \/>\u6216\u8005<br \/>&#8220;`<br \/>r = requests .post(HOST, json=json_data)<br \/>&#8220;`<br \/>\u65e2\u7136\u4f60\u7684 py \u4ee3\u7801\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u5219\u8868\u660e\u670d\u52a1\u7aef\u628a\u4f60\u7684 post \u8bf7\u6c42\uff0c\u4ee5 formdata \u683c\u5f0f\u5904\u7406                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728556\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Vegetable <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @index90 \u6211\u731c\u662f\u8fd9\u6837\uff0crequests \u7684 data \u53c2\u6570\u53d1\u51fa\u53bb\u662f form\uff0ca=b&amp;c=d \u6837\u5f0f\u7684\uff0c\u4e0d\u662f json\uff0c\u800c\u4f60 golang \u7684\u4ee3\u7801\u662f\u7528 json \u53d1\u51fa\u53bb\u7684<br \/>\u7528 url.Values \u8bd5\u8bd5\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728557\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kidtest <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @kidtest <br \/>\u6240\u4ee5\uff0c\u5e94\u8be5\u4f7f\u7528 url.Values \u53bb\u5b58\u4f60\u7684\u90a3\u4e9b\u6570\u636e\uff0c\u7136\u540e\u5c06 Encode \u4e4b\u540e\u7684\u6570\u636e\u5f53\u505a body\uff0cpost \u51fa\u53bb\u3002<br \/>\u5177\u4f53\u53ef\u4ee5\u641c\u7d22\uff1agolang post form urlencoded                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"728558\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : Aumujun <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @index90 <br \/>@Vegetable <br \/>@kidtest <\/p>\n<p>\u611f\u8c22\u51e0\u4f4d\u7684\u56de\u590d\uff0c\u89e3\u51b3\u4e86\u56f0\u6270\u6211\u4e00\u5929\u7684\u96be\u9898\uff0c\u975e\u5e38\u611f\u8c22\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u52a9\uff0c golang http p&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98790"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=98790"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/98790\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=98790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=98790"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=98790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}