duangao8359 2015-06-05 18:30
浏览 77
已采纳

使用HTTP协议和golang列出OrientDb数据库

I am trying to get a list of OrientDb databases using HTTP protocol. But I cannot get expected response, which I can get in a browser.

If I input in a browser address line http://localhost:2480/listDatabases then I have response:

{"@type":"d","@version":0,"databases":["MaximDB","GratefulDeadConcerts"],"@fieldTypes":"databases=e"}

How can I get the same using golang?

My code:

package main

import (
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    client := &http.Client{}
    req, err := http.NewRequest("GET", "http://localhost:2480/listDatabases", nil)
    req.SetBasicAuth("root", "1")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    fmt.Println("resp")
    fmt.Println(ToJson(resp))
}

func ToJson(obj interface{}) string {
    b, err := json.MarshalIndent(&obj, "", "   ")
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    strJson := string(b)

    return strJson
}

It outputs in console:

resp
{
   "Status": "200 OK",
   "StatusCode": 200,
   "Proto": "HTTP/1.1",
   "ProtoMajor": 1,
   "ProtoMinor": 1,
   "Header": {
      "Connection": [
         "Keep-Alive"
      ],
      "Content-Type": [
         "application/json; charset=utf-8"
      ],
      "Date": [
         "Fri Jun 05 22:19:23 MSK 2015"
      ],
      "Etag": [
         "0"
      ],
      "Server": [
         "OrientDB Server v.2.0.10 (build UNKNOWN@r; 2015-05-25 16:48:43+0000)"
      ],
      "Set-Cookie": [
         "OSESSIONID=-; Path=/; HttpOnly"
      ]
   },
   "Body": {},
   "ContentLength": -1,
   "TransferEncoding": null,
   "Close": false,
   "Trailer": null,
   "Request": {
      "Method": "GET",
      "URL": {
         "Scheme": "http",
         "Opaque": "",
         "User": null,
         "Host": "localhost:2480",
         "Path": "/listDatabases",
         "RawQuery": "",
         "Fragment": ""
      },
      "Proto": "HTTP/1.1",
      "ProtoMajor": 1,
      "ProtoMinor": 1,
      "Header": {
         "Authorization": [
            "Basic cm9vdDox"
         ]
      },
      "Body": null,
      "ContentLength": 0,
      "TransferEncoding": null,
      "Close": false,
      "Host": "localhost:2480",
      "Form": null,
      "PostForm": null,
      "MultipartForm": null,
      "Trailer": null,
      "RemoteAddr": "",
      "RequestURI": "",
      "TLS": null
   },
   "TLS": null
}
  • 写回答

1条回答 默认 最新

  • dqk42179 2015-06-05 22:04
    关注

    Your request is fine, it's the way you're trying to print out the response.

    You're marshaling the entire response object to JSON and you can see the "Body": {}, where your body is missing. A *http.Response won't marshal to JSON the way you want. This is because the Body field is not just a string or []bytes, it's an io.ReadCloser and the JSON marshaling code isn't going to call .Read on it.

    Try one of these to just get the response body

    var b bytes.Buffer
    _, err = b.ReadFrom(resp.Body)
    if err != nil {
        log.Fatal("Error : %s", err)
    }
    fmt.Println(b.String())
    

    or

    contents, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal("Error : %s", err)
    }
    fmt.Println(string(contents))
    

    Or to get the extra response meta information you could do this

    dump, err := httputil.DumpResponse(resp, true)
    if err != nil {
        log.Fatal("Error : %s", err)
    }
    fmt.Println(string(dump))
    

    (The second flag the true says to include the body, otherwise it would just show status and headers)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装