locked
Passing JSON file as input to Predictive Model while making API call POST Method to get output RRS feed

  • Question

  • Hi,

    There is a predictive model and we can call that model with API. I have to Pass JSON file as INPUT to get the prediction. How can do I that in C# Console Application. I am fairly new to C# programming. 

    Here is CURL command I used to invoke the model. I need to that with Console application.

    curl -k --user xxxxx-X POST -d @C:\input.json "https://middd-int:90999/mddp-model-broker/predict?model=iris"

    this give me result as :

    {"result":"virginica","score":2}

    Here is the content of JSON input file for testing:

    {
      "INPUT": {
        "PARAMS": {
          "Sepal.Length" : 7.9,
          "Sepal.Width" : 3.8,
          "Petal.Length" : 6.4,
          "Petal.Width" : 2.0
        }
    }
    }

    Thank you



    • Edited by 1234alex Thursday, November 14, 2019 4:45 PM
    Thursday, November 14, 2019 2:01 PM

Answers

  • You would probably need to use HTTPClinet(), which is the defacto and there are plenty of examples on how to use HTTPClient by using Bing or Google.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8

    http://zetcode.com/csharp/httpclient/

    https://johnthiriet.com/efficient-post-calls/#

    https://www.c-sharpcorner.com/blogs/get-the-json-in-console-application-using-httpclient

    • Marked as answer by 1234alex Friday, November 15, 2019 9:33 PM
    Thursday, November 14, 2019 11:38 PM

All replies

  • You would probably need to use HTTPClinet(), which is the defacto and there are plenty of examples on how to use HTTPClient by using Bing or Google.

    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8

    http://zetcode.com/csharp/httpclient/

    https://johnthiriet.com/efficient-post-calls/#

    https://www.c-sharpcorner.com/blogs/get-the-json-in-console-application-using-httpclient

    • Marked as answer by 1234alex Friday, November 15, 2019 9:33 PM
    Thursday, November 14, 2019 11:38 PM
  • Hi 1234alex,

    Thank you for posting here.

    For your question, you want to post a json data to Web API and get something back.

    As DA924x said, HTTPClient is an effective way, this is a sample code.
            static readonly HttpClient client = new HttpClient();
    
            static async Task  Main()
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("http://www.uber.com");
                    var content = new StringContent("json", Encoding.UTF8, "application/json");
                    var result = await client.PostAsync("Method Address", content);
                    string resultContent = await result.Content.ReadAsStringAsync();
                    Console.WriteLine(resultContent);
    
                    Console.WriteLine("Press any key to continue...");
                    Console.ReadKey();
                }
            }
    

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 15, 2019 8:11 AM