duancaiyi7567 2015-06-08 07:01
浏览 87
已采纳

带有字符串数据的POST golang请求

I can do a POST request with string data, using POSTMAN chrome extension.

enter image description here

I need to do the same using golang code.

But my Go code loses string INSERT INTO V SET name = 'jack', boss = #11:19 and posts empty data to the server.

package main

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

func main() {
    client := &http.Client{}
    // Why this stringData is lost and was not send with POST request?
    stringData := `INSERT INTO V SET name = 'jack', boss = #11:19`
    req, err := http.NewRequest("POST", "http://localhost:2480/command/GratefulDeadConcerts/sql", bytes.NewBufferString(stringData))
    req.SetBasicAuth("root", "1")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    fmt.Println("resp")
    fmt.Println(ToJson(resp))

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

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

    return strJson
}

It gives output:

resp
{
   "Status": "500 Internal Server Error",
   "StatusCode": 500,
   "Proto": "HTTP/1.1",
   "ProtoMajor": 1,
   "ProtoMinor": 1,
   "Header": {
      "Cache-Control": [
         "no-cache, no-store, max-age=0, must-revalidate"
      ],
      "Connection": [
         "Keep-Alive"
      ],
      "Content-Length": [
         "55"
      ],
      "Content-Type": [
         "text/plain; charset=utf-8"
      ],
      "Date": [
         "Mon Jun 08 10:47:46 MSK 2015"
      ],
      "Pragma": [
         "no-cache"
      ],
      "Server": [
         "OrientDB Server v.2.0.10 (build UNKNOWN@r; 2015-05-25 16:48:43+0000)"
      ]
   },
   "Body": {},
   "ContentLength": 55,
   "TransferEncoding": null,
   "Close": false,
   "Trailer": null,
   "Request": {
      "Method": "POST",
      "URL": {
         "Scheme": "http",
         "Opaque": "",
         "User": null,
         "Host": "localhost:2480",
         "Path": "/command/GratefulDeadConcerts/sql",
         "RawQuery": "",
         "Fragment": ""
      },
      "Proto": "HTTP/1.1",
      "ProtoMajor": 1,
      "ProtoMinor": 1,
      "Header": {
         "Authorization": [
            "Basic cm9vdDox"
         ]
      },
      "Body": {
         "Reader": {}
      },
      "ContentLength": 46,
      "TransferEncoding": null,
      "Close": false,
      "Host": "localhost:2480",
      "Form": null,
      "PostForm": null,
      "MultipartForm": null,
      "Trailer": null,
      "RemoteAddr": "",
      "RequestURI": "",
      "TLS": null
   },
   "TLS": null
}
java.lang.IllegalArgumentException: text cannot be null

How to make this POST request work successfully, using GoLang, as I can do with POSTMAN chrome extension?

Update

I made the same POST request successfully, using curl.

curl -X POST -u root:1 -H "Content-Type: application/json" --data-urlencode "INSERT INTO V SET name = 'jack', boss = #11:19"  http://localhost:2480/command/GratefulDeadConcerts/sql 

It outputs:

{"result":[{"@type":"d","@rid":"#9:822","@version":1,"@class":"V","name":null}]}

Update 2

Of course, I should not use --data-urlencode for curl command. The valid curl command is

curl -X POST -u root:1 -H "Content-Type: application/json" -d "INSERT INTO V SET name = 'jack', boss = #11:19"  http://localhost:2480/command/GratefulDeadConcerts/sql
{"result":[{"@type":"d","@rid":"#9:848","@version":1,"@class":"V","name":"jack","boss":"#11:19","@fieldTypes":"boss=x"}]}

I still have no clue how to make the similar GoLang POST request.

Update 3

url.QueryEscape did not help me.

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
    "net/http"
    "net/url"
)

func main() {
    client := &http.Client{}
    // Why this stringData is lost and do not pass thgouht POST request?
    stringData := `INSERT INTO V SET name = 'jack', boss = #11:19`
    stringData = url.QueryEscape(stringData)
    req, err := http.NewRequest("POST", "http://localhost:2480/command/GratefulDeadConcerts/sql", bytes.NewBufferString(stringData))
    req.SetBasicAuth("root", "1")
    req.Header.Set("Content-Type", "Content-Type: text/plain")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("Error : %s", err)
    }
    fmt.Println("resp")
    fmt.Println(ToJson(resp))

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

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

    return strJson
}

The result is the same:

resp
{
   "Status": "500 Internal Server Error",
   "StatusCode": 500,
   "Proto": "HTTP/1.1",
   "ProtoMajor": 1,
   "ProtoMinor": 1,
   "Header": {
      "Cache-Control": [
         "no-cache, no-store, max-age=0, must-revalidate"
      ],
      "Connection": [
         "Keep-Alive"
      ],
      "Content-Length": [
         "55"
      ],
      "Content-Type": [
         "text/plain; charset=utf-8"
      ],
      "Date": [
         "Mon Jun 08 19:39:12 MSK 2015"
      ],
      "Pragma": [
         "no-cache"
      ],
      "Server": [
         "OrientDB Server v.2.0.10 (build UNKNOWN@r; 2015-05-25 16:48:43+0000)"
      ]
   },
   "Body": {},
   "ContentLength": 55,
   "TransferEncoding": null,
   "Close": false,
   "Trailer": null,
   "Request": {
      "Method": "POST",
      "URL": {
         "Scheme": "http",
         "Opaque": "",
         "User": null,
         "Host": "localhost:2480",
         "Path": "/command/GratefulDeadConcerts/sql",
         "RawQuery": "",
         "Fragment": ""
      },
      "Proto": "HTTP/1.1",
      "ProtoMajor": 1,
      "ProtoMinor": 1,
      "Header": {
         "Authorization": [
            "Basic cm9vdDox"
         ],
         "Content-Type": [
            "Content-Type: text/plain"
         ]
      },
      "Body": {
         "Reader": {}
      },
      "ContentLength": 60,
      "TransferEncoding": null,
      "Close": false,
      "Host": "localhost:2480",
      "Form": null,
      "PostForm": null,
      "MultipartForm": null,
      "Trailer": null,
      "RemoteAddr": "",
      "RequestURI": "",
      "TLS": null
   },
   "TLS": null
}
java.lang.IllegalArgumentException: text cannot be null

Also, I changed content-type to text/plain in curl command.

curl -X POST -u root:1 -H "Content-Type: text/plain" -d "INSERT INTO V SET name = 'jack', boss = #11:19"  http://localhost:2480/command/GratefulDeadConcerts/sql
{"result":[{"@type":"d","@rid":"#9:858","@version":1,"@class":"V","name":"jack","boss":"#11:19","@fieldTypes":"boss=x"}]}

The result is the same, it's ok with curl, but not ok with golang POST.

Update 4

What am I trying to do? I am implementing OrientDB REST-interface for executing queries from a GO application.

  • 写回答

1条回答 默认 最新

  • dongpu1908 2015-06-08 16:53
    关注

    Reading the documentation clearly helps. From the OrientDB documentation documentation [1]:

    All the requests must have these 2 headers:

    'Accept-Encoding': 'gzip,deflate'
    'Content-Length': <content-length>
    

    Where the is the length of the request's body.

    And you do not set the Accept-Encoding header. Most probably this is all you need (and not escaping the body).

    [1] http://orientdb.com/docs/last/orientdb.wiki/OrientDB-REST.html#headers

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条