How to save and retrieve Gorm's many2many relationship using Go - go

I want to create a rest API to manage casts using Gorm and Gin. When I add a relation between two of my models and submitting those items to the API, I cannot make that expected relationship between those. and the BornLocations and Nationalities fields are null. Have I missed something here?
My question is: How can I store and retrieve that relationship in my responses?
My Models:
type Cast struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
FullName string `gorm:"size:150;not null" json:"full_name"`
NickNames string `gorm:"size:250;null;" json:"nick_names"`
BornLocation Country `gorm:"many2many:CastBornLocation;association_foreignkey:ID;foreignkey:ID" json:"born_location"`
Nationalities []Country `gorm:"many2many:Castnationalities;association_foreignkey:ID;foreignkey:ID" json:"cast_nationalities"`
MiniBio string `gorm:"size:1000;null;" json:"mini_bio"`
UserRefer string
}
type Country struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
Title string `gorm:"size:100;not null" json:"title"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
And here is the controller:
func (server *Server) CreateCast(c *gin.Context) {
errList = map[string]string{}
item := models.Cast{}
err = json.Unmarshal(body, &item)
if err != nil {
errList["Unmarshal_error"] = "Cannot unmarshal body"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
// check the token
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
user := models.User{}
err = server.DB.Debug().Model(models.User{}).Where("id = ?", uid).Take(&user).Error
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
item.UserRefer = uid
item.Prepare()
errorMessages := item.Validate("")
if len(errorMessages) > 0 {
errList = errorMessages
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
itemCreated, err := item.SaveCast(server.DB) //Saving the cast!
if err != nil {
formattedError := formaterror.FormatError(err.Error())
errList = formattedError
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": errList,
})
return
}
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusCreated,
"response": itemCreated,
})
}
And this is the JSON body I'm submitting to the API:
{
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born_location": {
"ID" :"caf2d6af-c360-4777-85d4-32541094b8be"
},
"cast_nationalities": [
{
"ID" :"caf2d6af-c360-4777-85d4-32541094b8be"
},
{
"ID" :"064058f4-f4ea-4d28-ad22-f9cf92df3513"
}
],
"mini_bio": "this is the mini bio of the cast"
}
And now, this is the response:
{
"response": {
"ID": "09f6a184-9b6e-4487-85f0-c2dbbce7ee5b",
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born": "",
"born_location": {
"ID": "e5c431ed-3158-4532-81b6-52d78e5539dc",
"title": "",
"created_at": "2020-07-16T13:23:32.579892866Z",
"updated_at": "2020-07-16T13:23:32.578216004Z",
"UserRefer": ""
},
"cast_nationalities": [
{
"ID": "caf2d6af-c360-4777-85d4-32541094b8be",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2020-07-16T13:23:32.580620065Z",
"UserRefer": ""
},
{
"ID": "064058f4-f4ea-4d28-ad22-f9cf92df3513",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2020-07-16T13:23:32.58249818Z",
"UserRefer": ""
}
],
"mini_bio": "this is the mini bio of the cast",
"bio": "",
"avatar": "",
"height": "",
"weight": "",
"created_at": "2020-07-16T13:23:32.576892187Z",
"updated_at": "2020-07-16T13:23:32.576892346Z",
"UserRefer": "c0223913-8679-4103-bcb6-66db0b2fed21"
},
"status": 201
}
And this is the controller and response for getting all the casts:
func (server *Server) GetCasts(c *gin.Context) {
//clear previous error if any
errList = map[string]string{}
item := models.Cast{}
// check the token
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
items, err := item.FindCasts(server.DB, uid)
if err != nil {
errList["No_Items"] = "No Items Found"
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": errList,
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": items,
})
}
func (u *Cast) FindCasts(db *gorm.DB, uid string) (*[]Cast, error) {
var err error
casts := []Cast{}
err = db.Debug().Model(&Cast{}).Where("user_refer = ?", uid).Limit(100).Find(&casts).Error
if err != nil {
return &[]Cast{}, err
}
return &casts, err
}
And the final response for the casts:
{
"response": [
{
"ID": "09f6a184-9b6e-4487-85f0-c2dbbce7ee5b",
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born": "",
"born_location": {
"ID": "",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"UserRefer": ""
},
"nationalities": null,
"mini_bio": "this is the mini bio of the cast",
"bio": "",
"avatar": "",
"height": "",
"weight": "",
"created_at": "2020-07-16T13:23:32.576892Z",
"updated_at": "2020-07-16T13:23:32.576892Z",
"UserRefer": "c0223913-8679-4103-bcb6-66db0b2fed21"
}
],
"status": 200

For fetching data you can use preload child data in gorm
err = db.Preload("BornLocation").Preload("Nationalities").Model(&Cast{})
.Where("user_refer = ?", uid).Limit(100).Find(&casts).Error
Or you can use flag
db.Set("gorm:auto_preload", true).Find(&casts).Error
Here you BornLocation seems like OneToMany you can do like this
type Cast struct {
...
BornLocationId string
BornLocation Country `gorm:"association_foreignkey:ID;foreignkey:BornLocationId" json:"born_location"`
...
}

I found the issue! for this specific issue, I had to add two things.
First, I had to add the relation identifier in the model's prepare function.
u.BornLocation = Country{}
Then, to save the cast entity, I had to change it to:
func (u *Cast) SaveCast(db *gorm.DB) (*Cast, error) {
var err error
err = db.Debug().Create(&u).Error
if err != nil {
return &Cast{}, err
}
if u.ID != 0 {
err = db.Debug().Model(&Country{}).Where("id = ?", u.BornLocationId).Take(&u.Country).Error
if err != nil {
return &Cast{}, err
}
}
return u, nil
}
And now, for retrieving all the entities:
err = db.Set("gorm:auto_preload", true).Find(&casts).Error

Related

Why golang go-gin error always returning empty object

im trying to parse error validation in gin golang, but produce an empty object "{}"
Here is my 1st attempt :
resp, err := userService.UserRegistrationService(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
returning :
{
"error": [
{},
{}
]
}
Here is my 2nd attempt :
resp, err := userService.UserRegistrationService(c)
if err != nil {
dada := fmt.Errorf("%v", err)
c.JSON(http.StatusBadRequest, dada)
return
}
returning
{}
Take a look if I debug the err parameter :
resp, err := userService.UserRegistrationService(c)
fmt.Println(err)
if err != nil {
dada := fmt.Errorf("%v", err)
fmt.Println(dada)
c.JSON(http.StatusBadRequest, dada)
return
}
Its return :
2020/11/10 12:41:46 stdout: Key: 'User.Email' Error:Field validation for 'Email' failed on the 'required' tag
2020/11/10 12:41:46 stdout: Key: 'User.Password' Error:Field validation for 'Password' failed on the 'required' tag
Can you guys help me, trying to search anywhere still no luck, I want return like this :
{
"MyRequestStruct.PropertyOne": {
"FieldNamespace": "MyRequestStruct.PropertyOne",
"NameNamespace": "PropertyOne",
"Field": "PropertyOne",
"Name": "PropertyOne",
"Tag": "required",
"ActualTag": "required",
"Kind": 24,
"Type": {},
"Param": "",
"Value": ""
},
"MyRequestStruct.PropertyTwo": {
"FieldNamespace": "MyRequestStruct.PropertyTwo",
"NameNamespace": "PropertyTwo",
"Field": "PropertyTwo",
"Name": "PropertyTwo",
"Tag": "required",
"ActualTag": "required",
"Kind": 24,
"Type": {},
"Param": "",
"Value": ""
}
}
You should try err.Error() instead of err while publishing as json response.
Try this :
resp, err := userService.UserRegistrationService(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
how to use it without an empty return?
resp, err := userService.UserRegistrationService(c)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error()
})
return
}

Unmarshalling nested json object from http request returns nil

I've been going through other similar questions here but I don't know what I'm doing wrong.
I am calling this API:
https://coronavirus-tracker-api.herokuapp.com/v2/locations
Which returns a JSON object like this one:
{
"latest": {
"confirmed": 272166,
"deaths": 11299,
"recovered": 87256
},
"locations": [
{
"id": 0,
"country": "Thailand",
"country_code": "TH",
"province": "",
"last_updated": "2020-03-21T06:59:11.315422Z",
"coordinates": {
"latitude": "15",
"longitude": "101"
},
"latest": {
"confirmed": 177,
"deaths": 1,
"recovered": 41
}
},
{
"id": 39,
"country": "Norway",
"country_code": "NO",
"province": "",
"last_updated": "2020-03-21T06:59:11.315422Z",
"coordinates": {
"latitude": "60.472",
"longitude": "8.4689"
},
"latest": {
"confirmed": 1463,
"deaths": 3,
"recovered": 1
}
}
]
}
So I have written a small program to parse it but I can only parse the outer object ("latest") while the inner array ("locations") always returns nil.
Code is here (even if TCP calls don't work on the playground):
https://play.golang.org/p/ma225d07iRA
and here:
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
type AutoGenerated struct {
Latest Latest `json:"latest"`
Locations []Locations `json:"locations"`
}
type Latest struct {
Confirmed int `json:"confirmed"`
Deaths int `json:"deaths"`
Recovered int `json:"recovered"`
}
type Coordinates struct {
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
}
type Locations struct {
ID int `json:"id"`
Country string `json:"country"`
CountryCode string `json:"country_code"`
Province string `json:"province"`
LastUpdated time.Time `json:"last_updated"`
Coordinates Coordinates `json:"coordinates"`
Latest Latest `json:"latest"`
}
var latestUrl = "https://coronavirus-tracker-api.herokuapp.com/v2/latest"
func getJson(url string, target interface{}) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
decoder := json.NewDecoder(res.Body)
var data AutoGenerated
err = decoder.Decode(&data)
if err != nil {
fmt.Println(err)
}
for i, loc := range data.Locations {
fmt.Printf("%d: %s", i, loc.Country)
}
defer res.Body.Close()
}
func main() {
var a AutoGenerated
getJson(latestUrl, &a)
}
The problem is that the endpoint https://coronavirus-tracker-api.herokuapp.com/v2/latest does not return locations. This is the response I get by calling it:
{
"latest": {
"confirmed": 304524,
"deaths": 12973,
"recovered": 91499
}
}
However if you call the correct endpoint https://coronavirus-tracker-api.herokuapp.com/v2/locations, it might work.

Custom built JSON schema not validating properly

I have a custom built JSON schema that only has a few more top-levels. The problem here is that it doesn't validate everything to 100%. For example, it only detects 2 out of 4 fields, and the required fields do not work at all, neither does additionalproperties, etc. I'm using this library for my json schema.
{
"users": {
"PUT": {
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"DisplayName",
"Username",
"Email",
"Password"
],
"properties": {
"DisplayName": {
"$id": "#/properties/DisplayName",
"type": "string",
"title": "The Displayname Schema",
"default": "",
"examples": [
""
],
"minLength": 3,
"maxLength": 24,
"pattern": "^(.*)$"
},
"Username": {
"$id": "#/properties/Username",
"type": "string",
"title": "The Username Schema",
"default": "",
"examples": [
""
],
"minLength": 3,
"maxLength": 15,
"pattern": "^(.*)$"
},
"Email": {
"$id": "#/properties/Email",
"type": "string",
"title": "The Email Schema",
"default": "",
"examples": [
""
],
"minLength": 7,
"pattern": "^(.*)$",
"format": "email"
},
"Password": {
"$id": "#/properties/Password",
"type": "string",
"title": "The Password Schema",
"default": "",
"examples": [
""
],
"pattern": "^(.*)$"
}
},
"additionalProperties": false
}
}
}
I'm parsing everything like this:
func Validate(data interface{}, r *http.Request) (interface{}, error) {
// Convert the data struct to a readable JSON bytes
JSONparams, err := json.Marshal(data)
if err != nil {
return nil, err
}
// Split URL segments so we know what part of the API they are accessing
modules := strings.Split(r.URL.String(), "/")
modules = modules[(len(modules) - 1):]
// Read the schema file
fileSchema, _ := ioutil.ReadFile("config/schema/schema.json")
var object interface{}
// Unmarshal it so we can choose what schema we specifically want
err = json.Unmarshal(fileSchema, &object)
if err != nil {
log.Fatal(err)
}
// Choose the preferred schema
encodedJSON, err := json.Marshal(object.(map[string]interface{})[strings.Join(modules, "") + "s"].(map[string]interface{})[r.Method])
if err != nil {
log.Fatal(err)
}
// Load the JSON schema
schema := gojsonschema.NewStringLoader(string(encodedJSON))
// Load the JSON params
document := gojsonschema.NewStringLoader(string(JSONparams))
// Validate the document
result, err := gojsonschema.Validate(schema, document)
if err != nil {
return nil, err
}
if !result.Valid() {
// Map the errors into a new array
var errors = make(map[string]string)
for _, err := range result.Errors() {
errors[err.Field()] = err.Description()
}
// Convert the array to an interface that we can convert to JSON
resultMap := map[string]interface{}{
"success": false,
"result": map[string]interface{}{},
"errors": errors,
}
// Convert the interface to a JSON object
errorObject, err := json.Marshal(resultMap)
if err != nil {
return nil, err
}
return errorObject, nil
}
return nil, nil
}
type CreateParams struct {
DisplayName string
Username string
Email string
Password string
}
var (
response interface{}
status int = 0
)
func Create(w http.ResponseWriter, r *http.Request) {
status = 0
// Parse the request so we can access the query parameters
r.ParseForm()
// Assign them to the interface variables
data := &CreateParams{
DisplayName: r.Form.Get("DisplayName"),
Username: r.Form.Get("Username"),
Email: r.Form.Get("Email"),
Password: r.Form.Get("Password"),
}
// Validate the JSON data
errors, err := schema.Validate(data, r)
if err != nil {
responseJSON := map[string]interface{}{
"success": false,
"result": map[string]interface{}{},
}
log.Fatal(err.Error())
response, err = json.Marshal(responseJSON)
status = http.StatusInternalServerError
}
// Catch any errors generated by the validator and assign them to the response interface
if errors != nil {
response = errors
status = http.StatusBadRequest
}
// Status has not been set yet, so it's safe to assume that everything went fine
if status == 0 {
responseJSON := map[string]interface{}{
"success": true,
"result": map[string]interface{} {
"DisplayName": data.DisplayName,
"Username": data.Username,
"Email": data.Email,
"Password": nil,
},
}
response, err = json.Marshal(responseJSON)
status = http.StatusOK
}
// We are going to respond with JSON, so set the appropriate header
w.Header().Set("Content-Type", "application/json")
// Write the header and the response
w.WriteHeader(status)
w.Write(response.([]byte))
}
The reason to why I'm doing it like this is I'm building a REST API and if api/auth/user gets a PUT request, I want to be able to specify the data requirements for specifically the "users" parts with the PUT method.
Any idea how this can be achieved?
EDIT:
My json data:
{
"DisplayName": "1234",
"Username": "1234",
"Email": "test#gmail.com",
"Password": "123456"
}
EDIT 2:
This data should fail with the schema.
{
"DisplayName": "1", // min length is 3
"Username": "", // this field is required but is empty here
"Email": "testgmail.com", // not following the email format
"Password": "123456111111111111111111111111111111111111111111111" // too long
}
If I manually load the schema and data using gojsonschema it works as expected. I suspect that since you're loading the schema in a somewhat complicated fashion the schema you put in ends up being something different than what you'd expect, but since your code samples are all HTTP based I can't really test it out myself.

Why does unmarshalling this API response return an unexpected EOF?

Problem
I'm creating a microservice in Go, using protocol buffers and gRPC. It interacts with a third-party API (Snooth) and I'm trying to unmarshal the JSON response into a protobuf struct I've created, using the proto package.
Unmarshalling returns an unexpected EOF error.
What I've Tried
Using json.newDecoder instead of json.unmarshal
jsonpb.Unmarshal instead of proto.Unmarshal (returns a bad value in StructValue for key error)
Limiting the response read with io.LimitReader
I've also read something about prefixing the proto types with a size tag or something? But I'm not sure what that is or if it's relevant. Here's the repo on Github.
Question
What is causing this unexpected EOF error and how do I fix it, so that the API response is successfully unmarshalled into the proto struct?
Side note: I am new to Go and would also appreciate any feedback/improvements on the following code. Thanks!
Code
Proto
message Response {
message Meta {
int32 results = 1;
int32 returned = 2;
string errmsg = 3;
int32 status = 4;
}
Meta meta = 1;
repeated google.protobuf.Struct wines = 2;
repeated google.protobuf.Struct actions = 3;
}
main.go
func fetchFromSnooth(e string, qs string, c chan response) {
defer func() {
if r := recover(); r != nil {
log.Printf("Error fetching from Snooth: %s", r)
errmsg := fmt.Sprint(r)
c <- response{nil, snoothApiError{errmsg}}
}
}()
v := url.Values{"akey": {os.Getenv("SNOOTH_API_KEY")}}
requestUrl := fmt.Sprintf("%s%s/?%s%s", snoothRoot, e, v.Encode(), qs)
log.Printf("Fetching: %s", requestUrl)
res, err := httpClient.Get(requestUrl)
if err != nil {
panic(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
result := &pb.Response{}
if err := proto.Unmarshal(body, result); err != nil {
panic(err)
}
c <- response{result, snoothApiError{""}}
}
func (s *snoothApiService) searchWines(params *pb.Parameters_WineSearch) response {
c := make(chan response)
defer close(c)
v := url.Values{}
go fetchFromSnooth("wines", v.Encode(), c)
return <-c
}
func main() {
snooth := snoothApiService{}
resp := snooth.searchWines(nil)
fmt.Println(resp)
}
Edit
Here's an example of the type of API response I'm trying to unmarshal:
{
"meta": {
"results": 1489442,
"returned": 5,
"errmsg": "",
"status": 1
},
"wines": [
{
"name": "Conway Deep Sea Chardonnay la Costa Wine Co",
"code": "conway-deep-sea-chardonnay-la-costa-wine-co-2008-1",
"region": "USA > California > Central Coast",
"winery": "Conway Family Wines",
"winery_id": "conway-family-wines",
"varietal": "Chardonnay",
"price": "21.99",
"vintage": "2008",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/conway-deep-sea-chardonnay-la-costa-wine-co-2008-1\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/0\/2\/8\/image_787698_square.jpeg",
"snoothrank": 3,
"available": 0,
"num_merchants": 0,
"num_reviews": 10
},
{
"name": "Olmaia Cabernet di Toscana",
"code": "olmaia-cabernet-di-toscana",
"region": "Italy > Tuscany > Toscana Igt",
"winery": "Col D Orcia",
"winery_id": "col-d-orcia",
"varietal": "Cabernet Sauvignon",
"price": "0.00",
"vintage": "",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/olmaia-cabernet-di-toscana\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/e\/e\/image_790198_square.jpeg",
"snoothrank": 3.5,
"available": 0,
"num_merchants": 0,
"num_reviews": 25
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "15.89",
"vintage": "2006",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2006\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/d\/0\/4\/image_336595_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "Dominio Dostares Prieto Picudo Vino de la Tierra de Castilla Y León Cumal",
"code": "dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005",
"region": "Spain > Castilla y León > Vino de la Tierra de Castilla y León",
"winery": "Bischöfliches Priesterseminar Trier",
"winery_id": "bischofliches-priesterseminar-trier",
"varietal": "Prieto Picudo",
"price": "38.99",
"vintage": "2005",
"type": "Red Wine",
"link": "http:\/\/www.snooth.com\/wine\/dominio-dostares-prieto-picudo-vino-de-la-tierra-de-castilla-y-leon-cumal-2005\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/1\/d\/a\/image_336596_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 1
},
{
"name": "The Little Penguin Chardonnay Premier",
"code": "the-little-penguin-chardonnay-premier-2010",
"region": "South East Australia",
"winery": "The Little Penguin",
"winery_id": "the-little-penguin",
"varietal": "Chardonnay",
"price": "11.99",
"vintage": "2010",
"type": "White Wine",
"link": "http:\/\/www.snooth.com\/wine\/the-little-penguin-chardonnay-premier-2010\/",
"tags": "",
"image": "https:\/\/ei.isnooth.com\/multimedia\/2\/c\/4\/image_826282_square.jpeg",
"snoothrank": "n\/a",
"available": 0,
"num_merchants": 0,
"num_reviews": 7
}
]
}
It DOES parse with then encoding/json package: https://play.golang.org/p/IQzMm2tDI7w
The protoc-generated code's Unmarshal parses a Protocol Buffers encoded byte stream, NOT JSON!
--Update--
I've now got the response unmarshalling as desired, using the jsonpb.Unmarshal method. To do so though I had to unmarshal and marshal the response with the regular json library first, in order to get around some escaped values in the response (I was receiving a 'bad value in Struct' error:
resJson, err := ioutil.ReadAll(res.Body)
j := make(map[string]interface{})
if err := json.Unmarshal(resJson, &j); err != nil {
panic(err)
}
jbytes, err := json.Marshal(j)
result := &pb.Response{}
r := strings.NewReader(string(jbytes))
if err := jsonpb.Unmarshal(r, result); err != nil {
panic(err)
}

list OrientDb databases using HTTP protocol and golang

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
}
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)

Resources