XML Applications Golang

Hello everyone, in my last blog, Golang SQL JSON

I used JSON for transferring data among application and service, now using same code with a small tweak we can use XML in place of JSON.

The code now looks like this:

Main.go

package main

import (
       "net/http"
       "fmt"
       "io"
       _ "github.com/go-sql-driver/mysql"
       "database/sql"

       "strconv"
       "encoding/xml"
)

var appdatabase *sql.DB



type Userstruct struct {
       Username string
       Age string
       Salary string
}


func insertInDatabase(data Userstruct) error {
       age,_ := strconv.Atoi(data.Age)
       salary,_ := strconv.Atoi(data.Salary)
       _, err := appdatabase.Exec("INSERT INTO emptable(name, age, salary) VALUES(?, ?, ?)", data.Username , age, salary)
       return err

}

func getFromdatabase(uname string, w http.ResponseWriter) error{

       out := Userstruct{}

       err := appdatabase.QueryRow("SELECT * FROM emptable WHERE name=?", uname).Scan(&out.Username, &out.Age, &out.Salary)

       if err != nil {
              return err
       }

       enc := xml.NewEncoder(w)

       err = enc.Encode(&out)

       return err
}

func userAddHandler(w http.ResponseWriter, r *http.Request) {


       //make byte array
       out := make([]byte,1024)

       //
       bodyLen, err := r.Body.Read(out)

       if err != io.EOF {
              fmt.Println(err.Error())
              w.Write([]byte("{error:" + err.Error() + "}"))
              return
       }

       var k Userstruct

       fmt.Println(string(out))

       err = xml.Unmarshal(out[:bodyLen],&k)


       if err != nil {
              w.Write([]byte("{error:" + err.Error() + "}"))
              return
       }

       err = insertInDatabase(k)

       if err != nil {
              w.Write([]byte("{error:" + err.Error() + "}"))
              return
       }

       w.Write([]byte(`{"error":"success"}`))

}

func userGetHandler(w http.ResponseWriter, r *http.Request) {

       type Userstruct struct {
              Username string
       }

       //make byte array
       out := make([]byte,1024)

       //
       bodyLen, err := r.Body.Read(out)

       if err != io.EOF {
              fmt.Println(err.Error())
              w.Write([]byte(`{"error":"bodyRead"}`))
              return
       }

       var k Userstruct

       //err = json.Unmarshal(out[:bodyLen],&k)

       err = xml.Unmarshal(out[:bodyLen],&k)

       if err != nil {
              w.Write([]byte(err.Error()))
              return
       }

       err = getFromdatabase(k.Username, w)

       if err != nil {
              w.Write([]byte("{error:" + err.Error() + "}"))
              return
       }


}



func StartService(port string) {

       http.HandleFunc("/adduser", userAddHandler)
       http.HandleFunc("/getuser", userGetHandler)
       http.ListenAndServe(":" + port, nil)
}

func main() {

       var err error
       appdatabase, err = sql.Open("mysql", "root:asdf1234@/employee")

       if err != nil {
              panic(err.Error())
       }
       err = appdatabase.Ping()
       if err != nil {
              fmt.Println(err.Error())
       }

       StartService("8090")
}

In place of Golang json encoder, I am using xml encoder and rest is same in complete code.

For testing I am again using Advance REST client.

Image of my test can be seen below:

This is for “adduser” functionality.

No we are sending XML raw payload.

capture

This is for “getuser” functionality.

No we are sending XML raw payload and getting XML response.

capture

Have a look, it’s so easy in Golang to change the APIs, whether it’s XML or JSON. You can be a rock star in just few lines of code.

So, this one ends here.

See You Soon.

Happy Coding.

Love,

Rajni

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s