smartsoldier
smartsoldier

Reputation: 153

Json C# add in the variable to the json string

I want to add my string username and password into the string jsonData however there seems to be an error.

public async Task<string> authLogin(string username, string password)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri("http://172.20.129.193/");

        string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}";

        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync("NYPStudentLifeService/api/student/login",content);

        string result = await response.Content.ReadAsStringAsync();
        return result;
    }

Upvotes: 1

Views: 5618

Answers (2)

smartsoldier
smartsoldier

Reputation: 153

The way you are using is wrong try out this method. Also the password order was wrong as mentioned above. ^

Instead of:

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}";

You can try out this:

string jsonData = @"{'AdminNo':'"+username+"','Password':'"+password+"'}";

Upvotes: 2

AbelMorgan
AbelMorgan

Reputation: 411

Use RestSharp for REST API calls and NewtoSoft to serialize your objects to JSONs. Your code is obsolete.

http://restsharp.org/

http://www.newtonsoft.com/json

By the way... your error is the order of "password"

You have

string jsonData = @"{""AdminNo"" : """+username+""", """+password+""" : ""password""}";

Should be

string jsonData = @"{""AdminNo"" : """+username+""", ""password"" : """+password+"""}";

Upvotes: 0

Related Questions