Reputation: 39190
I'm using the following code to post a message to the server.
String credentials="[email protected]&pass=abc123";
using (HttpClient client = new HttpClient())
{
HttpResponseMessage message = await client.PostAsync(
url, new StringContent(credentials));
result = await message.Content.ReadAsStringAsync();
}
However, the server responds with an error message saying that the email or pass isn't there.
{\"status\":\"error\",\"message\":\"Email or pass is missing\"}
Now, I can see it's lying but what can be done about it? Am I not providing the credentials as post data (the equivalent of "params" that can be seen if invoked from URL line in FF, as the image below shows?
I also noticed that when I access the RequestMessage like below, I get the URL but no credentials having been sent. What stupid thing am I missing?!
result = message.RequestMessage.ToString();
This result gives me the following string.
Method: POST, RequestUri: 'https://server.com/gettoken', Version: 1.1, Content: System.Net.Http.StringContent, Headers:\u000d\u000a{\u000d\u000a Content-Type: text/plain; charset=utf-8\u000d\u000a Content-Length: 56\u000d\u000a}
Upvotes: 0
Views: 4985
Reputation: 70602
You should use the FormUrlEncodedContent
to pass your POST data, instead of a StringContent
object.
var credentials = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("email", "[email protected]"),
new KeyValuePair<string, string>("pass", "abc123")
});
using (HttpClient client = new HttpClient())
{
HttpResponseMessage message = await client.PostAsync(
url, credentials);
result = await message.Content.ReadAsStringAsync();
}
Upvotes: 5