dshmkgq558192365 2019-02-09 13:29
浏览 87
已采纳

打印POST JSON数据

I have an issue printing the JSON from the POST. I use MUX for routing

r := mux.NewRouter()
r.HandleFunc("/test", Point).Methods("POST")
http.ListenAndServe(":80", r)`

and in Point function I have

func Point(w http.ResponseWriter, r *http.Request) {
    var callback Decoder
    json.NewDecoder(r.Body).Decode(&callback)
}

But I can use this method only when I know the structure and I want to figure out how to log.Print the whole JSON as a string. I tried

func Point(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    log.Println(r.Form)
} 

But it print empty map. PLease help to figure out this.

  • 写回答

2条回答 默认 最新

  • doudeng3008 2019-02-09 16:19
    关注

    In the assumption that you are building a standard API endpoint which receives some JSON and you would like to do something with it you should approach it the following way.

    Edit:

    As mentioned in the comments when you use the ioutil.ReadAll() function as in this example it will read everything that is sent in the post request into the application memory. It's a good idea to check this in a production application (e.g limiting payload size).

    1.) Make a struct which will hold the data coming from an API post request in GoLang
    2.) Convert your request body into a byte array []byte
    3.) Unmarshal your []byte into a single instance of your struct made earlier.

    I'll put an example below:

    1. Make a struct which you'll put your JSON into.


    Let's take an example of a simple blog post.

    The JSON object looks like this and has a slug, a title, and description

    {
      "slug": "test-slug",
      "title": "This is the title",
      "body": "This is the body of the page"
    }
    

    So your struct would look like this:

    type Page struct {
        Slug  string `json:"slug"`
        Title string `json:"title"`
        Body  string `json:"body"`
    }
    

    2 - 3. Get the body of your request and convert it to byte[] Then take that string and Unmarshal it into an instance of your struct.


    The data of a post request is the request 'Body'.

    In Golang the request in almost all cases (unless you're using something fancy outside of the defaults) will be an http.Request object. This is the 'r' which you normally have in your normal code and it holds the 'Body' of our POST request.

    import (
        "encoding/json"
        "github.com/go-chi/chi" // you can remove
        "github.com/go-chi/render" // you can remove but be sure to remove where it is used as well below.
        "io/ioutil"
        "net/http"
    )
    
    
    func GiveMeAPage(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("A page"))
    }
    

    So what we are going to do here is convert an io.ReadCloser, which is what the http.Request.Body is, to a []byte as the Unmarshal function takes a []byte type. I've commented inline below for you.

    func Create(w http.ResponseWriter, r *http.Request) {
        var p Page //Create an instance of our struct
    
    
        //Read all the data in r.Body from a byte[], convert it to a string, and assign store it in 's'.
        s, err := ioutil.ReadAll(r.Body) 
        if err != nil {
            panic(err) // This would normally be a normal Error http response but I've put this here so it's easy for you to test.
        }
        // use the built in Unmarshal function to put the string we got above into the empty page we created at the top.  Notice the &p.  The & is important, if you don't understand it go and do the 'Tour of Go' again.
        err = json.Unmarshal(s, &p)
        if err != nil {
            panic(err) // This would normally be a normal Error http response but I've put this here so it's easy for you to test.
        }
    
       // From here you have a proper Page object which is filled.  Do what you want with it.
    
        render.JSON(w, r, p) // This is me using a useful helper function from go-chi which 'Marshals' a struct to a json string and returns it to using the http.ResponseWriter.
    }
    

    As a side note. Please don't use Decoder to parse JSON unless you are using JSON streams. You are not here, and it's unlikely you will for a while. You can read about why that is here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器