'C# post JSON data to REST API

I am new to REST API JSON but have been able to consume a few API with asp.net. Here is the problem I am currently facing.

I have been able to send JSON data to an API using this method.

public void PostData()
{
    string sName = sysName.Text;
    string sDescrip = sysDescrip.Text;
    var httpclient = new HttpClient();
    httpclient.BaseAddress = new Uri("http://33.248.292.99:8094/bizzdesk/sysconfig/api/");
    var sys = new Bizsys(){name= sName, description= sDescrip};
    httpclient.PostAsJsonAsync("system", sys);
}

it work just fine.

Now I modify the code in order to accommodate more values thus:

var httpclient = new HttpClient();
// ArrayList paramList = new ArrayList();
httpclient.BaseAddress = new Uri("http://179.683.197.115:9091/tua-slte/api/organisations/");
var org = new Organisation() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=8, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017"};
var bas = new Basic() { id=1, username = "inno", password = "123", firstName="Innocent", lastName="Ujata", email = "[email protected]", mobile = "123456", photo="10201001", loginIp="127.0.0.1", loginDate="10/04/2017", locked=false, organisation = org, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", admin=true};
var org2 = new Organisation2() { id=1, name = "inno", abbreviation = "abx", type="school", sort = 7, isShow = true, createdBy=17, createdDate = "10/04/2017", editedBy = 09, editDate="11/04/2017"};
var hq = new HeadQuarter() { zonId=09, organisation = org2, zonCode = "123", zonName = "Abuja", zonAddress = "123456", zonCity = "Abuja", zonPostalCode = "120076", zonEmail = "answers", zonPhoneNumber = "0908765", zonFaxNumber = "1212", zonState = "FCT", createdBy=17, createdDate = "10/04/2017", editedBy = 11, editDate="11/04/2017", version=1};
var examp = new RootObject() {basic=bas, headQuarter=hq  };

var status = httpclient.PostAsJsonAsync("register", examp);
return status;

It keep returning this:

Id = 19, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

I hard coded the data to see it work first before making it dynamic.

I have tried using await async method too the result is the same. all the questions and answers I have seen here are not similar.

What does that mean, and what am I getting wrong?



Solution 1:[1]

You are calling an async method, but not awaiting it's results.

Change:

var status = httpclient.PostAsJsonAsync("register", examp);

to

var status = await httpclient.PostAsJsonAsync("register", examp);

Solution 2:[2]

Extension method PostAsJsonAsync returns Task<HttpResponseMessage>. You grab this task and see it's details. If you want to return status code of completed request, you should await for task completion and then grab status code from response message:

 private async Task<HttpStatusCode> PostSomethingAsync()
 {
     ...
     var response = await httpclient.PostAsJsonAsync("register", examp);
     return response.StatusCode;
 }

Or you can synchronously wait for request completion:

 private HttpStatusCode PostSomething()
 {
     ...
     var response = httpclient.PostAsJsonAsync("register", examp).Result;
     return response.StatusCode;
 }    

Solution 3:[3]

That's cause you are not awaiting the call like

var status = await httpclient.PostAsJsonAsync("register", examp);

Or, use ConfigureAwait()

var status = httpclient.PostAsJsonAsync("register", examp).ConfigureAwait(false);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 paul
Solution 2 Sergey Berezovskiy
Solution 3 Rahul