bogdanfinn / tls-client

net/http.Client like HTTP Client with options to select specific client TLS Fingerprints to use for requests.
BSD 4-Clause "Original" or "Old" License
667 stars 133 forks source link

How can I add/change/delete session's headers #74

Closed seadhy closed 10 months ago

seadhy commented 10 months ago

As I wrote in the title, I wanna change the headers after defining session. Is this possible? This is my code:

package main

import (
    "fmt"
    "io"
    "log"
    "net/url"

    http "github.com/bogdanfinn/fhttp"
    tls_client "github.com/bogdanfinn/tls-client"
)

func CreateSession() tls_client.HttpClient {
    baseHeader := http.Header{
        "authority":          {"hcaptcha.com"},
        "accept":             {"application/json"},
        "accept-language":    {"en-US,en;q=0.9"},
        "content-length":     {"0"},
        "content-type":       {"text/plain"},
        "origin":             {"https://newassets.hcaptcha.com"},
        "referer":            {"https://newassets.hcaptcha.com/"},
        "sec-ch-ua":          {`"Chromium";v="112", "Not)A;Brand";v="24", "Google Chrome";v="112"`},
        "sec-ch-ua-mobile":   {"?0"},
        "sec-ch-ua-platform": {`"Windows"`},
        "sec-fetch-dest":     {"empty"},
        "sec-fetch-mode":     {"cors"},
        "sec-fetch-site":     {"same-site"},
        "user-agent":         {"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"},
        http.HeaderOrderKey: {
            "authority",
            "accept",
            "accept-language",
            "content-length",
            "content-type",
            "origin",
            "referer",
            "sec-ch-ua",
            "sec-ch-ua-mobile",
            "sec-ch-ua-platform",
            "content-length",
            "sec-fetch-dest",
            "sec-fetch-mode",
            "sec-fetch-site",
            "user-agent",
        },
    }

    options := []tls_client.HttpClientOption{
        tls_client.WithTimeoutSeconds(30),
        tls_client.WithClientProfile(tls_client.Chrome_112),
        tls_client.WithNotFollowRedirects(),
        tls_client.WithDefaultHeaders(baseHeader),
    }

    client, err := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
    if err != nil {
        log.Println(err)
    }

    return client

}
func main() {
    session := CreateSession()

    params := url.Values{}

    params.Add("v", "19148ad")
    params.Add("host", "accounts.hcaptcha.com")
    params.Add("sitekey", "a5f74b19-9e45-40e0-b45d-47ff91b7a6c2")
    params.Add("sc", "1")
    params.Add("swa", "1")
    params.Add("spst", "1")

    query := params.Encode()

    req, err := http.NewRequest(http.MethodPost, "https://httpbin.org/post?"+query, nil)

    if err != nil {
        log.Fatal(err)
        return
    }

    resp, err := session.Do(req)

    if err != nil {
        log.Fatal(err)
        return
    }

    defer resp.Body.Close()

    bodyText, err := io.ReadAll(resp.Body)

    if err != nil {
        log.Fatal(err)
        return
    }

    fmt.Printf("%s\n", bodyText)

}

For example, I wanna change only the content-type value, how can I do that?

bogdanfinn commented 10 months ago

@seadhy the default headers are for the case that no headers are provided on the request itself. you should provide headers on the request itself.

you can reuse your baseHeader and set it on the request. afterwards adjust the one header you need

be carful that there is no merge happening between default headers and request headers.