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",

asked Mar 22, 2013 at 16:19
1
  • if it's a restful json service it might be expecting username and password as JSON in the body rather than in the url Commented Mar 22, 2013 at 16:37

2 Answers 2

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()) { }
answered Mar 22, 2013 at 17:16
3
  • 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\" }"); Commented Mar 23, 2013 at 1:40
  • but it's still giving me the same problem. Commented Mar 23, 2013 at 1:41
  • Did you explicitly set the content-type to "application/json"? Commented Mar 27, 2013 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>");
answered Mar 22, 2013 at 16:42
1
  • I will like to send data in json web service. Commented Mar 23, 2013 at 1:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.