I can do a POST request with string data, using POSTMAN chrome extension.
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.