zoukankan      html  css  js  c++  java
  • golang-http-post

    func httpPost() {
        resp, err := http.Post("https://www.abcd123.top/api/v1/login",
            "application/x-www-form-urlencoded",
            strings.NewReader("username=test&password=ab123123"))
        if err != nil {
            fmt.Println(err)
        }
    
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            // handle error
        }
    
        fmt.Println(string(body))
    }
    
    func httpPostForm() {
        resp, err := http.PostForm("https://www.denlery.top/api/v1/login",
            url.Values{"username": {"auto"}, "password": {"auto123123"}})
        if err != nil {
            // handle error
        }
        defer resp.Body.Close()
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            // handle error
        }
        fmt.Println(string(body))
    
    }
    
    func httpPostJson() {
        jsonStr :=[]byte(`{ "username": "auto", "password": "auto123123" }`)
        url:= "https://www.denlery.top/api/v1/login"
        req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
        req.Header.Set("Content-Type", "application/json")
    
        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {
            // handle error
        }
        defer resp.Body.Close()
    
        statuscode := resp.StatusCode
        hea := resp.Header
        body, _ := ioutil.ReadAll(resp.Body)
        fmt.Println(string(body))
        fmt.Println(statuscode)
        fmt.Println(hea)
    
    }
     
  • 相关阅读:
    【uTenux实验】事件标志
    【uTenux实验】信号量
    【uTenux实验】任务管理
    【uTenux实验】写在开始实验之前
    Git撤销add、commit
    vim笔记
    Git使用方法(精心整理,绝对够用)
    git笔记三
    git笔记记录
    git笔记
  • 原文地址:https://www.cnblogs.com/lavin/p/10595124.html
Copyright © 2011-2022 走看看