Forum Discussion

ravikiranmn's avatar
2 years ago

pull csv report via rest api

how to generate csv report and download using rest api

  • Anonymous's avatar
    Anonymous
    2 years ago

    You can use GET /report/links/{reportid} to get the list of links to the data. (warning, this endpoint is technically unsupported because LM decided it should be undocumented)

    You’re supposed to be able to POST to /report/reports/{reportid}/executions to get it to generate the report immediately. The response should have the result url in it. However, in my testing the report url was always null because the report was not done generating by the time the API sent the response. Also, with this request, you have to pass in an email address that you want to send the report to. Once the task is finished, even if you get a null value in the API response, you will get the email with the link to the report.

    So you should be able to

    1. POST to /report/reports/{reportid}/executions with a bogus email (or your own) to start generating the report.
    2. You probably want to wait a few seconds (maybe 30, maybe longer depending on the size of the report).
    3. Then you should be able to GET at /report/links/{reportid} and get the last item in the list `$.items[-1:].reportUrl`. Then it would be a get at that URL. Be careful, it looks like there are extra spaces around the reportUrl, so use a .trim() method to clean it up before trying to fetch the CSV data itself.

    Before you start, you could do the GET on /report/links/{reportid} first to get the id of the last report data. That would let you do repeated GETs at /report/links/{reportid} after doing your post and check if the new data is present by looking for a new ID after the known ID. That would let you wait a minimal amount of time before fetching the data again.

  • Anonymous's avatar
    Anonymous

    You can use GET /report/links/{reportid} to get the list of links to the data. (warning, this endpoint is technically unsupported because LM decided it should be undocumented)

    You’re supposed to be able to POST to /report/reports/{reportid}/executions to get it to generate the report immediately. The response should have the result url in it. However, in my testing the report url was always null because the report was not done generating by the time the API sent the response. Also, with this request, you have to pass in an email address that you want to send the report to. Once the task is finished, even if you get a null value in the API response, you will get the email with the link to the report.

    So you should be able to

    1. POST to /report/reports/{reportid}/executions with a bogus email (or your own) to start generating the report.
    2. You probably want to wait a few seconds (maybe 30, maybe longer depending on the size of the report).
    3. Then you should be able to GET at /report/links/{reportid} and get the last item in the list `$.items[-1:].reportUrl`. Then it would be a get at that URL. Be careful, it looks like there are extra spaces around the reportUrl, so use a .trim() method to clean it up before trying to fetch the CSV data itself.

    Before you start, you could do the GET on /report/links/{reportid} first to get the id of the last report data. That would let you do repeated GETs at /report/links/{reportid} after doing your post and check if the new data is present by looking for a new ID after the known ID. That would let you wait a minimal amount of time before fetching the data again.

  • This “used” to work by using this:

    Get a URL for an existing Report execution task

    HTTP Method:GET

    URI: /report/reports/REPORT_ID/tasks/TASK_ID

    Where REPORT_ID is the id of the report, and TASK_ID is the id of the task you’re wanting to get a report URL for (returned when you run the report).

    As documented here:

    https://www.logicmonitor.com/support/rest-api-developers-guide/v1/reports/run-a-report

    It stopped working in our portal early April (right after a portal upgrade). I’ve had an open case ever since.

  • Anonymous's avatar
    Anonymous

    Looks like you’re not using Postman with variables. Your content-type is bad and you’re missing some stuff. Does postman work for any requests? Make sure your postman is working right.

    Also, it looks like you have redacted some information, but you didn’t redact your API key.

  • Sorry its company’s job so hide the url

    Stuart, how can we get report_id code to get reports

    I am using Postman for testing purpose is there any other tool to test

    Thank You Stuart for time and Support

  • Anonymous's avatar
    Anonymous

    Postman is the right way to test it out. Did you follow the instructions in the link? Do that first and test that you have it working by doing a GET on /report/reports. 

    It’s fine that you redacted some information, but you didn’t redact your API key, which is the actual password to get into your API. You should be using the pre-request script in postman, all of which is explained in the postman setup instructions linked above.

  • Wow,

    I just stumbled across this thread…. Here, I thought I was being a good citizen, trying to align my scripts with official LM API v3 endpoint calls.  So while the first part of getting report (/report/reports/{id}/executions) works fine, the second part of getting report - you know getting the actual data (CSV|HTML|PDF format) using the /report/reports/{REPORT_ID}/tasks/{TASK_ID} does not.  Getting HTTP Status Code 404. Wasted an hour on that.  It is broken.  So yeah, back to fetching data via GET reportUrl.  Which works fine.