1

i have default body raw json and want to paste it into a struct so it can fetch data automatically and save it into a struct

Body Raw Json

 {
    "jsonrpc": "2.0",
    "params": {
    }
}

Response from api

{
"jsonrpc": "2.0",
"id": null,
"result": {
    "status": 200,
    "response": [
        {
            "service_id": 1129,
            "service_name": "Adobe Illustrator",
            "service_category_id": 28,
            "service_category_name": "License Software",
            "service_type_id": 25,
            "service_type_name": "Software",
            "create_date": "2020-03-09 03:47:44"
        },
],
    "message": "Done All User Returned"
}

}

I want to put it in the repository file so I can get data automatically

Repo file

// Get request
resp, err := http.Get("look at API Response Example")
if err != nil {
    fmt.Println("No response from request")
}

defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) // response body is []byte
if err != nil {
    return err
}

// data that already fetch accomadate to struct  
var result models.OdooRequest
if err := json.Unmarshal(body, &result); err != nil {  
    fmt.Println("Can not unmarshal JSON")
}

for _, rec := range result.Response {
    fmt.Println(rec.ServiceName)
}
return err

after being fetched then accommodated into a struct

struct

type OdooRequest struct {
    Response []UpsertFromOdooServices
}
4
  • 2
    What problems are you having implementing the stated functionality? What are the errors you encountered?
    – mkopriva
    Dec 17, 2021 at 14:10
  • how to add body raw json to repository @mkopriva Dec 17, 2021 at 14:29
  • 1
    What do you mean by "so I can get data automatically" ? Dec 17, 2021 at 14:41
  • @ManjeetThakur The body json will be added to the repository, and it will automatically get the data from other API Dec 17, 2021 at 14:47

1 Answer 1

0

Sure, here's a rough way to make that request and read the response:

package main

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

type OdooRequest struct {
    Result struct {
        Status   int `json:"status"`
        Response []struct {
            ServiceID           int    `json:"service_id"`
            ServiceName         string `json:"service_name"`
            ServiceCategoryID   int    `json:"service_category_id"`
            ServiceCategoryName string `json:"service_category_name"`
            ServiceTypeID       int    `json:"service_type_id"`
            ServiceTypeName     string `json:"service_type_name"`
            CreateDate          string `json:"create_date"`
        } `json:"response"`
        Message string `json:"message"`
    } `json:"result"`
}

func main() {
    if err := run(); err != nil {
        panic(err)
    }
}
func run() error {
    resp, err := http.Post(
        "ADD_URL_HERE",
        "application/json",
        bytes.NewBufferString(`{"jsonrpc": "2.0","params": {}}`),
    )
    if err != nil {
        return err
    }
    defer resp.Body.Close()
    var odooResp OdooRequest
    if err := json.NewDecoder(resp.Body).Decode(&odooResp); err != nil {
        return err
    }
    for _, rec := range odooResp.Result.Response {
        fmt.Println(rec.ServiceName)
    }
    return nil
}

1
  • thank you so much, but "println" don't appear? Dec 18, 2021 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.