案例中包含以下内容
- get请求
- get请求携带参数
- post请求携带参数
- post请求发送xml数据
- post请求发送单文件
- post请求发送多文件
- 自定义header
package main
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/net/ghttp"
"os"
"path"
)
import "fmt"
func main() {
//httpGet()
//httpGetParam()
//httpPost()
//httpPost2(
//httpPostXML()
//httpSendFile()
httpSendMultiFile()
}
// get请求
func httpGet(){
if response, err := ghttp.Get("http://127.0.0.1:5000/get"); err != nil {
panic(err)
} else {
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
// get请求带参数
func httpGetParam(){
if response, err := ghttp.Get("http://127.0.0.1:5000/get?name=admin&age=21"); err != nil {
panic(err)
} else {
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
// post请求
func httpPost(){
if response, err := ghttp.Post("http://127.0.0.1:5000/post", g.Map{"name": "admin"}); err != nil{
panic(err)
}else{
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
// post请求2
func httpPost2(){
// 创建http客户端对象
c := ghttp.NewClient()
// 默认请求头为application/json
// 设置请求头为application/x-www-form-urlencoded
c.SetHeader("Content-Type", "application/x-www-form-urlencoded")
if response, err := c.Post("http://127.0.0.1:5000/post", `{"id": 1, "name": "admin"}`); err != nil{
panic(err)
}else{
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
// post发送xml请求
func httpPostXML(){
c := ghttp.NewClient()
c.SetHeader("Content-Type", "application/xml")
xmlContent := `
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>`
if response, err := c.Post("http://127.0.0.1:5000/data", xmlContent); err != nil {
panic(err)
} else {
defer response.Close()
fmt.Println( response.ReadAllString() )
}
}
// post请求发送文件-单文件
func httpSendFile() {
// 获取当前文件夹
filePath, err:= os.Getwd()
if err != nil {
panic(err)
}
// 拼接文件
f := path.Join(filePath, "abc.txt")
if response, err := ghttp.Post("http://127.0.0.1:5000/file", `upload_file1=@file:`+f); err != nil {
panic(err)
} else {
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
// post上传多个文件
func httpSendMultiFile(){
// 获取当前文件夹
filePath, err:= os.Getwd()
if err != nil {
panic(err)
}
// 拼接文件
f1 := path.Join(filePath, "abc.txt")
f2 := path.Join(filePath, "def.txt")
if response, err := ghttp.Post("http://127.0.0.1:5000/file",
fmt.Sprintf("file1=@file:%s&file2=@file:%s", f1, f2),
); err != nil {
panic(err)
} else {
defer response.Close()
fmt.Println(response.ReadAllString())
}
}
Server端使用python flask框架
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/get')
def httpGet():
print(request.headers)
print('args->', request.args)
return 'ok'
@app.route('/post', methods=['POST'])
def httpPost():
print(request.headers)
print('form->', request.form)
return 'ok'
@app.route('/data', methods=['GET', 'POST'])
def httpData():
print(request.headers)
print('data->', request.data)
return 'ok'
@app.route('/file', methods=['POST'])
def httpFile():
print(request.headers)
print('file', request.files)
return 'ok'
@app.route('/to_struct', methods=['POST'])
def httpToStruct():
return jsonify({'name': 'admin', 'age': 23})
if __name__ == '__main__':
app.run(debug=True)
网友评论