I am trying to post this JSON data string to my php site:
{
"tag":"login"
"email":"email@email.com"
"password":"P@ssw0rd"
}
this is my C# code that I have:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost/login/");
string link = "http://localhost/login/";
UserCredentials cred = new UserCredentials(email.Text, pass.Password.ToString());
var data = new Dictionary<string, List<UserCredentials>>();
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
HttpResponseMessage re = await client.PostAsync(link, new StringContent(json));
User credentials class:
public class UserCredentials
{
public UserCredentials(string user, string pass)
{
User = user;
Pass = pass;
Tag = "login";
}
internal string User;
internal string Pass;
internal string Tag;
}
in my php script :
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
echo "got tags";
else
echo "didn't get anything";
?>
does anyone know how I am supposed to send the tags over the postasync method? I am getting ‘didn’t get anything’ .. please help
First, fix your UserCredentials class. Make your memebers public, not internal.
Second, serialize only your credentials.
You will see in the Output window something like this:
Third, replace
StringContentwithFormUrlEncodedContentIf you are using PHP
$_POST, then you must send something likekey1=value1&key2=value2in your HTTP request. This is calledx-www-form-urlencodedand HttpClient contains the right class for this:FormUrlEncodedContent.FormUrlEncodedContentreceives a dictionary of key/value pairs.This will be sent through the connection:
Fourth, use your data in PHP
As you see above, data is encoded with a lot of percent signs. PHP automatically decodes the data for you, so you do not need to worry about that.
To convert the credentials from json to an array, you may use json_decode