Custom User-Agent not remembered during session #180

Closed
karlcow opened this Issue Nov 22, 2013 · 4 comments

3 participants

@karlcow

When testing it is often useful to have a set of predefined profiles such as different set of headers for different browsers. It helps with testing Web sites.

One way to do that currently is to create a variable for each profile.

→ export FOO="blah:X Yada:Z"
→ echo $FOO
blah:X Yada:Z
→ http --print hH GET http://www.example.org/ $FOO
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, compress
Host: www.example.org
User-Agent: HTTPie/0.7.2
Yada: Z
blah: X

HTTP/1.1 200 OK
[…]

but it would be entirely possible to have a feature such as

http --profile foo GET http://example.org/

where foo is a JSON file in .httpie/profiles/foo.json

@miketaylr

I would love this feature. 👍

@jkbrzt
Owner

HTTPie supports sessions that allow you to do this. Example:

Create a session file:

$ http --session=/tmp/foo.json example.org blah:X Yada:Z

# or, you can also create the file manually:
$ echo '
{    
    "headers": {
        "Yada": "Z",
        "blah": "X"
    }
}' > /tmp/foo.json

Reuse it whenever needed:

$ http -v --session=/tmp/foo.json example.org
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate, compress
Host: example.org
User-Agent: HTTPie/0.8.0-dev
Yada: Z
blah: X

Please see https://github.com/jkbr/httpie#sessions for more details.

@jkbrzt jkbrzt closed this Nov 23, 2013
@karlcow

I guess we can reopen the issue because it is not what we were expecting.

Let's say we create the JSON file:

{ "headers": {
    "User-Agent": "Mozilla/5.0 (Mobile; rv:26.0) Gecko/26.0 Firefox/26.0", 
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "en-us,en;q=0.5",
    "Accept-Encoding":  "gzip, deflate",
    "DNT": "1"
    }
}

Following your instructions, I do

→ http --print hH --session=./firefox-os.json GET http://www.mozilla.org/

which is leading to:

GET / HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-us,en;q=0.5
DNT: 1
Host: www.mozilla.org
User-Agent: HTTPie/0.7.2

HTTP/1.1 301 Moved Permanently
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private, max-age=900
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Wed, 18 Dec 2013 21:01:27 GMT
Expires: Wed, 18 Dec 2013 21:16:27 GMT
Location: http://www.mozilla.org/en-US/
Pragma: no-cache
Server: Apache
Vary: User-Agent,Accept-Language
X-Backend-Server: bedrock4.webapp.scl3.mozilla.com
X-Cache-Info: not cacheable; response specified "Cache-Control: no-store"
X-Robots-Tag: noodp

The first anomaly is User-Agent: HTTPie/0.7.2.
Then HTTPie is rewriting the session file to

{
    "__meta__": {
        "about": "HTTPie session file", 
        "help": "https://github.com/jkbr/httpie#sessions", 
        "httpie": "0.7.2"
    }, 
    "auth": {
        "password": null, 
        "type": null, 
        "username": null
    }, 
    "cookies": {}, 
    "headers": {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 
        "Accept-Encoding": "gzip, deflate", 
        "Accept-Language": "en-us,en;q=0.5", 
        "DNT": "1", 
        "User-Agent": "Mozilla/5.0 (Mobile; rv:26.0) Gecko/26.0 Firefox/26.0"
    }
}

So not working.

@karlcow

This will solve the rewriting

http --print hH --session-read-only=./firefox-os.json GET http://www.mozilla.org/

The User-Agent is still not working. I tried reordering the json file but without success.

@jkbrzt jkbrzt reopened this Dec 18, 2013
@jkbrzt jkbrzt changed the title from Profile of headers for HTTP requests to Custom User-Agent not remembered during session May 6, 2014
@jkbrzt jkbrzt added the bug label May 6, 2014
@jkbrzt jkbrzt added a commit that closed this issue May 8, 2014
@jkbrzt Make sure session and default headers play nice
Before: headers = default + args + session
Now:    headers = default + session + args

Fixes #180
858555a
@jkbrzt jkbrzt closed this in 858555a May 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment