Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows API
  4. Client Universall App WebApi

Client Universall App WebApi

Scheduled Pinned Locked Moved Windows API
questiontestingbeta-testingjsonannouncement
3 Posts 2 Posters 19 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paolo Mazzon
    wrote on last edited by
    #1

    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);
    
    Richard DeemingR 1 Reply Last reply
    0
    • P Paolo Mazzon

      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);
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        P Offline
        P Offline
        Paolo Mazzon
        wrote on last edited by
        #3

        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)

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups