doubeishuai6598 2018-11-07 23:13
浏览 14
已采纳

如何使用Go向urlscanio发出发布请求?

What I'm doing is trying to submit a URL to scan to urlscan.io. I can do a search but have issues with submissions, particularly correctly sending the right headers/encoded data.

from their site on how to submit a url:

curl -X POST "https://urlscan.io/api/v1/scan/" \ -H "Content-Type: application/json" \ -H "API-Key: $apikey" \ -d "{\"url\": \"$url\", \"public\": \"on\"}"

This works to satisfy the Api key header requirement but

req.Header.Add("API-Key", authtoken)

This is my attempt that fails

data := make(url.Values)
data.Add("url", myurltoscan)

The URL property I have struggled with tremendously.

This is my error: "message": "Missing URL properties", "description": "The URL supplied was not OK, please specify it including the protocol, host and path (e.g. http://example.com/bar)", "status": 400

  • 写回答

1条回答 默认 最新

  • douben1891 2018-11-07 23:38
    关注

    url.Value is a map[string][]string containing values used in query parameters or POST form. You would need it if you were trying to do something like:

    curl -X GET https://urlscan.io/api/v1/scan?url=<urltoscan>
    

    or

    curl -X POST -F 'url=<urltoscan>' https://urlscan.io/api/v1/scan
    

    See documentation https://golang.org/pkg/net/url/#Values.

    To send a regular POST request with JSON data, you can encode the JSON as bytes and send with http.Post:

    var payload = []byte(`{"url":"<your-url>","public":"on"}`)
    req, err := http.NewRequest("POST", url, 
    bytes.NewBuffer(payload))
    req.Header.Set("API-Key", authtoken)
    req.Header.Set("Content-Type", "application/json")
    
    client := &http.Client{}
    resp, err := client.Do(req)
    if err != nil {
            panic(err)
    }
    defer resp.Body.Close()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?