-1

I'm trying to get information from a google api, but the response body appears to be empty. it just outputs {} to the console. Not sure where I went wrong as I used the docs to get the payload information for the request: 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", "POTENTIALLY_HARMFUL_APPLICATION"},
            PlatformTypes: []string{"ANY_PLATFORM"},
            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/"})
}

EDIT

I found a go package by google to do most of the heavy lifting, and yet, still an empty response. I should add I managed to get my hands on some urls that do contain malware, and IS detected via googles transparency report url search: https://transparencyreport.google.com/safe-browsing/search

So why is it empty for me when there should be results?

package main

import (
    "fmt"
    "github.com/google/safebrowsing"
)

func checkURLs(urls []string) {
    sb, err := safebrowsing.NewSafeBrowser(safebrowsing.Config{
        ID: "myapp",
        Version: "0.0.1",
        APIKey: "*",
    })

    if err != nil {
        fmt.Println(err)
        return
    }

    threats, err := sb.LookupURLs(urls)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(threats)
}

func main() {
    checkURLs([]string{"http://www.urltocheck1.org/", "http://www.urltocheck2.org/"})
}
Steve Coulter
  • 61
  • 2
  • 9

1 Answers1

0

I think this was stated in the docs

Note: If there are no matches (that is, if none of the URLs specified in the request are found on any of the lists specified in a request), the HTTP POST response simply returns an empty object in the response body.

fahmifan
  • 21
  • 5
  • Thanks, I'm not sure how I missed that, but I'm managed to get my hands on some urls that do contain malware and it's still empty for me. Even though google themselves can detect it via their search... https://transparencyreport.google.com/safe-browsing/search – Steve Coulter Dec 02 '21 at 16:31