2

So I am having a lot of trouble with how to send the below JSON data in C# format. I know exactly how to do it in cURL, but I can't figure this out for the life of me. This request is essential to what I am doing and I really need to get it done. Here is the curl statement:

   curl <ip of server>/<index>/_search?pretty=true -d '
 {
"query": {
    "match_all": {}
},
"size": 1,
"sort": [{
    "_timestamp": {
        "order": "desc"
    }
}]
}

If it helps at all I am making a request to an Elasticsearch server, and I am grabbing the JSON data. This cURL request gives me EXACTLY what I need. Here is the C# code that I have now, but I am not sure how to add this JSON data to the GET request. This is also gonna be running in the Unity game engine.

        // Create a request for the URL.        
        request = WebRequest.Create(elk_url);
        // If required by the server, set the credentials.
        request.Credentials = CredentialCache.DefaultCredentials;
        // Get the response.

        response = (HttpWebResponse)request.GetResponse();
        // Display the status.
        Debug.Log(response.StatusDescription);
        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Debug.Log(responseFromServer);
        // Cleanup the streams and the response.
        reader.Close();
        dataStream.Close();
        response.Close();

The above is just from the documentation page, and I am pretty new to HTTP requests in code, so any help would be greatly appreciated.

2 Answers 2

2

I figured it out!

    WebRequest request = WebRequest.Create(elk_url);

    request.ContentType = "application/json";
    request.Method = "POST";
    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(queryString);
    string result = System.Convert.ToBase64String(buffer);
    Stream reqstr = request.GetRequestStream();
    reqstr.Write(buffer, 0, buffer.Length);
    reqstr.Close();

    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Debug.Log(response.StatusDescription);
    dataStream = response.GetResponseStream();
    // Open the stream using a StreamReader for easy access.
    reader = new StreamReader(dataStream);
    // Read the content.
    string responseFromServer = reader.ReadToEnd();
    // Display the content.
    Debug.Log(responseFromServer);
    // Cleanup the streams and the response.
    reader.Close();
    dataStream.Close();
    response.Close();

The Query string is the JSON data in the original post. For those who want to know for the ELK stack, this will give you the JSON data(in string format) from the most recent event. Depending on what beat you use, this could be pretty cool for data visualization.

Sign up to request clarification or add additional context in comments.

1 Comment

how are you passing the "result" from the queryString byte buffer to the request object?
0

Here's how I'd send a Unity WWW-request with POST:

    public IEnumerator SendSomething()
    {
        WWWForm wwwForm = new WWWForm();
        wwwForm.AddField("Parameter_Name", jsonString);

        WWW www = new WWW(url, wwwForm);
        yield return www;

        if (www.error == null)
        {
             Debug.Log("Everything worked!");
        }
        else
        {
             Debug.Log("Something went wrong: " + www.error);
        }
    }

The WWW-class defaults to GET if you don't supply the postData parameter (my wwwForm), so if you want to use GET you could just supply the WWW-class with:

WWW www = new WWW(url + "?" + jsonString);

and skipping the first 2 rows of my method.

Here we're utilizing the IEnumerator to yield return www: which waits for the request to complete until continuing. To utilize the IEnumerator method you invoke it with StartCoroutine(SendSomething()); which will run asyncronized.

1 Comment

I get a "400 Bad Request" when I run that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.