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)) }

  

最新文章

  1. gcc编译命令行依赖库的指定顺序
  2. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之摄像机介绍Cameras
  3. IAR for msp430 MDK中 warning: #223-D: function &quot;xxx&quot; declared implicitly 解决方法
  4. [bzoj 1004][HNOI 2008]Cards(Burnside引理+DP)
  5. 第四十六课:MVC和MVVM的开发区别
  6. linux下proc里关于磁盘性能的参数
  7. PHP学习——数据类型
  8. js 去掉浏览器右击默认事件
  9. html5 拖放---(二)转
  10. win32贪吃蛇实现
  11. SharePoint2013 列表栏设置
  12. Unity shader学习之屏幕后期处理效果之高度雾,重建world pos方法2
  13. python 实现网页 自动登录
  14. vue系列之vue-resource
  15. idea上传项目到github出现&quot;remote with selected name already exists&quot;情况的解决
  16. C++ ------ const迭代器 和 const_iterator的区别
  17. Netty Message RefCount
  18. Python学习记录day8
  19. KMP Demo
  20. Vue-Module

热门文章

  1. vuex使用 实现点击按钮进行加减
  2. 初识STM32固件库
  3. UNION types numeric and text cannot be matched
  4. C语言 &#183; 删除数组中的0元素
  5. am335x 无屏实现开关机程序
  6. gch文件学习
  7. 1.1.18 zabbix监控NFS
  8. 【BZOJ】1688: [Usaco2005 Open]Disease Manangement 疾病管理(状压dp)
  9. mysql插入多行数据
  10. Tomcat6连接数设置:permsize