1

I am trying to send dictionary using POST in C# to a django server. The server receives the request and acknowledges with 200 message, however it receives an empty Multi Value Dictionary.

Here is the client sending POST request.

public class Hello1
{
    public static void Main()
    {
        Console.WriteLine("Request Initiating");

        using (var wb = new WebClient())
        {
            var data = new NameValueCollection();


            data["key1"] = "test";
            data["key2"] = "TEST2";
            data["key3"] = "NA";

            string url = "http://10.34.150.153:8000/key_detect/detect/";
            var response = wb.UploadValues(url, "POST", data);
            string result = System.Text.Encoding.UTF8.GetString(response);

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

1 Answers1

0

See this post: POSTing JSON to URL via WebClient in C#

You need to serialize your data to JSON before you upload it.

var data = new NameValueCollection();
data["key1"] = "test";
data["key2"] = "TEST2";
data["key3"] = "NA";

using (var client = new WebClient())
{
   var dataString = JsonConvert.SerializeObject(data);
   client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
   client.UploadString(new Uri("http://www.contoso.com/1.0/service/action"), "POST", dataString);
}

Prerequisites: Json.NET library

Community
  • 1
  • 1
P. Roe
  • 2,077
  • 1
  • 16
  • 23