Forum Discussion

SmokinIndo's avatar
4 years ago

How to post an API key on a header with the post() method using Groovy?

 


I'm trying to write a script in Groovy that makes an API request to check the status of VPN tunnels. I've been trying the implement the post() method from the HTTP library to update the header of the url with the API key. I keep running into an error "no signature on method". This is my code so far.

import com.santaba.agent.groovyapi.http.*;
ip = "api-mp.meraki.com";
httpClient = HTTP.open(ip, 443);
url = "https://"+ip+"/api/v1/organizations/999999/appliance/vpn/statuses";
def postResponse = httpClient.post(url,["X-Cisco-Meraki-API-Key":"ApiKey"]);

I just followed the example from this tutorial: https://www.logicmonitor.com/support/terminology-syntax/scripting-support/access-a-website-from-groovy

I'm wondering if I'm getting the error because I'm not inserting a payload in my method. But I don't have a payload... I'm just trying to update the header.

2 Replies

  • 12 hours ago, SmokinIndo said:
     

     

    I'm trying to write a script in Groovy that makes an API request to check the status of VPN tunnels. I've been trying the implement the post() method from the HTTP library to update the header of the url with the API key. I keep running into an error "no signature on method". This is my code so far.

    
    import com.santaba.agent.groovyapi.http.*;
    ip = "api-mp.meraki.com";
    httpClient = HTTP.open(ip, 443);
    url = "https://"+ip+"/api/v1/organizations/999999/appliance/vpn/statuses";
    def postResponse = httpClient.post(url,["X-Cisco-Meraki-API-Key":"ApiKey"]);
    

    I just followed the example from this tutorial: https://www.logicmonitor.com/support/terminology-syntax/scripting-support/access-a-website-from-groovy

    I'm wondering if I'm getting the error because I'm not inserting a payload in my method. But I don't have a payload... I'm just trying to update the header.

     

    Maybe it's easier for you to use one the already existent datasource(s) that queries Cisco Meraki API & from there build the query you actually want

    You can refer to the code of the DS below for example:

    This is usually what I do if I want to extend the capabilities of certain monitoring... I always check if they didn't did the 'connection' already. That way I don't need to loose time understanding how to do it :)/emoticons/smile@2x.png 2x" title=":)" width="20" /> 

  • Anonymous's avatar
    Anonymous

    From the support docs:

    Quote

    post(url, payload, headers] do an HTTP POST to the provided url and return the entire response

     string url – the target url

     string payload – the post data payload

     map headers – a map containing one or more HTTP POST headers

     return string response  the entire HTTP response

     

    It appears you want to pass a blank payload and add the X-Cisco-Meraki-API-Key header. In that case, you'll need to include a blank payload string:

    import com.santaba.agent.groovyapi.http.*
    
    ip = "api-mp.meraki.com"
    url = "https://"+ip+"/api/v1/organizations/999999/appliance/vpn/statuses"
    key = hostProps.get("merakiAPIKey")
    
    httpClient = HTTP.open(ip, 443)
    def postResponse = httpClient.post(url,"",["X-Cisco-Meraki-API-Key":key])

    I haven't checked this, so it'd remain to be verified, but this is at least how the docs indicate it should work. I've also reordered it to be more standard and to fetch the api key from the device property (assuming you're running this as a scripted LogicModule).