0

I am posting a JSON request to a web api using the WebClient.UploadString method. The JSON string is created from parsing a text file into a JSON formatted string. How do I define a parameter in the request body working with my code below? It looks like I just add it to the end of my jsonPOSTString string variable?

string result = "";
    using (var client = new WebClient())
    {
        result = client.UploadString(url, "POST", jsonPOSTString);
    }
kyle_13
  • 1,173
  • 6
  • 25
  • 47
  • 1
    Your JSON *is* the request body. Obviously you cannot modify it arbitrarily, it must still be JSON. Are you sure you have the correct idea of what the API expects from you? – Jon Feb 13 '14 at 14:07
  • The API expects a JSON formatted request with one parameter defined. I don't know how to add the parameter to the request body. I'm new to apis, sorry if I'm missing something. – kyle_13 Feb 13 '14 at 14:20

1 Answers1

1

You aren't telling what this code does, but I think you'll get an error from the server because you don't supply the content-type:

string result;
using (var client = new WebClient())
{
    client.Headers.Add("Content-Type","application/json");
    result = client.UploadString(url, "POST", jsonPOSTString);
}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272