0

I am a beginner in programming and have recently learnt and used http uri to get data and then parse json string from the stream using C# in SSIS and was able to load the data into sql server database.

Below is the sample code I used..

System.Uri uri = new Uri("API URL");
WebRequest webRequest = HttpWebRequest.Create(uri);
HttpWebRequest httpWebRequest = (HttpWebRequest)webRequest;
NetworkCredential networkCredential = new NetworkCredential("LOGIN","Password");
credentialCache.Add(uri,"Basic",networkCredential);
WebResponse webResponse = webRequest.GetResponse();
...

However I am trying to setup the same type of connection for another api which uses POST method.

The query looks something like.. URL + JSON API query which is similar to..

{"JSON" : {
"name": "Dataset",
"ColumnSelect": ["Name","Age","Email"],
"sort":["Name"],
"filterList": [
{
"Attribute": "Age",
"Operator": ">",
"Value": ["25"]
}],"returnObject"

I am not sure how this can be used to query the data and load data to sql like the http request. Can someone please advise me on the right direction to achieve this. Thank you for all your help.

Nat85
  • 27
  • 2
  • 8

1 Answers1

0

I'm not %100 sure what your scenario is, but as far as I understand you are trying to get data from some server, and store the data in your database.

You have achieved it using HTTP GET, but this time you need to do it with POST.

The basic difference with GET and POST is, GET is used to query the existing data, but in POST, you are delivering something to the server. (of course there are more differences, check this link: https://www.w3schools.com/tags/ref_httpmethods.asp)

This can be done easily:

  1. Prepare your POST request, you need to have a content for your parameters.
  2. Create classes for your incoming response string, from your JSON posted:

    public class JSON
    {
        public string name { get; set; }
    
        public string[] ColumnSelect { get; set; }
    
        public string[] sort { get; set; }
    
        public filterList filterList { get; set; }
    }
    
    public class filterList
    {
        public string Attribute { get; set; }
    
        public string Operator { get; set; }
    
        public string[] Value { get; set; }
    }
    
  3. When you have the response string (stringified JSON), deserialize it via JSON.NET.

  4. Now you have them as .NET objects. You can use Entity Framework to commit them to the database.

I can edit my answer if you need advice on any steps.

Mithgroth
  • 1,114
  • 2
  • 13
  • 23
  • Thank you for your response.. when u say "but in POST, you are delivering something to the server". Do you mean we have to send this query through a url and get the response. Sorry for being a little slow here. Is it possible to get an example to understand the logic and try it for building per my requirement. Thank you – Nat85 Dec 03 '17 at 14:51
  • Check the link I provided. By "in POST, you are delivering something to the server" I mean, you need to send post data with your request. Check `data` and `stream.Write(data, 0, data.Length);` lines. If the answer above helped you, please mark it as answer. – Mithgroth Dec 03 '17 at 15:21