• 0

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


Question

 


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.

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
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:

image.png.c0122146c1fa3bbd73f30c1be25b3390.png

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 :) 

Link to post
Share on other sites
  • 0
  • Administrators

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). 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.