Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6942595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:02:37+00:00 2026-05-27T13:02:37+00:00

I am trying access a REST API. I can get it working in Curl/REST

  • 0

I am trying access a REST API.

I can get it working in Curl/REST Client (the UI tool), with preemptive authentication enabled.

But, using urllib2, it doesn’t seem to support this by default and I can’t find a way to turn it on.

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:02:38+00:00Added an answer on May 27, 2026 at 1:02 pm

    Here’s a simple Preemptive HTTP basic auth handler, based on the code from urllib2.HTTPBasicAuthHandler. It can be used in the exact same manner, except an Authorization header will be added to every request with a matching URL. Note that this handler should be used with a HTTPPasswordMgrWithDefaultRealm. That’s because there is no realm coming back in a WWW-Authenticate challenge since you’re being preemptive.

    class PreemptiveBasicAuthHandler(urllib2.BaseHandler):
    
            def __init__(self, password_mgr=None):
                    if password_mgr is None:
                            password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
                    self.passwd = password_mgr
                    self.add_password = self.passwd.add_password
    
            def http_request(self,req):
                    uri = req.get_full_url()
                    user, pw = self.passwd.find_user_password(None,uri)
                    #logging.debug('ADDING REQUEST HEADER for uri (%s): %s:%s',uri,user,pw)
                    if pw is None: return req
    
                    raw = "%s:%s" % (user, pw)
                    auth = 'Basic %s' % base64.b64encode(raw).strip()
                    req.add_unredirected_header('Authorization', auth)
                    return req
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to access a REST API for the first time using visual
I'm using versae's neo4j rest client to access a neo4j DB but I stumbled
I'm trying to access the REST Disqus API using the following url: http://disqus.com/api/3.0/threads/listPosts.json ?api_key=myKey
I have a REST API that I am trying to access using Rails 3.0.1
A consumer is trying to call my REST API from client-side code (Javascript /
Given: I'm trying to create a REST API using ASP.NET MVC . I'm using
Situation I'm trying to create a REST API, where users can request responses in
I'm trying to access the Sorenson-360 API with cURL and PHP, however I am
I am trying to access a REST api and need to call it with
I'm trying to call the Recurly REST API using the Hammock C# library for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.