Splunk Dev

Could anyone spot why the code is failing to create a search with HTTP POST?

ChintanaM
Explorer

Dear all,

I am trying to initiate a search using Splunk cloud rest API. Using following code

 

 

 

 

const accessToken = "--my-super-secret-token--";
const url = "https://company-installation.splunkcloud.com:8089/services/search/jobs";

try {
  const authHeaderValue = `Splunk ${accessToken}`;

  const config = {
    headers: {
      'Authorization': authHeaderValue
    },
    params: {
      'output_mode': 'json',
      'search':'search *'
    }
  };

  const res = await axios.post(url, config);

  return {
    statusCode: 200,
    body: JSON.stringify(res.data),
  };
  
} catch (e) {


  return {
    statusCode: 400,
    body: JSON.stringify(e),
  };
}

 

 

 

 

 

When the code is executed I get a 401 at line const res = await axios.post(url, config);

My api token is valid and my IP address is whitelisted

When axios.post is replaced with axios.get, I get list of searches back which also verifies token and IP address are good

Could anyone spot why the code is failing to create a search with HTTP POST please?

I am very new to Splunk REST API and any help is much appreciated

 

 

 

Labels (1)
0 Karma
1 Solution

ChintanaM
Explorer

Hi all,

The issue was on how axios was dealing with its payload. Just needed to encode data for to be able to calculate content-length.

Cheers

CM

View solution in original post

0 Karma

arsen_ye
New Member

Hi @ChintanaM

I'm also new to splunk and I faced to a problem, maybe you can help me. Does you search param works fine? I'm also trying to search with axios post request but I'm getting 26mb of data. It seems that my search parameter doesn't work and my request return all the data that exists.   

Thanks in advance.

0 Karma

ChintanaM
Explorer

Hi all,

The issue was on how axios was dealing with its payload. Just needed to encode data for to be able to calculate content-length.

Cheers

CM

0 Karma

Pat
Path Finder

Could you expand on this with what you had changed?  I have no idea what you meant by encode data.

0 Karma
Get Updates on the Splunk Community!

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...

Combine Multiline Logs into a Single Event with SOCK: a Step-by-Step Guide for ...

Combine multiline logs into a single event with SOCK - a step-by-step guide for newbies Olga Malita The ...

Stay Connected: Your Guide to May Tech Talks, Office Hours, and Webinars!

Take a look below to explore our upcoming Community Office Hours, Tech Talks, and Webinars this month. This ...