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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:28:53+00:00 2026-06-11T12:28:53+00:00

The following function connects to an API and returns a json object. Although it

  • 0

The following function connects to an API and returns a json object. Although it only works on development. In the production server it just returns False.

Any ideas why it doesn’t work in a different server?

def request_api(api_call, post_fields, authentication_data = None):
    try:
        log.debug("Connecting To API: " + settings.API_URL + api_call)
        curl = pycurl.Curl()
        curl.setopt(pycurl.URL, settings.API_URL + api_call)
    except:
        log.debug("Can't connect To API: " + settings.API_URL + api_call)
    if post_fields:
        try:
            log.debug("Post Fields: " + post_fields)
            curl.setopt(curl.POSTFIELDS, str(post_fields))
        except:
            log.debug("Error setting post fields.")
    if authentication_data:
        try:
            log.debug("Authentication Fields: " + authentication_data)
            curl.setopt(pycurl.USERPWD, authentication_data)
        except:
            log.debug("Error during authentication.")
    try:
        contents = StringIO.StringIO()
        curl.setopt(pycurl.WRITEFUNCTION, contents.write)
        curl.perform()
    except:
        log.debug("Error on curl.")
    try:
        responseCode = curl.getinfo(pycurl.HTTP_CODE);
        log.debug("Response Code: " + str(responseCode))
    except:
        log.debug("Response Code: Error")
    try:
        log.debug("Response: " + contents.getvalue())
    except:
        log.debug("Response: Error")
    try:
        pyobj = json.loads(contents.getvalue())
        log.debug("Response In Json Format: " + str(pyobj))
    except:
        log.debug("Response In Json Format: Error")
    return pyobj

Error:

Exception Type:     UnboundLocalError
Exception Value:    

local variable 'pyobj' referenced before assignment

Log File:

[18/Sep/2012 04:07:52] DEBUG [contests.views:29] Connecting To API: https://my-api/v1/token
[18/Sep/2012 04:07:52] DEBUG [contests.views:39] Authentication Fields: test@user.com:test@user.com
[18/Sep/2012 04:49:32] DEBUG [contests.views:51] Error on curl.
[18/Sep/2012 04:49:32] DEBUG [contests.views:54] Response Code: 0
[18/Sep/2012 04:49:32] DEBUG [contests.views:58] Response: 
[18/Sep/2012 04:49:32] DEBUG [contests.views:65] Response In Json Format: Error
  • 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-06-11T12:28:55+00:00Added an answer on June 11, 2026 at 12:28 pm

    What’s causing the immediate error is this:

    try:
        pyobj = json.loads(contents.getvalue())
        log.debug("Response In Json Format: " + str(pyobj))
    except:
        log.debug("Response In Json Format: Error")
    return pyobj
    

    The try clause is failing, as you can see in your log, which means pyobj isn’t being defined. When you try to return it at the end of the function, it doesn’t exist, so you get an UnboundLocalError.

    Why this section is failing will have to do with either contents.getValue crashing or passing something inappropriate to json.loads. However, because you are catching all exceptions and returning your own less than helpful error messages, you are hiding all of the relevant traceback information that would help you realise where the problem really lies. The traceback will tell you exactly what the error is and where it occurred down to the file & line of code, which will be of much more use to you than Response In Json Format: Error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Facebook's FB.api( '/me' ) function recently started throwing the following error when run in
Using the Actionscript API, I am receiving the following IOError [IOErrorEvent type=ioError bubbles=false cancelable=false
The following function rewrites urls from news and product titles that contain all sorts
The following function is designed to check whether this row in this tables exists.
The following function that reports the current line-number (and time) which I would like
The following function System.Threading.Thread.Sleep(); delay the thread in millisecond, and take the integer value
/** The following function checks the red black tree black height * @param n
The following function generates a 'stack level too deep (SystemStackError)' for n = 5,000
The following function is trying to find the nth to last element of a
/************************************************************************** * * Function: toggleVis * * Description: Following Function hides and expands the

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.