Colly: Post method with headers

Created on 21 Dec 2018  路  3Comments  路  Source: gocolly/colly

Regard, Can anyone convert me this working code to Colly. Thank you

package main

import (
    "fmt"
    "net/http"
    "strings"
    "io/ioutil"
)

func main() {
    url := "http://1024game.org/api"
    payload := strings.NewReader(`term=4404094910003`)
    req, _ := http.NewRequest("POST", url, payload)
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    res, _ := http.DefaultClient.Do(req) 
    defer res.Body.Close()

    body, _ := ioutil.ReadAll(res.Body) 
    fmt.Println(string(body))
}

Most helpful comment

Can you please write how to read response?
Thank you

All 3 comments

You can try something like this:

package main

import (
    "net/http"
    "strings"

    "github.com/gocolly/colly"
)

func main() {
    c := colly.NewCollector()

    c.Request("POST",
        "http://1024game.org/api",
        strings.NewReader(`term=4404094910003`),
        nil,
        http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}
    )
}

Can you please write how to read response?
Thank you

@mishop

c := colly.NewCollector()

// here

c.OnResponse(func(r *colly.Response) {
    log.Println("resp", string(r.Body))
})


c.Request("POST",
    "http://1024game.org/api",
    strings.NewReader(`term=4404094910003`),
    nil,
    http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}
)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

peterhellberg picture peterhellberg  路  3Comments

mazhigali picture mazhigali  路  5Comments

x0rzkov picture x0rzkov  路  3Comments

prologic picture prologic  路  6Comments

icamys picture icamys  路  3Comments