<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to upload csv data file into splunk by using REST API? Can someone provide the exact URI Used to upload CSV File. I was confusing with the URI's provided by splunk. in Getting Data In</title>
    <link>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442886#M94007</link>
    <description>&lt;P&gt;I know this question is about two years old but I had to solve this issue for my team and I wanted to share the solution I came up with. &lt;/P&gt;

&lt;P&gt;@harsmarvania57 is correct that there is not a &lt;EM&gt;direct&lt;/EM&gt; way to import with api, but if you are feeling adventurous there is an alternative way of doing this if the file is not extremely massive. I only had powershell to work with when I created this and I wrote a script that does the following steps:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;converts the csv into json&lt;/LI&gt;
&lt;LI&gt;escapes any escapes \ and escaped quotes &lt;CODE&gt;\"&lt;/CODE&gt; in the json elements (you'll see why later)&lt;/LI&gt;
&lt;LI&gt;escapes any quotes in the json body&lt;/LI&gt;
&lt;LI&gt;takes the fully escaped json body and passes it into a splunk search via api and curl&lt;/LI&gt;
&lt;LI&gt;Using makeresults the search splits out the different rows and parses the json into the appropriate columns and outputs the results into the csv.  This is the reason we had to do all of the earlier escaping.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;The powershell script is below.  I have wrapped things you need to change in curly brackets with a label in between &lt;CODE&gt;{change me}&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$csvjson = import-csv "{absolute path to file}" | ConvertTo-Json
$escapingescapedescapes = $csvjson -replace '\\\\', '\\\\\\'
$escapingescapedquotes = $escapingescapedescapes -replace '([^\\])\\"','$1\\\"'
$fileescaped = $escapingescapedquotes -replace '([\n\r]\s+)"(.*)":(\s+)"(.*)"(,?[\n\r])','$1\"$2\":$3\"$4\"$5'
$search = '| makeresults count=1 | fields - _time | eval data="'+$fileescaped+'" | eval data=trim(data, "[]") | rex field=data mode=sed "s/(\s+)\},/\1}█/g" | makemv data delim="█" | mvexpand data | eval data="[".data."]" | spath input=data | fields - data | rename "{}.*" as * | outputlookup {lookup file name goes here}'
Add-Type -Assembly System.Web
$searchencoded = [System.Web.HttpUtility]::UrlEncode("$search")
curl.exe -k -u {credentials here} -X POST &lt;A href="https://{put" target="test_blank"&gt;https://{put&lt;/A&gt; your domain here}/services/search/jobs -d exec_mode=oneshot -d output_mode=csv -d count=0 -d search="$searchencoded"
Remove-Variable -Name pass
Remove-Variable -Name csvjson
Remove-Variable -Name escapingescapedescapes
Remove-Variable -Name escapingescapedquotes
Remove-Variable -Name fileescaped
Remove-Variable -Name search
Remove-Variable -Name searchencoded
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The four things that need to be changed are &lt;CODE&gt;{absolute path to file}&lt;/CODE&gt; on line 1 which should have the path to your file (e.g. &lt;CODE&gt;C:\Users\dmarling\Desktop\test.csv&lt;/CODE&gt;), &lt;CODE&gt;{lookup file name goes here}&lt;/CODE&gt; on line 5 which should have the name of your lookup file that you are writing to (e.g. &lt;CODE&gt;myTestLookup.csv&lt;/CODE&gt;), '{credentials here}' on line 7 is where you will need to put your splunk login credentials (e.g. &lt;CODE&gt;admin:P@ssW03d&lt;/CODE&gt;), and &lt;CODE&gt;{put your domain here}&lt;/CODE&gt; on line 7 which should have the domain and potentially ip address that you are connecting to via curl (e.g. &lt;CODE&gt;localhost:8089&lt;/CODE&gt;).&lt;/P&gt;

