<?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 Correct Search string for search using the Splunk API in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Correct-Search-string-for-search-using-the-Splunk-API/m-p/20953#M3359</link>
    <description>&lt;P&gt;I want to be able to do a search like "UserName=Bleh sourcetype=ns_log" but it doesn't seem to work. Does the API use a different syntax than the GUI?&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jan 2011 23:49:28 GMT</pubDate>
    <dc:creator>bsonposh</dc:creator>
    <dc:date>2011-01-04T23:49:28Z</dc:date>
    <item>
      <title>Correct Search string for search using the Splunk API</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Correct-Search-string-for-search-using-the-Splunk-API/m-p/20953#M3359</link>
      <description>&lt;P&gt;I want to be able to do a search like "UserName=Bleh sourcetype=ns_log" but it doesn't seem to work. Does the API use a different syntax than the GUI?&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jan 2011 23:49:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Correct-Search-string-for-search-using-the-Splunk-API/m-p/20953#M3359</guid>
      <dc:creator>bsonposh</dc:creator>
      <dc:date>2011-01-04T23:49:28Z</dc:date>
    </item>
    <item>
      <title>Re: Correct Search string for search using the Splunk API</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Correct-Search-string-for-search-using-the-Splunk-API/m-p/20954#M3360</link>
      <description>&lt;P&gt;I found this link which is helpful: &lt;/P&gt;

&lt;P&gt;This answer was two fold. &lt;/P&gt;

&lt;P&gt;1) Finding the write string to send.
2) Figuring out how to send the correct POST string via .NET/Powershell.&lt;/P&gt;

&lt;P&gt;For one I used: &lt;A href="http://www.splunk.com/base/Documentation/latest/SearchReference/Search" rel="nofollow"&gt;http://www.splunk.com/base/Documentation/latest/SearchReference/Search&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;For two I add to build the string and use System.Web.HttpUtility.UrlEncode and then convert the string to a byte array.&lt;/P&gt;

&lt;P&gt;Here is the sample code (Powershell)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;function New-SplunkSearchJob
{

    [Cmdletbinding()]
    Param(

        [Parameter()]
        [String]$Server = $Splunk_Server,

        [Parameter()]
        [int]$Port = $Splunk_Port,

        # Search parameters support in POST
        # &lt;A href="http://www.splunk.com/base/Documentation/latest/Developer/RESTSearch#POST" target="test_blank"&gt;http://www.splunk.com/base/Documentation/latest/Developer/RESTSearch#POST&lt;/A&gt;

        [Parameter()]           # search
        [String]$Search = "search *",

        [Parameter()]           # required_field_list (comma separated list)
        [String]$RequireFields,

        [Parameter()]           # earliest_time
        [String]$StartDate,

        [Parameter()]           # latest_time
        [String]$EndDate,

        [Parameter()]           # id
        [String]$ID,

        [Parameter()]           # max_count = int
        [int]$MaxCount,

        [Parameter()]           # max_time = int
        [int]$MaxTime,

        [Parameter()]
        [System.Management.Automation.PSCredential]$Creds = $Splunk_Credentials

        # Plan to implement

        #[Parameter()]           # exec_mode = blocking | oneshot | normal (only supporting oneshot/normal)
        #[Switch]$Wait,      

        #[Parameter()]           # 'search_mode: normal | realtime'
        #[Switch]$Realtime,

    )

    function Get-Bytes($String)
    {
        [Byte[]]$byteArray = [System.Text.Encoding]::UTF8.GetBytes($String)
        $byteArray
    }

    $URL = "https://${Server}:${Port}/services/search/jobs" 

    Write-Verbose " [New-SplunkSearchJob] :: URL = $URL"

    $Request = [System.Net.WebRequest]::Create($url)
    $Request.Credentials = $Creds
    $Request.Method ="POST"
    $Request.ContentType = "application/x-www-form-urlencoded"
    $RequestStream = $Request.GetRequestStream()

    Write-Verbose " [New-SplunkSearchJob] :: Creating POST message"

    Write-Verbose " [New-SplunkSearchJob] :: Adding Search string [search=$Search] to POST message"
    #[string]$PostString = "search=$Search"
    [string]$PostString = "search={0}" -f [System.Web.HttpUtility]::UrlEncode($search)

    if($StartDate)
    {
        $PostString += "&amp;amp;earliest_time={0}" -f [System.Web.HttpUtility]::UrlEncode($StartDate)
    }

    if($EndDate)
    {
        $PostString += "&amp;amp;latest_time={0}" -f [System.Web.HttpUtility]::UrlEncode($EndDate)
    }

    if($MaxCount)
    {
        $PostString += "&amp;amp;max_count={0}" -f [System.Web.HttpUtility]::UrlEncode($MaxCount)
    }

    if($MaxTime)
    {
        $PostString += "&amp;amp;max_time={0}" -f [System.Web.HttpUtility]::UrlEncode($MaxTime)
    }


    Write-Verbose " [New-SplunkSearchJob] :: `$PostString = $PostString"

    Write-Verbose " [New-SplunkSearchJob] :: Converting POST message to Byte Array"
    $Bytes = Get-Bytes $PostString

    Write-Verbose " [New-SplunkSearchJob] :: Sending POST message"
    $RequestStream.Write($Bytes,0,$Bytes.length)

    Write-Verbose " [New-SplunkSearchJob] :: Closing POST stream"
    $RequestStream.Close()

    Write-Verbose " [New-SplunkSearchJob] :: Getting Response from POST"
    $Response = $Request.GetResponse()
    $Reader = new-object System.IO.StreamReader($Response.GetResponseStream())

    [XML]$Results = $Reader.ReadToEnd()

    $SID = $Results.Response.sid
    Write-Verbose " [New-SplunkSearchJob] :: ID = $SID"

    if($SID)
    {
        $cont = $true
        Write-Host "Please wait. It could take a bit..." -NoNewline
        while($cont)
        {
            $Job = Get-SplunkSearchJob -Filter $SID | ?{$_.isDone -eq 1}
            if($Job.ID)
            {
                $Job | Get-SplunkSearchJobResult 
                $cont = $false
                Write-Host
                Write-Host "Search complete"
                continue
            }
            sleep 1
        }
    }
}    # New-SplunkSearchJob
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Jan 2011 23:54:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Correct-Search-string-for-search-using-the-Splunk-API/m-p/20954#M3360</guid>
      <dc:creator>bsonposh</dc:creator>
      <dc:date>2011-01-04T23:54:30Z</dc:date>
    </item>
  </channel>
</rss>

