0

How can you serialize a json object and pass it on to an api call, like the one in the example posted as an answer here Call web APIs in C# using .NET framework 3.5

System.Net.WebClient client = new System.Net.WebClient();
 client.Headers.Add("content-type", "application/json");//set your header here, you can add multiple headers
 string s = Encoding.ASCII.GetString(client.UploadData("http://localhost:1111/Service.svc/SignIn", "POST", Encoding.Default.GetBytes("{\"EmailId\": \"admin@admin.com\",\"Password\": \"pass#123\"}")));

In Postman I would just do this

 var client = new RestClient("");
 var request = new RestRequest(Method.POST);
 request.JsonSerializer = new RestSharpJsonNetSerializer();
 request.AddJsonBody(JsonObject);

However, as Postman is not supported in .net framework 3.5, I have to use System.Net.WebClient.

miniHessel
  • 778
  • 4
  • 15
  • 39

1 Answers1

1

You can do what you want with WebClient (and Json.NET package) like this:

var yourObject = new YourObject {
    Email = "email",
    Password = "password"
};
string s = client.UploadString("http://localhost:1111/Service.svc/SignIn","POST", JsonConvert.SerializeObject(yourObject));
Evk
  • 98,527
  • 8
  • 141
  • 191
  • @miniHessel ah sorry I missed about .NET 3.5. Maybe there are indeed not much options there, not sure. – Evk Nov 16 '17 at 08:35
  • I think there isn't. Do you know how to check for status code like this IRestResponse response = client.Execute(request); if (response.StatusCode == System.Net.HttpStatusCode.OK) return true; with WebClient? – miniHessel Nov 16 '17 at 08:36
  • Seems there is no direct way, but you can check this: https://stackoverflow.com/q/3574659/5311735. Though if response code is not successful (not in 2xx range) - it should throw an exception. – Evk Nov 16 '17 at 08:41