Client Universall App WebApi
-
Hello I am trying to load data into a web API with a Universal app, but testing with Fiddler will not come out the data in the correct format This should be the result:
{"IDBusta":4,"NomeCliente":"Fior","Prezzo":9.0}]
instead it comes so:
alue=%7B%22IDBusta%22%3A22%2C%22NomeCliente%22%3A%22fff%22%2C%22Prezzo%22%3A22.0%7D
this is the class on question:
public sealed partial class CreateOrUpdate : Page
{
bool? create { get; set; }
public CreateOrUpdate()
{
this.InitializeComponent();
}protected override void OnNavigatedTo(NavigationEventArgs e) { // Must be a request to update // Because if the "create" is null or true, // it is to denote that the request is to create a new object if (e.Parameter as bool? == false) { create = e.Parameter as bool?; var busta = App.ActiveBusta; TextBoxIdBusta.Text = busta.IDBusta.ToString(); TextBoxNome.Text = busta.NomeCliente.ToString(); TextBoxPrezzo.Text = busta.Prezzo.ToString(); actionButton.Content = "Update"; } } private void Button\_Click(object sender, RoutedEventArgs e) { if ((sender as Button).Content.ToString() == "Home") { // Go to default page this.Frame.Navigate(typeof(MainPage)); return; // and cancel the event. } // Otherwise var busta = new Busta { IDBusta = Convert.ToInt32(TextBoxIdBusta.Text), NomeCliente = TextBoxNome.Text.ToString(), Prezzo = float.Parse(TextBoxPrezzo.Text), }; using (var client = new HttpClient()) { var content = JsonConvert.SerializeObject(busta); if (create == null || create == true) { // Send a POST Task task = Task.Run(async () => { var data = new HttpFormUrlEncodedContent( new Dictionary { \["value"\] = content } ); await client.PostAsync(App.BaseUri, data);
-
Hello I am trying to load data into a web API with a Universal app, but testing with Fiddler will not come out the data in the correct format This should be the result:
{"IDBusta":4,"NomeCliente":"Fior","Prezzo":9.0}]
instead it comes so:
alue=%7B%22IDBusta%22%3A22%2C%22NomeCliente%22%3A%22fff%22%2C%22Prezzo%22%3A22.0%7D
this is the class on question:
public sealed partial class CreateOrUpdate : Page
{
bool? create { get; set; }
public CreateOrUpdate()
{
this.InitializeComponent();
}protected override void OnNavigatedTo(NavigationEventArgs e) { // Must be a request to update // Because if the "create" is null or true, // it is to denote that the request is to create a new object if (e.Parameter as bool? == false) { create = e.Parameter as bool?; var busta = App.ActiveBusta; TextBoxIdBusta.Text = busta.IDBusta.ToString(); TextBoxNome.Text = busta.NomeCliente.ToString(); TextBoxPrezzo.Text = busta.Prezzo.ToString(); actionButton.Content = "Update"; } } private void Button\_Click(object sender, RoutedEventArgs e) { if ((sender as Button).Content.ToString() == "Home") { // Go to default page this.Frame.Navigate(typeof(MainPage)); return; // and cancel the event. } // Otherwise var busta = new Busta { IDBusta = Convert.ToInt32(TextBoxIdBusta.Text), NomeCliente = TextBoxNome.Text.ToString(), Prezzo = float.Parse(TextBoxPrezzo.Text), }; using (var client = new HttpClient()) { var content = JsonConvert.SerializeObject(busta); if (create == null || create == true) { // Send a POST Task task = Task.Run(async () => { var data = new HttpFormUrlEncodedContent( new Dictionary { \["value"\] = content } ); await client.PostAsync(App.BaseUri, data);
The value you're seeing in Fiddler is the value you're sending.
var data = new HttpFormUrlEncodedContent(
new Dictionary
{
["value"] = content
}
);You're telling the code to post a URL-encoded form, with a single key called "value", whose value is the URL-encoded JSON string representing your content. That results in a body containing:
value=%7B%22IDBusta%22%3A22%2C%22NomeCliente%22%3A%22fff%22%2C%22Prezzo%22%3A22.0%7D
If you decode the value, you get:
{"IDBusta":22,"NomeCliente":"fff","Prezzo":22.0}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The value you're seeing in Fiddler is the value you're sending.
var data = new HttpFormUrlEncodedContent(
new Dictionary
{
["value"] = content
}
);You're telling the code to post a URL-encoded form, with a single key called "value", whose value is the URL-encoded JSON string representing your content. That results in a body containing:
value=%7B%22IDBusta%22%3A22%2C%22NomeCliente%22%3A%22fff%22%2C%22Prezzo%22%3A22.0%7D
If you decode the value, you get:
{"IDBusta":22,"NomeCliente":"fff","Prezzo":22.0}
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
This is the mehod Web api "POST":
// POST: api/Bustas
[ResponseType(typeof(Busta))]
public IHttpActionResult PostBusta(Busta busta)
{
if (!ModelState.IsValid)
{return BadRequest(ModelState); } db.Bustas.Add(busta); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = busta.IDBusta }, busta); }
putting a breakpoint on this line
db.Bustas.Add(busta);
this result is (IDbusta = 3 , NomeCliente = nulll, prezzo = 0)