1. http encapsulate get, postJson class
package httpUtil import ( "bytes" logxx "go-gift-panel/src/log" "io" "io/ioutil" "net/http" ) func PostJson(url string, jsonData []byte) string { ("Requestedurl=", url, " RequestedJSON Data:", string(jsonData)) // Output data in JSON format resp, err := (url, "application/json", (jsonData)) // Initiate a POST request if err != nil { ("ERROR post Json request exception error:", err) return "e" } defer func(Body ) { err := () if err != nil { ("ERROR post Json request exception error:", err) } }() body, err := () // Read the response content if err != nil { ("ERROR postJson reads body exception err:", err) return "e" } ("Response Body:", string(body), " Response Status:", ) // Output response content return string(body) } func Get(url string) { ("Requestedurl=", url) // Output resp, err := (url) // Initiate a GET request if err != nil { ("ERROR Get request exception err:", err) return } defer func(Body ) { err := () if err != nil { ("ERROR Get request exception err:", err) } }() body, err := () // Read the response content if err != nil { ("ERROR Get Read Body Exception:", err) return } ("Response Body:", string(body), " Response Status:", ) // Output response content}
2. Log logxx class
package logxx import ( "/sirupsen/logrus" "/natefinch/lumberjack.v2" "io" "os" ) var Log = () // var file * func Init() { logFile := &{ Filename: "/data/giftpanel/logs/giftpanel_.log", MaxSize: 100, // megabytes single file size 100MB MaxBackups: 100, //Retain 100 files MaxAge: 60, //days reserved for 60 days Compress: false, // Whether to compress, it is convenient for querying and not compressing LocalTime: true, //true uses local time zone } // Set log level () // Set the output file name and line number (true) // Set the log format to JSON format (&{ TimestampFormat: "2006-01-02 15:04:05.000", // Set the time format, including milliseconds }) (logFile) // Set the output to the console and files simultaneously ((, logFile)) }
This is the article about using golang for http, get or postJson requests. For more related go request content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!