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RwSZZmNXzd0S4XY3oV19DOiyrLl3qBxhgw33pLYla5dIAs_KnrGVmLSpNJT61T810kqN-dT1c4EFMZ1iz7Bfh4RucK4oHGZmPlR3znqMMijirK8QT7ukTJvhG-TlmfsBMe0r_9UjdQ9wxjZ7pDi7pfYKVOv55O9Lzk2PbAjIHe3UKiA",
"not-before-policy": 1549293916,
"session_state": "66b3d62d-b3c6-42bb-8679-f0ded0e78957",
"scope": "openid email profile"
}
The struct I am using is as follow:
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry time.Time `json:"expires_in"`
}
After decoding the response, I tried printing the response :
var authToken Token
json.Unmarshal(response.Body, &authToken)
fmt.Println("-------------------- accessToken " + authToken.AccessToken)
fmt.Println("-------------------- refreshToken " + authToken.RefreshToken)
fmt.Println("-------------------- expires ", authToken.Expiry)
fmt.Println("-------------------- type " + authToken.TokenType)
Only the first one AccessToken has a value, everything else is empty.
I also tried using json.NewDecoder(response.Body).Decode(&authToken)
Same result.
Is there anything wrong with my approach ?
You have to check error from json.Unmarshal or from decoder.Decode.
And "expires_in": 299, it is not time, it is int.
code:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
type Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry int `json:"expires_in"`
}
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var authToken Token
if err := decoder.Decode(&authToken); err != nil {
fmt.Fprintf(w, "error: %s", err)
return
}
fmt.Println("-------------------- accessToken " + authToken.AccessToken)
fmt.Println("-------------------- refreshToken " + authToken.RefreshToken)
fmt.Println("-------------------- expires ", authToken.Expiry)
fmt.Println("-------------------- type " + authToken.TokenType)
})
http.ListenAndServe(":8000", nil)
}
curl:
curl -XPOST 'http://localhost:8000' -H 'Content-Type: application/json' -d '{
"access_token": "at",
"refresh_token": "rt",
"expires_in": 299,
"token_type": "bearer"
}'
result:
-------------------- accessToken at
-------------------- refreshToken rt
-------------------- expires 299
-------------------- type bearer
Related
Using a post API request I want to receive a JWT token for the response.
For that, I want to send the username and password as JSON body input to the API. The username and password should be taken dynamically from the user as a JSON payload.
Currently, I am hardcoding the user name and password using strings.NewReader which is not suitable as this is a web application and many users will be using it.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/joho/godotenv"
)
var authToken string
type authorization struct {
AuthorizationToken string `json:"authorizationToken"`
ExpiresTimestamp string `json:"expiresTimestamp"`
}
func main() {
enverr := godotenv.Load(".env")
if enverr != nil {
fmt.Println("Error loading .env file")
os.Exit(1)
}
token()
}
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.NewReader(`
{
"name" : "testuser",
"pwd" : "testpwd",
"hName" : "hname"
}
`)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
if err != nil {
panic(err)
}
defer response.Body.Close()
content, _ := ioutil.ReadAll(response.Body)
var result authorization
if err := json.Unmarshal(content, &result); err != nil { // Parse []byte to the go struct pointer
fmt.Println("Can not unmarshal JSON")
}
authToken := result.AuthorizationToken
fmt.Println(PrettyPrint(authToken))
}
func PrettyPrint(i interface{}) string {
s, _ := json.MarshalIndent(i, "", "\t")
return string(s)
}
You can use http requests body (for POST request)
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
c := Credentials{}
decoder := json.NewDecoder(r.Body)
defer r.Body.Close()
err: = decoder.Decode(&c)
if err!=nil{
// handle it
}
// use it
fmt.Println(c.Username, c.Password)
If you are intending to distribute the application as an executable (aka command line in the Unix world), you can either:
Provide the username and password as program arguments, os.Args (omitting unchanged code):
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", os.Args[2], -1), "USER", os.Args[1], -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}
You should then be able to run your program (once compiled): ./filename user password
Or as better alternative, having sensitive data involved, have the username and password input from standard input with password being echoed (hiding the characters):
import (
// ...
"syscall"
"golang.org/x/term"
)
var inputTemplate = `
{
"name" : "USER",
"pwd" : "PWD",
"hName" : "hname"
}
`
func token() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter username: ")
username := reader.ReadString('\n') // handle error
fmt.Print("Enter password: ")
pwdbytes := term.ReadPassword(int(syscall.Stdin))
password := string(pwdbytes)
authorizationUrl := os.Getenv("authorizationUrl")
requestBody := strings.Replace(strings.Replace(inputTemplate, "PWD", password, -1), "USER", username, -1)
response, err := http.Post(authorizationUrl, "application/json", requestBody)
// ...
}
I am getting a json response from a server with three fix json object field and one field with different json objects.
Example - Response 1
{
status : "success",
error-code : "",
message : "login successful",
data : {
token : "token value",
refresh-token : "refresh token value"
}
}
Example - Response 2
{
status : "success",
error-code : "",
message : "user data fetched",
data : {
first-name: "josh",
last-name : "walter",
age : "30",
phone: 1234567890,
address : "x street, dc"
}
}
for above json response created struct as follows
type loginData struct {
Token string `json:"token"`
RefreshToken string `json:"refresh-token"`
}
type userdata {
FirstName string `json:"first-name"`
LastName string `json:"refresh-token"`
Age string `json:"age"`
Phone string `json:"phone"`
Address string `json:"address"`
}
type response struct {
Status string `json:"status"`
ErrorCode string `json:"error-code"`
RespMessage string `json:"message"`
RespData string `json:"data"`
}
How to add logindata struct while unmarshaling during login response and userdata struct while unmarshaling userdata response in "RespData" field in response struct
First change the RespData field's type:
type response struct {
Status string `json:"status"`
ErrorCode string `json:"error-code"`
RespMessage string `json:"message"`
RespData interface{} `json:"data"`
}
Then, depending on what request you are making, set the RespData field to a pre-allocated instance of a pointer to the expected type:
r, err := http.Get("https://some_api.com/loginData")
if err != nil {
return err
}
defer r.Body.Close()
// check r.StatusCode and make sure it's correct
data := loginData{}
resp := response{RespData: &data}
if err := json.NewDecoder(r.Body).Decode(&resp); err != nil {
return err
}
fmt.Println(data)
According to me, you should have two different structs to do this. One reason is separation of concern, as the structure of these responses may change in the future and these are responses from two different apis so its better to maintain different response objects.
type loginData struct {
Token string `json:"token"`
RefreshToken string `json:"refresh-token"`
}
type userdata struct {
FirstName string `json:"first-name"`
LastName string `json:"refresh-token"`
Age string `json:"age"`
Phone string `json:"phone"`
Address string `json:"address"`
}
type response struct {
Status string `json:"status"`
ErrorCode string `json:"error-code"`
RespMessage string `json:"message"`
}
type userResponse struct {
response
RespData userdata `json:"data"`
}
type loginResponse struct {
response
RespData loginData `json:"data"`
}
#mkopriva as you said tried this suggestion it worked.
package main
import (
"encoding/json"
"fmt"
"strings"
)
type loginData struct {
Token string `json:"token"`
RefreshToken string `json:"refresh-token"`
}
type userdata struct {
FirstName string `json:"first-name"`
LastName string `json:"last-name"`
Age string `json:"age"`
Phone string `json:"phone"`
Address string `json:"address"`
}
type response struct {
Status string `json:"status"`
ErrorCode string `json:"error-code"`
RespMessage string `json:"message"`
RespData interface{} `json:"data"`
}
func main() {
jsonresp_1 := `{
"status" : "success",
"error-code" : "",
"message" : "login successful",
"data" : {
"token" : "token value",
"refresh-token" : "refresh token value"
}
}`
jsonresp_2 := `{
"status" : "success",
"error-code" : "",
"message" : "user data fetched",
"data" : {
"first-name": "josh",
"last-name" : "walter",
"age" : "30",
"phone": "1234567890",
"address" : "x street, dc"
}
}`
resp1 := strings.NewReader(jsonresp_1)
data1 := loginData{}
respdata1 := response{RespData: &data1}
if err := json.NewDecoder(resp1).Decode(&respdata1); err != nil {
fmt.Println(err)
}
fmt.Printf("token : %s \nrefreshtoken : %s \n", data1.Token, data1.RefreshToken)
fmt.Println("-------------------")
resp2 := strings.NewReader(jsonresp_2)
data2 := userdata{}
respdata2 := response{RespData: &data2}
if err := json.NewDecoder(resp2).Decode(&respdata2); err != nil {
fmt.Println(err)
}
fmt.Printf("firstname : %s \nlastname : %s ", data2.FirstName, data2.LastName)
}
Output
⚡ ⇒ go run main.go
token : token value
refreshtoken : refresh token value
-------------------
firstname : josh
lastname : walter
I followed this guide Developing a simple CRUD API with Go, Gin and Gorm to build my first RESTful service in golang, I can read parameter(s) from my web service, and update one value of parameter, But how to parse the JSON to update several values of parameters at one PUT request.
The CURL command I intend to use for my test as bellow
$ curl -i -X PUT http://localhost:8080/params -d '{ [ {"id":"1","value": "10"}, {"id":"2","value": "20"}] }'
Following is my code
package main
import (
"fmt"
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
)
type Param struct {
ID int `json:"id" `
Name string `json:"name"`
Title string `json:"title"`
Value string `json:"value"`
}
type ParamValue struct {
ID int `json:"id" `
// Name string `json:"name"`
// Title string `json:"title"`
Value string `json:"value"`
}
var db *gorm.DB
var err error
func main() {
db, err = gorm.Open("mysql", "user:password#tcp(127.0.0.1:3306)/test?charset=utf8&parseTime=True&loc=Local")
defer db.Close()
if err != nil {
log.Panic(err.Error())
}
db.AutoMigrate(&Param{})
r := gin.Default()
r.GET("/params", GetParams)
r.GET("/params/:id", GetParam)
r.PUT("/params/:id", UpdateParam)
r.PUT("/params", UpdateParams) // How to implement this one
r.Use(cors.Default())
r.Run()
}
func GetParams(c *gin.Context) {
var params []Param
if err := db.Find(¶ms).Error; err != nil {
c.AbortWithStatus(404)
} else {
c.JSON(200, params)
}
}
func GetParam(c *gin.Context) {
id := c.Params.ByName("id")
var param Param
if err := db.Where("id = ?", id).First(¶m).Error; err != nil {
c.AbortWithStatus(404)
} else {
c.JSON(200, param)
}
}
func UpdateParams(c *gin.Context) {
// Debug info
fmt.Println("c.Request.Method >> " + c.Request.Method)
fmt.Println("c.Request.URL.String() >> " + c.Request.URL.String())
// command for testing
// $ curl -i -X PUT http://localhost:8080/params -d '{ [ {"id":"1","value": "10"}, {"id":"2","value": "20"}] }'
}
func UpdateParam(c *gin.Context) {
var param Param
id := c.Params.ByName("id")
if err := db.Where("id = ?", id).First(¶m).Error; err != nil {
c.AbortWithStatus(404)
} else {
name := param.Name // readonly
c.BindJSON(¶m)
if param.Name != name { // if changed
param.Name = name // restore the origin
}
db.Save(¶m)
c.JSON(200, param)
}
// $ curl -i -X PUT http://localhost:8080/params/63 -d '{ "name": "recharge", "title": "switch 0/1:on/off", "value": "1"}'
}
Any help is appreciated.
first of all, you json is invalid.
I guess that this is what you wanted:
[{"id":"1" ,"value": "10"}, {"id":"2" ,"value": "20"}]
an easier solution will be to add an struct that match your json:
and then add the json.Unmarshal to parse the json:
jsn := `[{"id":"1" ,"value": "10"}, {"id":"2" ,"value": "20"}]`
var data yourSturctName
json.Unmarshal([]byte(jsn), &data)
for _, r := range data {
fmt.Printf("ID:%s Value:%s", r.ID, r.Value)
}
then add that into your logic.
I'm trying to make a request to my (magento)webserver using golang.
I managed to make a POST request, but however, I'm not getting the same response I'm getting when using CURL from the cmd.
I followed this post (How to get JSON response in Golang) which does not give me any response.
So I tried this
package main
import (
"fmt"
"net/http"
"os"
)
var protocol = "http://"
var baseURL = protocol + "myserver.dev"
func main() {
fmt.Print("Start API Test on: " + baseURL + "\n")
r := getCart()
fmt.Print(r)
}
func getCart() *http.Response {
resp, err := http.Post(os.ExpandEnv(baseURL+"/index.php/rest/V1/guest-carts/"), "", nil)
if err != nil {
fmt.Print(err)
}
return resp
}
This just return the http reponse like
{200 OK 200 HTTP/1.1 1 1 map[Date:[Thu, 04 May 2017 05:30:20 GMT] Content-Type:[application/json; charset=utf-8] Set-Cookie:[PHPSESSID=elklkflekrlfekrlklkl; expires=Thu, 04-May-2017 ...
When using curl -g -X POST "my.dev/index.php/rest/V1/guest-carts/" I retrieve some kind of ID which I need to proceed.
How can I get the this curl result in golang?
You need to read the resp.Body (and don't forget to close it!), ie
func main() {
fmt.Print("Start API Test on: " + baseURL + "\n")
r := getCart()
defer r.Body.Close();
if _, err := io.Copy(os.Stdout, r.Body); err != nil {
fmt.Print(err)
}
}
So I tried something based on the example here in my code, and get no data, but no error either. The code is:
import (
"io"
"fmt"
"net/http"
"encoding/json"
)
type Credential struct {
username string `json:"username"`
password string `json:"password"`
}
func login(res http.ResponseWriter, req *http.Request) {
if req.Method == "POST" {
cred := Credential{}
err := json.NewDecoder(req.Body).Decode(&cred)
if err != nil {
panic("can't decode")
}
fmt.Println("credentials: " + cred.username + " , " + cred.password)
}
}
I test with
curl -X POST -H "Accept: application/json" --data "{\"username\":\"x\",\"password\":\"y\"}" 127.0.0.1:8000/login -i
And the server prints out:
credentials: ,
Why is there nothing in cred.username and cred.password?
golang use first character of the field to declare public or private for that struct. so change username to Username