【五、http】go的http的信息提交(表单,json,上传文件)

一、post提交的几种

  • form表单
  • json
  • 文件

1、提交表单

cpp 复制代码
//http的post

func requstPost(){
	params := make(url.Values)
	params.Set("name", "kaiyue")
	params.Set("age", "18")

	formDataStr := []byte(params.Encode())
	formDataByte := bytes.NewBuffer(formDataStr)

	requst, err := http.NewRequest(http.MethodPost, "http://httpbin.org/post", formDataByte)
	if err != nil {
		fmt.Println("ss")
	}
	requst.URL.RawQuery = params.Encode()

	r, err := http.DefaultClient.Do(requst)
	if err != nil {
		fmt.Println("ss")
	}
	defer r.Body.Close()
	printBody(r)
}

2、提交json

cpp 复制代码
func postJson(){
	u := struct {
		Name string `json:"name"`
		Age int `json:"age"`
	}{
		Name: "kaiyue",
		Age: 18,
	}
	payload, _ := json.Marshal(u)
	r, _ := http.Post(
		"http://httpbin.org/post",
		"application/x-www-form-urlencoded",
		bytes.NewReader(payload),
	)
	defer r.Body.Close()

	content, _ := io.ReadAll(r.Body)
	fmt.Printf("%s\n", content)
}

func main() {
	postJson()
}

结果:

cpp 复制代码
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "{\"name\":\"kaiyue\",\"age\":18}": ""
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "26", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/1.1", 
    "X-Amzn-Trace-Id": "Root=1-6544fb3a-777a0c6563dee4ad74037aeb"
  }, 
  "json": null, 
  "origin": "120.244.60.192", 
  "url": "http://httpbin.org/post"
}

3、提交文件

cpp 复制代码
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"strings"
)

func postForm() {
	// form data 形式 query string,类似于 name=poloxue&age=18
	data := make(url.Values)
	data.Add("name", "poloxue")
	data.Add("age", "18")
	payload := data.Encode()

	r, _ := http.Post(
		"http://httpbin.org/post",
		"application/x-www-form-urlencoded",
		strings.NewReader(payload),
	)
	defer func() { _ = r.Body.Close() }()

	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func postJson() {
	u := struct {
		Name string `json:"name"`
		Age  int    `json:"age"`
	}{
		Name: "poloxue",
		Age:  18,
	}
	payload, _ := json.Marshal(u)
	r, _ := http.Post(
		"http://httpbin.org/post",
		"application/json",
		bytes.NewReader(payload),
	)
	defer func() { _ = r.Body.Close() }()

	content, _ := ioutil.ReadAll(r.Body)
	fmt.Printf("%s", content)
}

func postFile() {
	body := &bytes.Buffer{}

	writer := multipart.NewWriter(body)
	_ = writer.WriteField("words", "123")

	// 一个是输入表单的 name,一个上传的文件名称
	upload1Writer, _ := writer.CreateFormFile("uploadfile1", "uploadfile1")

	uploadFile1, _ := os.Open("uploadfile1")
	defer func() {_ = uploadFile1.Close()}()

	_, _ = io.Copy(upload1Writer, uploadFile1)

	// 一个是输入表单的 name,一个上传的文件名称
	upload2Writer, _ := writer.CreateFormFile("uploadfile2", "uploadfile2")

	uploadFile2, _ := os.Open("uploadfile2")
	defer func() {_ = uploadFile2.Close()}()

	_, _ = io.Copy(upload2Writer, uploadFile2)

	_ = writer.Close()

	fmt.Println(writer.FormDataContentType())
	fmt.Println(body.String())
	r, _ := http.Post("http://httpbin.org/post",
		writer.FormDataContentType(),
		body,
	)
	defer func() {_ = r.Body.Close()}()

	content, _ := ioutil.ReadAll(r.Body)

	fmt.Printf("%s", content)
}

func main() {
	// post 请求的本质,它是 request body 提交,相对于 get 请求(urlencoded 提交查询参数, 提交内容有大小限制,好像 2kb)
	// post 不同的形式也就是 body 的格式不同
	// post form 表单,body 就是 urlencoded 的形式,比如 name=poloxue&age=18
	// post json,提交的 json 格式
	// post 文件,其实也是组织 body 数据
	// postJson()
	postFile()
}

其中

uploadfile1 中的内容为 abc

uploadfile2 中的内容为 abc

结果:

cpp 复制代码
multipart/form-data; boundary=a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2
--a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2
Content-Disposition: form-data; name="words"

123
--a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2
Content-Disposition: form-data; name="uploadfile1"; filename="uploadfile1"
Content-Type: application/octet-stream

abc

--a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2
Content-Disposition: form-data; name="uploadfile2"; filename="uploadfile2"
Content-Type: application/octet-stream

abc

--a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2--

{
  "args": {}, 
  "data": "", 
  "files": {
    "uploadfile1": "abc\n", 
    "uploadfile2": "abc\n"
  }, 
  "form": {
    "words": "123"
  }, 
  "headers": {
    "Accept-Encoding": "gzip", 
    "Content-Length": "559", 
    "Content-Type": "multipart/form-data; boundary=a7c095e326382b46f363a6fa2d579ea3c02245a6a8368969a37ddfb50dd2", 
    "Host": "httpbin.org", 
    "User-Agent": "Go-http-client/1.1", 
    "X-Amzn-Trace-Id": "Root=1-6544fc1a-3104d8a606a4510d6a68fcde"
  }, 
  "json": null, 
  "origin": "120.244.60.192", 
  "url": "http://httpbin.org/post"
}
相关推荐
m0_748238275 分钟前
WebClient HTTP 请求问题处理模板(泛型响应、忽略 SSL 证书等)
网络协议·http·ssl
我曾经是个程序员15 分钟前
鸿蒙学习记录之http网络请求
服务器·学习·http
轩辰~1 小时前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
EasyDSS5 小时前
国标GB28181-2022平台EasyGBS:安防监控中P2P的穿透方法
网络协议·php·音视频·p2p
网安墨雨5 小时前
常用网络协议
网络·网络协议
ZoeLandia7 小时前
WebSocket | 背景 概念 原理 使用 优缺点及适用场景
网络·websocket·网络协议
Crossoads11 小时前
【汇编语言】端口 —— 「从端口到时间:一文了解CMOS RAM与汇编指令的交汇」
android·java·汇编·深度学习·网络协议·机器学习·汇编语言
诸葛悠闲11 小时前
SOME/IP 协议详解——信息格式
网络协议
gywl14 小时前
openEuler VM虚拟机操作(期末考试)
linux·服务器·网络·windows·http·centos
某柚啊15 小时前
Windows开启IIS后依然出现http error 503.the service is unavailable
windows·http