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",
-
if it's a restful json service it might be expecting username and password as JSON in the body rather than in the urlNeil Thompson– Neil Thompson2013年03月22日 16:37:14 +00:00Commented Mar 22, 2013 at 16:37
2 Answers 2
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– rosepalette2013年03月23日 01:40:30 +00:00Commented Mar 23, 2013 at 1:40
-
but it's still giving me the same problem.rosepalette– rosepalette2013年03月23日 01:41:21 +00:00Commented Mar 23, 2013 at 1:41
-
Did you explicitly set the content-type to "application/json"?Jonathan Moosekian– Jonathan Moosekian2013年03月27日 13:44:16 +00:00Commented Mar 27, 2013 at 13:44
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>");
-
I will like to send data in json web service.rosepalette– rosepalette2013年03月23日 01:58:55 +00:00Commented Mar 23, 2013 at 1:58