Make HTTP Request with an absolute URL in the Request URL

Issue

I was wondering if it’s possible to make a request as follows using Go’s net/http library:

GET http://test2.com/thisisatest HTTP/1.1
Host: test1.com

To clarify, if I’m sending a request to localhost I don’t want to change the host header as in:

nc -lnvp 8001
listening on [any] 8001 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 55122
GET /headers HTTP/1.1
Host: not-localhost
User-Agent: Go-http-client/1.1
Content-Type: application/json
Accept-Encoding: gzip

But what I intend is to have the full URL in the request line:

nc -lnvp 8001
listening on [any] 8001 ...
connect to [127.0.0.1] from (UNKNOWN) [127.0.0.1] 55122
GET http://localhost/headers HTTP/1.1
Host: localhost
User-Agent: Go-http-client/1.1
Content-Type: application/json
Accept-Encoding: gzip

I can’t seem to find any information about it. If it’s not possible using net/http is there any other way that this would be doable?

Thank you!

Diego

Solution

No, this us undoable with net/http because net/http implements HTTP and what you want to do simply isn’t HTTP.

If it’s not possible using net/http is there any other way that this would be doable?

Do raw networking, i.e. use package net.

This is wrong. Sorry.

Answered By – Volker

Answer Checked By – Senaida (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.