anaskhan96 / soup

Web Scraper in Go, similar to BeautifulSoup
MIT License
2.18k stars 168 forks source link

Custom headers in Get() request #11

Closed danilopolani closed 7 years ago

danilopolani commented 7 years ago

Fix feature #9.

There are two ways to define a custom header:

  1. Via the Header() function (suggested if you have to add a few headers);
  2. "Massive" add via the Headers variable which is an associative array (map[string]string).

As always the README needs an edit.

Examples

Note: all the examples are generated printing the headers received in a PHP page.

No headers

Code

source, _ := soup.Get("http://localhost/headers.php")
fmt.Println(source)

Output No headers

Headers via Header function

Code

soup.Header("Authorization", "Bearer foobar")
source, _ := soup.Get("http://localhost/headers.php")
fmt.Println(source)

Output Header function

Headers via Headers variable

Code

soup.Headers = map[string]string{
    "Authorization": "Bearer foobar",
    "Content-Type": "application/json",
}

source, _ := soup.Get("http://localhost/headers.php")
fmt.Println(source)

Output Headers variable

danilopolani commented 7 years ago

@anaskhan96 any news?

anaskhan96 commented 7 years ago

@danilopolani Sorry I've been busy the last few days. This looks good at first glance. I'll be reviewing this sometime later and revert back to you.

anaskhan96 commented 7 years ago

This looks good. Make the necessary changes in the README too and it should be ready to merge.