Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

import/path: issue title #69075

Closed
dataflowjs opened this issue Aug 26, 2024 · 5 comments
Closed

import/path: issue title #69075

dataflowjs opened this issue Aug 26, 2024 · 5 comments

Comments

@dataflowjs
Copy link

dataflowjs commented Aug 26, 2024

Go version

go version go1.22.5 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\a-cod\AppData\Local\go-build
set GOENV=C:\Users\a-cod\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=E:\w\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\w\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.5
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=E:\w\go\src\github.com\imbaMF\gscrm\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\a-cod\AppData\Local\Temp\go-build124280173=/tmp/go-build -gno-record-gcc-switches

What did you do?

im trying to understand, what the difference?
im do a request in python

import requests

url = "http://159.223.250.48/bonus/promo"
data = {"body": {"code": "wolu24c"}}
headers = {
    "Host": "lb-api-rt.com",
    "Content-Type": "application/json",
    "Accept": "application/json",
    "SessionId": "sqeh0fo8c4iju00p6tii6nnnq3",
}
response = requests.post(url, json=data, headers=headers)
print(response.status_code, response.text)

and python request working well as exected
the output

400 {"code":1,"message":"The promo code \u0022wolu24c\u0022 has already been taken once","i18n":"api.bonus.promo.already_taken","args":{"@code":"wolu24c","@player_id":"5552822"}}

then i do golang code:

package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
)

func main() {
	url := "http://159.223.250.48/bonus/promo"

	var jsonStr = []byte(`{"body":{"code": "wolu24c"}}`)
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	// Set headers
	req.Header.Set("Host", "lb-api-rt.com")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json")
	req.Header.Set("SessionId", "sqeh0fo8c4iju00p6tii6nnnq3")

	// Create an HTTP client and send the request
	client := http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()

	// Print the response status and body
	fmt.Println("Status code:", resp.StatusCode)

	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
	}
	log.Println(string(b))

}

and i got the response:

Status code: 404
2024/08/26 22:54:51 <html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

i trying to debug it and spend to much to to understand what the difference and after hours i understand that the problem is in:
this line

req.Header.Set("Host", "lb-api-rt.com")

when i change this line to:

req.Host = "lb-api-rt.com"

The request works as expected...
Why default http package have a different behaviours in this lines?
req.Header.Set("Host", "lb-api-rt.com")
req.Host = "lb-api-rt.com"

What did you see happen?

Status code: 404
2024/08/26 22:54:51

<title>404 Not Found</title>

404 Not Found


nginx/1.18.0

What did you expect to see?

{"code":1,"message":"The promo code \u0022wolu24c\u0022 has already been taken once","i18n":"api.bonus.promo.already_taken","args":{"@code":"wolu24c","@player_id":"5552822"}}

@ianlancetaylor
Copy link
Contributor

Unlike many projects Go does not use the issue tracker for questions. Please see https://go.dev/wiki/Questions. Thanks.

For the difference between setting the Host header and the Host field please see https://pkg.go.dev/net/http#Request.Host.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Aug 26, 2024
@dataflowjs
Copy link
Author

when i set

req.Header.Set("Host", "lb-api-rt.com")

I expect that the host value will be the some (correct value)
becouse i set this value! but under the hood the Header.Set change this value to

Request Dump:
POST /bonus/promo HTTP/1.1
Host: 159.223.250.48
User-Agent: python-requests/2.28.2
Content-Length: 29
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Type: application/json
Sessionid: sqeh0fo8c4iju00p6tii6nnnq3

so its not expected behovior
the value must be the some as i set

@dataflowjs
Copy link
Author

was debug with

requestDump, err := httputil.DumpRequestOut(req, true)
	if err != nil {
		fmt.Println("Error dumping request:", err)
	}
	fmt.Println("Request Dump:\n", string(requestDump))

@ianlancetaylor
Copy link
Contributor

You will get better and faster answers if you use a forum. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

13 participants
@ianlancetaylor @dataflowjs @gabyhelp and others