[英]blank http response body using golang
我正在嘗試從谷歌 api 獲取信息,但響應正文似乎為空。 它只是將{}
輸出到控制台。 不確定我哪里出錯了,因為我使用文檔獲取請求的有效負載信息: https://developers.google.com/safe-browsing/v4/lookup-api
package main
import (
"fmt"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
)
type payload struct {
Client client `json:"client"`
ThreatInfo threatInfo `json:"threatInfo"`
}
type client struct {
ClientId string `json:"clientId"`
ClientVersion string `json:"clientVersion"`
}
type threatInfo struct {
ThreatTypes []string `json:"threatTypes"`
PlatformTypes []string `json:"platformTypes"`
ThreatEntryTypes []string `json:"threatEntryTypes"`
ThreatEntries []entry `json:"threatEntries"`
}
type entry struct {
URL string `json:"url"`
}
func checkURLs(urls []string) {
// populate entries
var entries = []entry{}
for _, url := range urls {
entries = append(entries, entry{URL: url})
}
data := payload {
Client: client{
ClientId: "myapp",
ClientVersion: "0.0.1",
},
ThreatInfo: threatInfo{
ThreatTypes: []string{"MALWARE", "SOCIAL_ENGINEERING"},
PlatformTypes: []string{"WINDOWS"},
ThreatEntryTypes: []string{"URL"},
ThreatEntries: entries,
},
}
jsonBytes, _ := json.Marshal(data)
key := "*"
api := fmt.Sprintf("https://safebrowsing.googleapis.com/v4/threatMatches:find?key=%s", key)
req, _ := http.NewRequest("POST", api, strings.NewReader(string(jsonBytes)))
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(res) // 200 OK
fmt.Println(err) // nil
fmt.Println(string(body)) // {}
}
func main() {
checkURLs([]string{"http://www.urltocheck1.org/", "http://www.urltocheck2.org/"})
}
我認為這是在文檔中說明的
注意:如果沒有匹配項(即,如果在請求中指定的任何列表中都找不到請求中指定的 URL),則 HTTP POST 響應僅在響應正文中返回一個空的 object。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.