&lt;P&gt;The above process will take a csv that looks like this:&lt;BR /&gt;
column1,column2,rownum&lt;BR /&gt;
a,b,1&lt;BR /&gt;
b,a,2&lt;BR /&gt;
c,a,3&lt;BR /&gt;
d,b,4&lt;BR /&gt;
e,a,5&lt;BR /&gt;
f,e,6&lt;BR /&gt;
a,a,7&lt;BR /&gt;
b,b,8&lt;BR /&gt;
c,c,9&lt;BR /&gt;
d,"d,""4\""""",10&lt;/P&gt;

&lt;P&gt;and turn it into this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=1 | fields - _time | eval data="[
    {
        \"rownum\":  \"1\",
        \"column1\":  \"a\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"2\",
        \"column1\":  \"b\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"3\",
        \"column1\":  \"c\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"4\",
        \"column1\":  \"d\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"5\",
        \"column1\":  \"e\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"6\",
        \"column1\":  \"f\",
        \"column2\":  \"e\"
    },
    {
        \"rownum\":  \"7\",
        \"column1\":  \"a\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"8\",
        \"column1\":  \"b\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"9\",
        \"column1\":  \"c\",
        \"column2\":  \"c\"
    },
    {
        \"rownum\":  \"10\",
        \"column1\":  \"d\",
        \"column2\":  \"d,\\\"4\\\\\\\"\\\"\"
    }
]" | eval data=trim(data, "[]") | rex field=data mode=sed "s/(\s+)\},/\1}█/g" | makemv data delim="█" | mvexpand data | eval data="[".data."]" | spath input=data | fields - data | rename "{}.*" as * | outputlookup myTestLookup.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This process has some potential pitfalls especially with very large csv files as you may run into memory constraints using mvexpand depending on the limits that are put upon you as a user. This process can be ported to be used on linux based systems, but I unfortunately did not have access to one when I was creating the above process.&lt;/P&gt;</description>
    <pubDate>Tue, 14 Apr 2020 15:45:41 GMT</pubDate>
    <dc:creator>dmarling</dc:creator>
    <dc:date>2020-04-14T15:45:41Z</dc:date>
    <item>
      <title>how to upload csv data file into splunk by using REST API? Can someone provide the exact URI Used to upload CSV File. I was confusing with the URI's provided by splunk.</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442884#M94005</link>
      <description>&lt;P&gt;hi i am trying  to upload csv data file to the splunk enterprise through the REST API, there were lot of URI's available for different operations, &lt;BR /&gt;
can someone provide exact URI which will upload CSV file to Splunk.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Oct 2018 14:35:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442884#M94005</guid>
      <dc:creator>gopij</dc:creator>
      <dc:date>2018-10-23T14:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to upload csv data file into splunk by using REST API? Can someone provide the exact URI Used to upload CSV File. I was confusing with the URI's provided by splunk.</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442885#M94006</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;As far as I know there are no easy way to upload CSV file using Splunk REST API. First you need to upload lookup file into Splunk Staging area &lt;CODE&gt;$SPLUNK_HOME/var/run/splunk/lookup_tmp&lt;/CODE&gt; and then you can move that CSV file to respective app.&lt;/P&gt;

&lt;P&gt;Have a look at POST method on REST API doc &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.2.0/RESTREF/RESTknowledge#data.2Flookup-table-files"&gt;http://docs.splunk.com/Documentation/Splunk/7.2.0/RESTREF/RESTknowledge#data.2Flookup-table-files&lt;/A&gt; &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Create a lookup table file by moving a file from the upload staging area into $SPLUNK_HOME
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 24 Oct 2018 08:40:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442885#M94006</guid>
      <dc:creator>harsmarvania57</dc:creator>
      <dc:date>2018-10-24T08:40:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to upload csv data file into splunk by using REST API? Can someone provide the exact URI Used to upload CSV File. I was confusing with the URI's provided by splunk.</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442886#M94007</link>
      <description>&lt;P&gt;I know this question is about two years old but I had to solve this issue for my team and I wanted to share the solution I came up with. &lt;/P&gt;

&lt;P&gt;@harsmarvania57 is correct that there is not a &lt;EM&gt;direct&lt;/EM&gt; way to import with api, but if you are feeling adventurous there is an alternative way of doing this if the file is not extremely massive. I only had powershell to work with when I created this and I wrote a script that does the following steps:&lt;/P&gt;

&lt;OL&gt;
&lt;LI&gt;converts the csv into json&lt;/LI&gt;
&lt;LI&gt;escapes any escapes \ and escaped quotes &lt;CODE&gt;\"&lt;/CODE&gt; in the json elements (you'll see why later)&lt;/LI&gt;
&lt;LI&gt;escapes any quotes in the json body&lt;/LI&gt;
&lt;LI&gt;takes the fully escaped json body and passes it into a splunk search via api and curl&lt;/LI&gt;
&lt;LI&gt;Using makeresults the search splits out the different rows and parses the json into the appropriate columns and outputs the results into the csv.  This is the reason we had to do all of the earlier escaping.&lt;/LI&gt;
&lt;/OL&gt;

&lt;P&gt;The powershell script is below.  I have wrapped things you need to change in curly brackets with a label in between &lt;CODE&gt;{change me}&lt;/CODE&gt;:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;$csvjson = import-csv "{absolute path to file}" | ConvertTo-Json
$escapingescapedescapes = $csvjson -replace '\\\\', '\\\\\\'
$escapingescapedquotes = $escapingescapedescapes -replace '([^\\])\\"','$1\\\"'
$fileescaped = $escapingescapedquotes -replace '([\n\r]\s+)"(.*)":(\s+)"(.*)"(,?[\n\r])','$1\"$2\":$3\"$4\"$5'
$search = '| makeresults count=1 | fields - _time | eval data="'+$fileescaped+'" | eval data=trim(data, "[]") | rex field=data mode=sed "s/(\s+)\},/\1}█/g" | makemv data delim="█" | mvexpand data | eval data="[".data."]" | spath input=data | fields - data | rename "{}.*" as * | outputlookup {lookup file name goes here}'
Add-Type -Assembly System.Web
$searchencoded = [System.Web.HttpUtility]::UrlEncode("$search")
curl.exe -k -u {credentials here} -X POST &lt;A href="https://{put" target="test_blank"&gt;https://{put&lt;/A&gt; your domain here}/services/search/jobs -d exec_mode=oneshot -d output_mode=csv -d count=0 -d search="$searchencoded"
Remove-Variable -Name pass
Remove-Variable -Name csvjson
Remove-Variable -Name escapingescapedescapes
Remove-Variable -Name escapingescapedquotes
Remove-Variable -Name fileescaped
Remove-Variable -Name search
Remove-Variable -Name searchencoded
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The four things that need to be changed are &lt;CODE&gt;{absolute path to file}&lt;/CODE&gt; on line 1 which should have the path to your file (e.g. &lt;CODE&gt;C:\Users\dmarling\Desktop\test.csv&lt;/CODE&gt;), &lt;CODE&gt;{lookup file name goes here}&lt;/CODE&gt; on line 5 which should have the name of your lookup file that you are writing to (e.g. &lt;CODE&gt;myTestLookup.csv&lt;/CODE&gt;), '{credentials here}' on line 7 is where you will need to put your splunk login credentials (e.g. &lt;CODE&gt;admin:P@ssW03d&lt;/CODE&gt;), and &lt;CODE&gt;{put your domain here}&lt;/CODE&gt; on line 7 which should have the domain and potentially ip address that you are connecting to via curl (e.g. &lt;CODE&gt;localhost:8089&lt;/CODE&gt;).&lt;/P&gt;

&lt;P&gt;The above process will take a csv that looks like this:&lt;BR /&gt;
column1,column2,rownum&lt;BR /&gt;
a,b,1&lt;BR /&gt;
b,a,2&lt;BR /&gt;
c,a,3&lt;BR /&gt;
d,b,4&lt;BR /&gt;
e,a,5&lt;BR /&gt;
f,e,6&lt;BR /&gt;
a,a,7&lt;BR /&gt;
b,b,8&lt;BR /&gt;
c,c,9&lt;BR /&gt;
d,"d,""4\""""",10&lt;/P&gt;

