You are here

Having problems with the REST api

3 posts / 0 new
Last post
dsaa
dsaa's picture
Having problems with the REST api

I need to consume services from the api documented here:
http://docs.opsview.com/doku.php?id=opsview-community:restapi

The login call returns a token successfully. However, when I try other calls, I'm getting the following error:

    failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden

This is the function I wrote to make my calls:

    public function sendRequestGetResults($path, $data = [], $method = 'GET', $headers =  [], $contentType = "application/json")
    {        
        return json_decode
        (
            file_get_contents
            (
                'https://' . $this->apiHost . '/rest/' . $path,
                false,
                stream_context_create
                (
                        [
                            'http' =>
                            [
                                'header'  => array_merge
                                (
                                        [
                                            "Content-type: " . $contentType
                                        ],
                                        $headers
                                ),
                                'method'  => $method,
                                'content' => json_encode
                                (
                                        $data
                                )
                            ],
                        ]
                )        
            )
        );
    }

I tried adding the login and the token returned by the login call to the headers of the request by calling my function with the following $headers parameter, but still no luck:

    [
        "X-Opsview-Username: myUsername",
        "X-Opsview-Token: myToken"
    ]

Doing the curl calls on the command line, I get the token by a call like this:

    curl  -H "Content-type: application/json" -X POST -d '{"username":"myUser","password":"myPassword"}'  https://my.api.com/rest/login

And with the token returned by the previous call, I try a call like the following:

    curl -v -H "Accept: application/json" -H "Content-type: application/json\r\nX-Opsview-Username: myUser\r\nX-Opsview-Token: myToken\r\n" -X GET -d '{}'  https://my.api.com/rest/event

But in the response I get

    {"message":"Token invalid"}

What might I be missing?

Thanks!

tvoon
tvoon's picture
Re: Having problems with the REST api

I don't know about the Java code, but in curl, the issue is that your headers are not set correctly. The clue is in your debug:

 

 GET /opsview/rest/event HTTP/1.1 > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8x zlib/1.2.3 > Host: server > Accept: application/json > Content-type: application/json\r\nX-Opsview-Username: username\r\nX-Opsview-Token:  1140c53547b3155d743a601891038ff275d63e51\r\n > Content-Length: 2  

The header is not on the correct lines. You should run separate -H calls so each header is on a different line:

curl -v -H 'Accept: application/json' -H 'Accept: application/json' -H "Content-type: application/json" -H "X-Opsview-Username: username" -H "X-Opsview-token: token" -X GET -d '{}' "https://server/opsview/rest/event?rows=10"

This gives:

 

> GET /opsview/rest/event?rows=10 HTTP/1.1 > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8x zlib/1.2.3 > Host: server > Accept: application/json > Content-type: application/json > X-Opsview-Username: username > X-Opsview-token: 1140c53547b3155d743a601891038ff275d63e51   So now the Opsview server can read the headers correctly.   Ton
dsaa
dsaa's picture
Re: Having problems with the REST api

Thanks for the reply! Your observation got me closer, and now I do get data when I call /rest/info for instance:

{"opsview_edition":"commercial","opsview_build":"4.4.0.13139","server_timezone_offset":"-14400","opsview_version":"4.4.0","server_timezone":"America/New_York","uuid":"DDE6AD24-6DCD-11E1-B449-71C5AECADF8C"}

However, when I make the call that I actually need for my application, which is /rest/status/performancemetric, I get a 502 response:

"Opsview Apache Error:  502, Opsview is not running or responding to requests", when running curl from the terminal, and

"HTTP request failed! HTTP/1.1 502 Bad Gateway" when using my application.

Any ideas on what could be going on?

Thanks!

Diego Saa