Go Web Application Request body

スポンサーリンク

Request body

To send data from client, request body is one of the way.
For post request, body data is important.

For Go Web application, provide Body.Read to read body data

Example

params.go

package web

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

func ReadBody(w http.ResponseWriter, r *http.Request) {
	len := r.ContentLength
	body := make([]byte, len)
	r.Body.Read(body)
	fmt.Fprintln(w, string(body))
}

This program check content size and keep byte data with size.

And, Body.Read copies data to byte,

Last part is just shown data as string.

golang
スポンサーリンク
Professional Programmer2

コメント