&lt;P&gt;and turn it into this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults count=1 | fields - _time | eval data="[
    {
        \"rownum\":  \"1\",
        \"column1\":  \"a\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"2\",
        \"column1\":  \"b\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"3\",
        \"column1\":  \"c\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"4\",
        \"column1\":  \"d\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"5\",
        \"column1\":  \"e\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"6\",
        \"column1\":  \"f\",
        \"column2\":  \"e\"
    },
    {
        \"rownum\":  \"7\",
        \"column1\":  \"a\",
        \"column2\":  \"a\"
    },
    {
        \"rownum\":  \"8\",
        \"column1\":  \"b\",
        \"column2\":  \"b\"
    },
    {
        \"rownum\":  \"9\",
        \"column1\":  \"c\",
        \"column2\":  \"c\"
    },
    {
        \"rownum\":  \"10\",
        \"column1\":  \"d\",
        \"column2\":  \"d,\\\"4\\\\\\\"\\\"\"
    }
]" | eval data=trim(data, "[]") | rex field=data mode=sed "s/(\s+)\},/\1}█/g" | makemv data delim="█" | mvexpand data | eval data="[".data."]" | spath input=data | fields - data | rename "{}.*" as * | outputlookup myTestLookup.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This process has some potential pitfalls especially with very large csv files as you may run into memory constraints using mvexpand depending on the limits that are put upon you as a user. This process can be ported to be used on linux based systems, but I unfortunately did not have access to one when I was creating the above process.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Apr 2020 15:45:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/442886#M94007</guid>
      <dc:creator>dmarling</dc:creator>
      <dc:date>2020-04-14T15:45:41Z</dc:date>
    </item>
    <item>
      <title>Re: how to upload csv data file into splunk by using REST API? Can someone provide the exact URI Used to upload CSV File</title>
      <link>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/526055#M94008</link>
      <description>&lt;P&gt;Hey dmarling,&lt;/P&gt;&lt;P&gt;I spotted your feedback searching in google how to upload files to Splunk using curl command.&lt;/P&gt;&lt;P&gt;In my case, I'd like to upload a json formatted file but I have no idea how the curl command has too look like. So the normal usage of sending a single event I found in Splunk documentation looks as follows:&lt;/P&gt;&lt;PRE&gt;curl -k "https://mysplunkserver.example.com:8088/services/collector" \
    -H "Authorization: Splunk CF179AE4-3C99-45F5-A7CC-3284AA91CF67" \
    -d '{"event": "Hello, world!", "sourcetype": "manual"}'&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Are you able to help modifying this to allow me the upload of a json file?&lt;/P&gt;&lt;P&gt;regard&lt;BR /&gt;Ralf&lt;/P&gt;</description>
      <pubDate>Thu, 22 Oct 2020 15:27:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Getting-Data-In/how-to-upload-csv-data-file-into-splunk-by-using-REST-API-Can/m-p/526055#M94008</guid>
      <dc:creator>Ralf</dc:creator>
      <dc:date>2020-10-22T15:27:47Z</dc:date>
    </item>
  </channel>
</rss>

