JSON API Confusion

polaris · · 547 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hello, I&#39;m very fresh to Go. What I would consider to be a really quick script in Posh, I&#39;m having a hard time figuring out with Go.</p> <p>I&#39;m trying to figure out how to interact with my Zabbix server&#39;s API. I&#39;ve included what normal looks like as well.</p> <pre><code>/* This is a particular post that uses the user.login method POST http://myserver.mydomain.local/zabbix/api_jsonrpc.php { &#34;jsonrpc&#34;: &#34;2.0&#34;, &#34;method&#34;: &#34;user.login&#34;, &#34;params&#34;: { &#34;user&#34;: &#34;user1&#34;, &#34;password&#34;: &#34;Derp&#34; }, &#34;id&#34;: 1, &#34;auth&#34;: null } Json Response { &#34;jsonrpc&#34;: &#34;2.0&#34;, &#34;result&#34;: &#34;9433bae6b19c1fe84a2a75ed53d4622d&#34;, &#34;id&#34;: 1 } */ package main import ( &#34;fmt&#34; &#34;encoding/json&#34; &#34;net/http&#34; &#34;bytes&#34; &#34;io/ioutil&#34; ) type LoginPost struct { JSONrpc string `json:&#34;jsonrpc&#34;` Method string `json:&#34;method&#34;` ID int64 `json:&#34;id&#34;` Params struct { User string `json:&#34;user&#34;` Password string `json:&#34;password&#34;` } `json:&#34;params&#34;` } func main() { url := &#34;http://myserver.mydomain.local/zabbix/api_jsonrpc.php&#34; username := &#34;user1&#34; password := &#34;Derp&#34; login := &amp;LoginPost{ // Build my POST struct JSONrpc: &#34;2.0&#34;, Method: &#34;user.login&#34;, ID: 1, } login.Params.User = username login.Params.Password = password loginJSON, _ := json.Marshal(login) // Convert my login struct to JSON format fmt.Println(string(loginJSON)) // Check that my JSON looks good // OUTPUT // {&#34;jsonrpc&#34;:&#34;2.0&#34;,&#34;method&#34;:&#34;user.login&#34;,&#34;id&#34;:1,&#34;params&#34;:{&#34;user&#34;:&#34;user1&#34;,&#34;password&#34;:&#34;Derp&#34;}} b := new(bytes.Buffer) // initialize b, my body, json.NewEncoder(b).Encode(string(loginJSON)) // This piece converts it to an io.Reader type res, _ := http.Post(url, &#34;application/json&#34;, b) // Make the actual call // fmt.Println(res) // See the Raw Response body, _ := ioutil.ReadAll(res.Body) // convert to bytes // fmt.Println(body) // see the byte array fmt.Println(string(body)) // View the reply in readable JSON format // OUTPUT // {&#34;jsonrpc&#34;:&#34;2.0&#34;,&#34;error&#34;:{&#34;code&#34;:-32600,&#34;message&#34;:&#34;Invalid Request.&#34;,&#34;data&#34;:&#34;JSON-rpc version is not specified.&#34;},&#34;id&#34;:null} } </code></pre> <p>Looking up this error, <a href="https://www.zabbix.com/forum/showthread.php?t=53140" rel="nofollow">https://www.zabbix.com/forum/showthread.php?t=53140</a>, it looks like this happens when the server is expecting different input. Can I send it as a string rather than the io.Reader stream? I know I&#39;m clearly misunderstanding this. I&#39;m trying to figure out where. I&#39;m looking at several guides, and people seem to go about the same thing in so many ways it seems like they&#39;re almost conflicting. Any advice or guides would be appreciated.</p> <p>Edit: I found this guy&#39;s package for zabbix. I&#39;ll use it as a reference to understand what I&#39;ve been doing wrong. <a href="https://github.com/AlekSi/zabbix" rel="nofollow">https://github.com/AlekSi/zabbix</a></p>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

547 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传