2020-05-01 09:43:36 +08:00

98 lines
2.2 KiB
Go

// Copyright 2017-2020 The ShadowEditor Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
//
// For more information, please visit: https://github.com/tengge1/ShadowEditor
// You can also visit: https://gitee.com/tengge1/ShadowEditor
package helper
import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
// Get make a get request
func Get(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
// Post make a post request
func Post(url string, data url.Values) ([]byte, error) {
resp, err := http.PostForm(url, data)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
// Write write a string to http response
func Write(w http.ResponseWriter, args ...interface{}) {
header := w.Header()
header.Set("Content-Type", "text/plain")
header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "0")
w.Write([]byte(fmt.Sprint(args...)))
}
// Writef write a string to http response
func Writef(w http.ResponseWriter, format string, args ...interface{}) {
header := w.Header()
header.Set("Content-Type", "text/plain")
header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "0")
w.Write([]byte(fmt.Sprint(args...)))
}
// WriteJSON write a json to http response
func WriteJSON(w http.ResponseWriter, obj interface{}) error {
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "0")
bytes, err := ToJSON(obj)
if err != nil {
return err
}
w.Write(bytes)
return nil
}
// WriteBSON write a bson to http response
func WriteBSON(w http.ResponseWriter, obj interface{}) error {
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "0")
bytes, err := ToJSON(obj)
if err != nil {
return err
}
w.Write(bytes)
return nil
}