http请求及json发送与解析 post string
2024-08-25 19:54:20
golang http请求及json流解析 - 长风v持成的博客 - CSDN博客 https://blog.csdn.net/u011677067/article/details/80852158
package main import (
"encoding/json"
"fmt"
//"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
) const IssueURL = "https://api.github.com/search/issues" type IssuesSearchResult struct {
TotalCount int `json:"total_count"`
Items []*Issue
} type Issue struct {
Number int
HTMLURL string `json:"html_url"`
Title string
State string
User *User
CreateAt time.Time `json:"create_at"`
Body string
} type User struct {
Login string
HTMLURL string `json:"html_url"`
} func SearchIssues(terms []string) (*IssuesSearchResult, error) {
q := url.QueryEscape(strings.Join(terms, " "))
urls := IssueURL + "?q=" + q
resp, err := http.Get(urls)
if err != nil {
return nil, err
}
defer resp.Body.Close() fmt.Println("start encode ...") /*web, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
fmt.Printf("web:%s\n==============\n", web)
*/
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("search query failed: %s", resp.Status)
} var result IssuesSearchResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, err
}
return &result, nil
} func main() {
result, err := SearchIssues(os.Args[1:])
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d issue: \n", result.TotalCount) for _, item := range result.Items {
fmt.Printf("#<%-5d> <%9.9s> <%.55s>\n\n", item.Number, item.User.Login, item.Title)
}
}
go http 传递json数据 - 翔云 - CSDN博客 https://blog.csdn.net/lanyang123456/article/details/78702873
package main import (
"encoding/json"
"fmt"
"html"
"io/ioutil"
"log"
"net/http" ) type Cmd struct {
ReqType int
FileName string } func main() { http.HandleFunc("/bar", func (w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) if r.Method == "POST" {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Println("Read failed:", err)
}
defer r.Body.Close() cmd := &Cmd{}
err = json.Unmarshal(b, cmd)
if err != nil {
log.Println("json format error:", err)
} log.Println("cmd:", cmd)
} else { log.Println("ONly support Post")
fmt.Fprintf(w, "Only support post")
} }) log.Fatal(http.ListenAndServe(":8080", nil)) }
//post json
package main import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http" ) type Cmd struct { ReqType int
FileName string } func main() { url := "http://127.0.0.1:8080/bar"
contentType := "application/json;charset=utf-8" cmd := Cmd{ReqType: 12, FileName: "plugin"}
b ,err := json.Marshal(cmd)
if err != nil {
log.Println("json format error:", err)
return
} body := bytes.NewBuffer(b) resp, err := http.Post(url, contentType, body)
if err != nil {
log.Println("Post failed:", err)
return
} defer resp.Body.Close() content, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("Read failed:", err)
return
} log.Println("content:", string(content)) }
最新文章
- gcc编译命令行依赖库的指定顺序
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras
- IAR for msp430 MDK中 warning: #223-D: function ";xxx"; declared implicitly 解决方法
- [bzoj 1004][HNOI 2008]Cards(Burnside引理+DP)
- 第四十六课:MVC和MVVM的开发区别
- linux下proc里关于磁盘性能的参数
- PHP学习——数据类型
- js 去掉浏览器右击默认事件
- html5 拖放---(二)转
- win32贪吃蛇实现
- SharePoint2013 列表栏设置
- Unity shader学习之屏幕后期处理效果之高度雾,重建world pos方法2
- python 实现网页 自动登录
- vue系列之vue-resource
- idea上传项目到github出现";remote with selected name already exists";情况的解决
- C++ ------ const迭代器 和 const_iterator的区别
- Netty Message RefCount
- Python学习记录day8
- KMP Demo
- Vue-Module