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

How can I set a HOST for request? #154

Closed
mail2fish opened this issue Jun 1, 2018 · 5 comments
Closed

How can I set a HOST for request? #154

mail2fish opened this issue Jun 1, 2018 · 5 comments
Assignees

Comments

@mail2fish
Copy link

There is a vhost server IP. We like to access it with a host name which like www.example.com.

Usually we can set a Host header in request, but it didn't work for golang http request.

So could you add a special method for the matter?

@jeevatkm jeevatkm self-assigned this Jun 1, 2018
@jeevatkm
Copy link
Member

jeevatkm commented Jun 1, 2018

@mail2fish This is known issue in the Go http client golang/go/issues/7682 however there is a workaround.

Use resty.SetPreRequestHook() to set Host value.

For example:

resty.SetPreRequestHook(func(c *resty.Client, r *resty.Request) error {
	r.RawRequest.Host = "www.example.com"
	return nil
})

Can you try and please let me know?

@jeevatkm
Copy link
Member

jeevatkm commented Jun 2, 2018

Or via classic approach is using hosts file mapping of target-ip with host address. Then make with requests with mapped address.

@mail2fish
Copy link
Author

@jeevatkm thanks for your answer. That's worked. But I think that is convenient for developers if there is a shortcut for this matter. In my situation, I developed in the localhost, and don't want to change my hosts file because I have to access that host by a browser.

Thanks again, That hook have solved my problem.

@jeevatkm
Copy link
Member

jeevatkm commented Jun 4, 2018

@mail2fish thanks for confirming, I have created an issue to add built-in support for Host value. Coming in next release.

@jeevatkm
Copy link
Member

jeevatkm commented Jun 5, 2018

@sorribas just did a PR for #155, its coming in v1.7 release 😄

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

No branches or pull requests

2 participants