How can I force urllib2/requests modules to use relative paths instead of full/absolute URL??
when I send request using urllib2/requests I see in my proxy that it re
I think there is a little bit of confusion here. As per RFC 2616 only absolute path or absolute URI are allowed in http request line. There is simply no such thing as relative http request -- as basicly http is stateless.
In your question you talking about proxy, that RFC state clearly that:
The absoluteURI form is REQUIRED when the request is being made to a proxy.
As per se, AFAIK, your proxy is not HTTP/1.1 compliant. Is this a commercial product or an in-house development?
By the way, HTTP 302 is a redirect. Are you sure the ressource hasn't simply moved to an other location?
Anyway, by looking at the source code or requests (requests/models.py L276) I'm afraid it doesn't seem to have any easy way to force the use of absolute path
My best bet would be to change the PreparedRequest
object before it is send as described in advanced usages/Prepared request.
Probably the following code would do for your case:
from urlparse import urljoin
import requests
class RelativeSession(requests.Session):
def __init__(self, base_url):
super(RelativeSession, self).__init__()
self.__base_url = base_url
def request(self, method, url, **kwargs):
url = urljoin(self.__base_url, url)
return super(RelativeSession, self).request(method, url, **kwargs)
session = RelativeSession('http://server.net')
response = session.get('/rel/url')