0

How do i post data in json? As I keep receiving the error message that I have not passed the parameter. This is my c# code:

Firing the button: url = szAPIURL + url;

WebClient postWithParamsClient = new WebClient();
postWithParamsClient.UploadStringCompleted += 
  new UploadStringCompletedEventHandler(postWithParamsClient_UploadStringCompleted);

postWithParamsClient.Headers["Content-Length"] = postdata.Length.ToString();
postWithParamsClient.UploadStringAsync(new Uri(url), 
                                       "POST", 
                                       "?username=name123&password=pass123");

private void postWithParamsClient_UploadStringCompleted(object sender, 
                                                UploadStringCompletedEventArgs e)
{
    if (e.Error == null)
      MessageBox.Show("WebClient: " + e.Result);
    else
      MessageBox.Show("WebClient: " + e.Error.Message);
}

This is what I receive from the call :

[{"error_code":2,"error_messages":["You must specify login user name and password"],"tokenid":"","userid":0}]

This is the original ajax api:

var msgData = {};
msgData['username'] = szUserName;
msgData['password'] = szEncryptedPassword;

$.ajax({
url: szAPIURL + "Authenticate",
type: 'POST',
// data need to post tp the server.
//data: JSON.stringify({data:"test"}),
data: msgData,
/*dataType: "jsonp",*/
dataType: "json",

rosepalette
  • 186
  • 2
  • 19

2 Answers2

1

There are a few things I needed to do when doing this same thing (albeit I was using UploadString and not UploadStringAsync).

First, I needed to add this to the Headers collection:

postWithParamsClient.Headers.Add("Content-Type", "application/json");

Then like Jevgenij mentions, you have to actually send the data as a JSON string, like this:

postWithParamsClient.UploadStringAsync(new Uri(url), 
                                   "POST", 
                                   "{ \"username\": \"name123\", \"password\": \"pass123\" }");

Update: If you don't pass the second parameter "POST", the method will automatically submit it as a POST for you.

Also, as an aside, since WebClient is IDisposable, I'd recommend using it in a using statement like:

using (postWithParamsClient = new WebClient()) { }
  • string url = "Authenticate"; url = szAPIURL + url; WebClient postWithParamsClient = new WebClient(); postWithParamsClient.UploadStringCompleted += new UploadStringCompletedEventHandler(postWithParamsClient_UploadStringCompleted); postWithParamsClient.UploadStringAsync(new Uri(url), "POST", "{ \"username\": \"name123\", \"password\": \"pass123\" }"); – rosepalette Mar 23 '13 at 01:40
  • but it's still giving me the same problem. – rosepalette Mar 23 '13 at 01:41
  • Did you explicitly set the content-type to "application/json"? – Jonathan Moosekian Mar 27 '13 at 13:44
0

Not sure, but the third parameter of UploadStringAsync is actual data, which you want to send. Probably you want to put your "?username=name123&password=pass123" into the url?

Something like this:

client.UploadStringAsync(new Uri(string.Format("{0}?{1}", url, "username=name123&password=pass123")), "POST", "<your data>");
Jevgenij Nekrasov
  • 2,690
  • 3
  • 30
  • 51