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

Allow setting of default base_url #215

Closed
nrocco opened this issue Apr 29, 2014 · 11 comments
Closed

Allow setting of default base_url #215

nrocco opened this issue Apr 29, 2014 · 11 comments
Labels
enhancement New feature or enhancement

Comments

@nrocco
Copy link

nrocco commented Apr 29, 2014

Hi there, first of all httpie is an excellent library. I recently switched to it after using resty for ages.

One of the nice features i am missing is a way to set a base url that will be used for subsequent http invocations.

I often find myself issuing multiple rest calls when testing/browsing an API and defining a base url saves me from a lot of typing.

For example, assume the following base url:

https://api.example.com/api/v1

When issuing the following command

http GET /users

Will do a GET request to the resource located at https://api.example.com/api/v1/users.

Currently I solve my impediment by applying the following patch to the httpie library

diff --git a/httpie/input.py b/httpie/input.py
index 8eb6a7d..e49079d 100644
--- a/httpie/input.py
+++ b/httpie/input.py
@@ -131,6 +131,8 @@ class Parser(ArgumentParser):
         self._parse_items()
         if not self.args.ignore_stdin and not env.stdin_isatty:
             self._body_from_file(self.env.stdin)
+        if 'HTTPIE_BASEURL' in os.environ:
+            self.args.url = os.environ['HTTPIE_BASEURL'].rstrip('/') + self.args.url
         if not (self.args.url.startswith((HTTP, HTTPS))):
             scheme = HTTP

This allows me to define a base url for the current running shell:

export HTTPIE_BASEURL='https://api.example.com/api/v1'
http GET /users
http GET /users/8292

I am curious what you think about this approach and maybe have a better idea.
I would be happy to contribute to this idea if needed.

Keep up the great work.

@jkbrzt jkbrzt added the feature label May 5, 2014
@smartboyathome
Copy link

I've been looking for something like this, but instead of working with a single environment, I have multiple that I can hit. As such, I'd like to do something like this:

http GET {{api}}/users  --environment=test
http GET {{api}}/users/8292  --environment=prod

I know this isn't necessarily the role of HTTPie, but it's a feature that otherwise I'd otherwise write a wrapper for. If anything, the ability to write a plugin to support this would be helpful.

@jkbrzt
Copy link
Member

jkbrzt commented May 6, 2014

Related feature request: #180

@nrocco
Copy link
Author

nrocco commented May 6, 2014

@jakubroztocil IMHO there is a difference between issue #180 or @smartboyathome suggestion and my proposal.

Sessions solve the problem of defining default parameters (headers, query string etc) which is a very nice feature, indeed. But as far as I know it does not solve the default base url problem.

I still think it would be nice to be able to define a default base url for the above mentioned reasons.

Instead of writing a wrapper around HTTPie (which works perfectly fine) I like to see how people think about including it as a feature in HTTPie. I am not sure if it should be the responsibility of HTTPie to offer such as feature.

@jkbrzt
Copy link
Member

jkbrzt commented May 6, 2014

@nrocco You are right, it's not actually related.

If this should be added, then I think it should be done by redefining/extending the similar and already existing :/foo localhost shortcut functionality.

Something like this:

  1. When the URL doesn't include any host, then the value of HTTPIE_BASEURL is prepended to it.
  2. The default value of HTTPIE_BASEURL is http://localhost.

So that:

$ http /foo   # => http://localhost/foo

$ export HTTPIE_BASEURL=http://example.org
$ http /foo   # => http://example.org/foo

The localhost shortcut also enables a port to be specified (e.g. :8000/foo). That should probably be made possible for the custom base url as well.

@jkbrzt jkbrzt added the decide label May 6, 2014
@nrocco
Copy link
Author

nrocco commented May 7, 2014

@jakubroztocil Thanks for your feedback. Allow me to prepare a PR and report back here.

If you agree with the implementation then fine, otherwise it can serve as a basis for a better implementation or at least for the discussion if it should be part of HTTPie or not.

@nrocco nrocco mentioned this issue May 8, 2014
@nrocco
Copy link
Author

nrocco commented May 8, 2014

@jakubroztocil I have prepared a pull request #217. I am looking forward to your thoughts.

@jkbrzt
Copy link
Member

jkbrzt commented Jul 2, 2016

Have a look at https://github.com/eliangcs/http-prompt if you're interested in this feature.

@jkbrzt jkbrzt closed this as completed Jul 2, 2016
@trollfred
Copy link

@jakubroztocil I would vote for this feature, using http-prompt is not always convenient

@noahcoad
Copy link

+1 This would be helpful!

@johnnymetz
Copy link

+1 Must have this!

@hmeine
Copy link

hmeine commented Jul 12, 2021

Please can someone comment on #838 ? I feel like the existing session functionality could be a good match for adding a default host (instead of localhost).

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

No branches or pull requests

7 participants