Forum Discussion

Javier's avatar
Javier
Icon for Neophyte rankNeophyte
4 years ago

Unable to get eventsources list using REST API

I am trying to get eventsources information using REST API, as documented here.

I am using a small python script to try to get any results, but can only get this output:

('Response Status:', 406)
('Response Body:', '<!doctype html><html lang="en"><head><title>HTTP Status 406 \xe2\x80\x93 Not Acceptable</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 406 \xe2\x80\x93 Not Acceptable</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> Not Acceptable</p><p><b>Description</b> The target resource does not have a current representation that would be acceptable to the user agent, according to the proactive negotiation header fields received in the request, and the server is unwilling to supply a default representation.</p><hr class="line" /><h3>LogicMonitor Technical Operations</h3></body></html>')

Did anybody get this type of call working anytime?

Apart from this, I would like to add new eventsources using the REST API. It looks like there is only an import method to get this, using a XML file.

Are there any examples or templates of that XML file?

Thanks and regards.

  • I finally changed my code to use the SDK. I recognize I didn't use it at the first time as it looked somehow complicated. When I saw this example code I realized it was really simple to use. 

    Now my code works perfectly! Thanks a lot for your help!

     

  • Anonymous's avatar
    Anonymous

    That data dictionary must be converted to JSON before being concatenated with the requestVars. As suggested, try the SDK. Note that the SDK example is auto-generated and in this case is missing the quotes around the path to the file.

  • I am trying this and found several problems.

    When using on the header, the "Content-Type" spec, I always get an error 500 with an "Unknown error" message. I tried removing it and it looks to work better.

    Now I am getting a clear "Authentication failed" error message. I think the problem I have now is creating the correct authentication string. As this string is build with, among others, the request details, I don't know how to correctly append this "files" variable (a dictionary object) to the request variable using this piece of code:

    #Concatenate Request details
    requestVars = httpVerb + epoch + data + resourcePath
    
    #Construct signature
    signature = base64.b64encode(hmac.new(AccessKey,msg=requestVars,digestmod=hashlib.sha256).hexdigest())
    
    #Construct headers
    auth = 'LMv1 ' + AccessId + ':' + signature + ':' + epoch
    headers = {'Authorization':auth}

    In this case, "data" is not a string, but a dictionary containing a file object.

    How authorization string should be built for this particular case?

    I will have a look at the SDK anytime, as suggested.

  • 9 hours ago, Javier said:

    Hi again,

    I prepared my script to programmatically create the XML file to define new EventSources. I test it successfully from the UI, using "Add" - "From File".

    Now I would like to use the API to make such a call. According swagger documentation here, it looks like I need to provide this file as a parameter, but I don't have a clear idea on how to provide that. I tried several methods, but none of them worked so far:

    Using any of these methods, I always get an error "HTTP 415 Unsupported Media Type". 

    What am I doing wrong? 

     

    Set the content type as multipart/form-data then send a file object as a local file read for python

     

    header = {'Content-Type': 'multipart/form-data', 'Authorization': 'LMv1....'}
    files = {'file': ('file.xml', open('file.xml', 'rb'),'text/xml')}
    
    r = requests.post(url, headers=header, files=files, verify=False)

     

     

    You would be much better using the SDK though

  • Hi again,

    I prepared my script to programmatically create the XML file to define new EventSources. I test it successfully from the UI, using "Add" - "From File".

    Now I would like to use the API to make such a call. According swagger documentation here, it looks like I need to provide this file as a parameter, but I don't have a clear idea on how to provide that. I tried several methods, but none of them worked so far:

    Using any of these methods, I always get an error "HTTP 415 Unsupported Media Type". 

    What am I doing wrong? 

  • Thanks for the clue!!

    I solved the problem with my python script. The solution was to include ?v=2 as the first parameter:

    https://xxxxx.logicmonitor.com/santaba/rest/setting/eventsources?v=2

     

    I also found that option to export an eventsource to a XML file using the UI. I hope I can work with that pretty well.

    Thanks a lot for your help!

  • Anonymous's avatar
    Anonymous

    You can get an example XML file by exporting any EventSource from the UI. Just navigate to an EventSource >> More >> Export. 

    As far as the get request, i get the same thing if i don't specify "2" in the X-Version header. Basically, the endpoint for v1 appears to be only partially implemented. Use v2 of the API.

  • Can you provide some example code? They fact that your getting html response instead of JSON likely means something more fundamental is an issue then the specific EventSource API details. You might want to use some of the REST v1 examples on the support pages, to make sure any API calls work, then you can convert them to v2 convention.

    Based on the v2 docs, it does look like import via XML is the documented method to create EventSources. You might need to generate the XML file (perhaps by having an XML template that the script modifies) to provide into the API call.