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 3966032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:32:43+00:00 2026-05-20T03:32:43+00:00

I want to get data from an API using Python. The API documentation give

  • 0

I want to get data from an API using Python. The API documentation give examples in CURL and Ruby. I would be very happy if you can post code snippets on how to do the following things with Python.

To get authentication token:

Curl example:

curl -X POST -d "{\"username\" : \"user@sample.com\", \"password\":\"sample\"}" http://api.sample.com/authenticate

Ruby example:

require 'rubygems'
require 'rest_client'
require 'json'

class AuthorizationClient
  attr_accessor :base_url

  def initialize(base_url)
    @base_url = base_url
  end

  def authenticate(username,password)
    login_data = { 'username' => username, 'password' => password}.to_json
    begin
      JSON.parse(RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json)['output']
    rescue Exception => e
      JSON.pretty_generate JSON.parse e.http_body
    end
  end
end

client = AuthorizationClient.new('http://api.sample.com/authenticate')
puts client.authenticate('user@sample.com','sample')

After authentication, to get data:

CURL example:

curl http://api.sample.com/data/day/2011-02-10/authToken/80afa08-1254-46ee-9545-afasfa4565

And Ruby code:

require 'rubygems'
require 'rest_client'
require 'json'

class ReportingClient
  attr_accessor :auth_token, :base_url

  def initialize(base_url)
    @base_url = base_url
  end

  def authenticate(username,password)
    login_data = { 'username' => username, 'password' => password}.to_json

    response = RestClient.post "#{@base_url}/authenticate", login_data, :content_type => :json, :accept => :json
    @auth_token = JSON.parse(response)['output']
  end

  def get_report(start_date, end_date)
    response = RestClient.get "#{@base_url}/data/day/#{day}/authToken/#{auth_token}"
    JSON.parse(response)
  end

end

client = ReportingClient.new('http://api.sample.com:20960')
client.authenticate('user@sample.com','sample')

results = client.get_report('2011-02-10')

puts JSON.pretty_generate(results)

Thank you..

PS: I am aware of pycurl. But I am not sure if I really need it. I am happy with using Python native libraries. Pycurl might be over-kill for my needs.

I am new to Python and I couldn’t find the right solution after reading ‘urllib2’ documentation and trying examples.

  • 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-20T03:32:44+00:00Added an answer on May 20, 2026 at 3:32 am

    This is not a port but it looks like you are sending a POST and then obtaining the authorization token and then sending a GET request using this.
    This is based on what I understood.

    import urllib
    import urllib2
    import simplejson
    import datetime
    
    authURL = "http://api.sample.com/authenticate"
    values = {"username" : "user@sample.com",
              "password" : "sample"}
    
    data = urllib.urlencode(values)
    
    req = urllib2.Request(authURL, data)
    response = urllib2.urlopen(req)
    
    authToken = simplejson.load(response)["output"]
    
    day = str(datetime.date.today())
    dataURL = "http://api.sample.com/data/day/" + day + "/authToken/" + authToken
    
    print simplejson.load(urllib2.urlopen(dataURL))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to get the base 10 logarithm of a Fixnum using Ruby, but
I want to build a service which needs to get this data from some
I am developing an app using data from google reader's API and using GData
I want to create gallery application which would get thumbnails and metadata from web
What method do you use when you want to get performance data about specific
I have image data and i want to get a sub image of that
I want to get at the item that is being data bound, during the
table data of 2 columns category and subcategory i want to get a collection
We get these ~50GB data files consisting of 16 byte codes, and I want
I want to get started doing some game development using Microsoft's XNA. Part of

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.