Ad Widget

Collapse

Zabbix API: Error 412 Precondition Failed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fandingo
    Junior Member
    • Dec 2012
    • 6

    #1

    Zabbix API: Error 412 Precondition Failed

    I'm trying to get started with the Zabbix API, but I always get HTTP 412 responses no matter what I try.

    I'm using Python and Zabbix 2.0.3.

    Code:
    import urllib2
    import simplejson
    
    a = { 'jsonrpc': '2.0', 'method': 'user.login', 'params': { 'user': 'Admin', 'password' : '*******'}, 'id' : 1}
    b = simplejson.dumps(a)
    c = urllib2.urlopen('http://MYSERVER/zabbix/api_jsonrpc.php', data=b)
    Code:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
        return _opener.open(url, data, timeout)
      File "/usr/lib64/python2.6/urllib2.py", line 397, in open
        response = meth(req, response)
      File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response
        'http', request, response, code, msg, hdrs)
      File "/usr/lib64/python2.6/urllib2.py", line 435, in error
        return self._call_chain(*args)
      File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain
        result = func(*args)
      File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default
        raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 412: Precondition Failed
    I've tried some other operations like host.create, but I always get the same 412 error.

    In the Apache access logs, I see
    Code:
    MYSERVERIP - - [22/Jan/2013:14:47:55 -0600] "POST /zabbix/api_jsonrpc.php HTTP/1.1" 412 - "-" "Python-urllib/2.6"
    Has anyone experienced this problem and know of a workaround?

    Thanks,
  • fandingo
    Junior Member
    • Dec 2012
    • 6

    #2
    I figured it out.

    API calls must set the Content-Type header to application/json.

    Here's a working example:

    Code:
    import urllib2
    import simplejson
    a = { 'jsonrpc': '2.0', 'method': 'user.authenticate', 'params': { 'user': 'Admin', 'password' : '*****'}, 'id' : 1}
    b = js.dumps(a)
    c = urllib2.Request('http://MYSERVER/zabbix/api_jsonrpc.php', b, {'Content-Type': 'application/json'})
    d = urllib2.urlopen(c)
    d.read()
    Code:
    '{"jsonrpc":"2.0","result":"7f4901b22b63cd2***************","id":1}'

    Comment

    • Pavels
      Member
      • Oct 2011
      • 83

      #3
      Keep in mind that the user.authenticate method is deprecated starting from 2.2. You should use user.login instead:

      Comment

      • fandingo
        Junior Member
        • Dec 2012
        • 6

        #4
        Pavels thanks for the reminder.

        I did review the migration guide. No use in writing code that's already deprecated.

        Comment

        • SergejZ
          Junior Member
          • May 2012
          • 6

          #5
          Many thanks for the post

          Comment

          • SergejZ
            Junior Member
            • May 2012
            • 6

            #6
            It works as expected with Python2.7.
            But my be someone know how to use it with Python3.3 ?

            Comment

            Working...