How to test a web api post method which receives a classWeb API 2 POST request simulation in POSTMAN Rest ClientWeb API Post Method not workingHow do I test for an empty JavaScript object?How to make HTTP POST web requestHow do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?WCF vs ASP.NET Web APIHow do I get ASP.NET Web API to return JSON instead of XML using Chrome?How to secure an ASP.NET Web APIHow to pass json POST data to Web API method as an object?Postman Testing send string to Web Api accepting string is nullPOST from Typescript to Web API API, unable to pass a JSON ObjectUnable to test the create webapi method using Postman

Is it moral to remove/hide certain parts of a photo, as a photographer?

Why isn't the new LEGO CV joint available on Bricklink or Brickowl?

Proof of First Difference Property for Fourier Series

Difference between "jail" and "prison" in German

What does Argus Filch specifically do?

How do I safety check that there is no light in Darkroom / Darkbag?

A criterion for finite abelian group to embed into a symmetric group

Meaning of ギャップ in the following sentence

Can an unintentional murderer leave Ir Miklat for Shalosh Regalim?

What is the reason behind water not falling from a bucket at the top of loop?

Why does the friction act on the inward direction when a car makes a turn on a level road?

Partial Fractions: Why does this shortcut method work?

Skipping same old introductions

A wiild aanimal, a cardinal direction, or a place by the water

How does Rust's 128-bit integer `i128` work on a 64-bit system?

Accurately recalling the key - can everyone do it?

Is there any difference between "result in" and "end up with"?

Deflecting lasers with lightsabers

In a KP-K endgame, if the enemy king is in front of the pawn, is it always a draw?

Unlocked Package Dependencies

Is law enforcement responsible for damages made by a search warrant?

Can I say "Gesundheit" if someone is coughing?

HackerRank Implement Queue using two stacks Solution

Detect lightning:recordForm change in mode attribute



How to test a web api post method which receives a class


Web API 2 POST request simulation in POSTMAN Rest ClientWeb API Post Method not workingHow do I test for an empty JavaScript object?How to make HTTP POST web requestHow do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?WCF vs ASP.NET Web APIHow do I get ASP.NET Web API to return JSON instead of XML using Chrome?How to secure an ASP.NET Web APIHow to pass json POST data to Web API method as an object?Postman Testing send string to Web Api accepting string is nullPOST from Typescript to Web API API, unable to pass a JSON ObjectUnable to test the create webapi method using Postman






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have created an ASP.NET web API which has a controller named ImageSaveController. This has an InsertImage method which inserts data into database and is an HttpPost method. This method receives an object of type ImageData as a parameter. The code for the controller and the parameter class are given below:



public class ImageSaveController : ApiController

[HttpPost]
public IHttpActionResult InsertImage(ImageData imageData)

System.Data.SqlClient.SqlConnection conn = null;
try

//Image save to database code here

catch (Exception ex)

return Content(HttpStatusCode.NotModified, ex.Message);

finally

if (conn != null)
conn.Close();

return Content(HttpStatusCode.OK,"");



//ImageData class
public class ImageData

public int Id get; set;
public byte[] ImageValue get; set;



I would like to test it from a client. As you can notice, the ImageValue property of the ImageData class is a byte array. Not sure how to pass the C# class parameter to this method. Ideally I would like to pass the parameter as json and I am not sure how to construct the json for this purpose. I am also not sure whether it could be tested using the chrome app called postman.










share|improve this question
































    1















    I have created an ASP.NET web API which has a controller named ImageSaveController. This has an InsertImage method which inserts data into database and is an HttpPost method. This method receives an object of type ImageData as a parameter. The code for the controller and the parameter class are given below:



    public class ImageSaveController : ApiController

    [HttpPost]
    public IHttpActionResult InsertImage(ImageData imageData)

    System.Data.SqlClient.SqlConnection conn = null;
    try

    //Image save to database code here

    catch (Exception ex)

    return Content(HttpStatusCode.NotModified, ex.Message);

    finally

    if (conn != null)
    conn.Close();

    return Content(HttpStatusCode.OK,"");



    //ImageData class
    public class ImageData

    public int Id get; set;
    public byte[] ImageValue get; set;



    I would like to test it from a client. As you can notice, the ImageValue property of the ImageData class is a byte array. Not sure how to pass the C# class parameter to this method. Ideally I would like to pass the parameter as json and I am not sure how to construct the json for this purpose. I am also not sure whether it could be tested using the chrome app called postman.










    share|improve this question




























      1












      1








      1


      1






      I have created an ASP.NET web API which has a controller named ImageSaveController. This has an InsertImage method which inserts data into database and is an HttpPost method. This method receives an object of type ImageData as a parameter. The code for the controller and the parameter class are given below:



      public class ImageSaveController : ApiController

      [HttpPost]
      public IHttpActionResult InsertImage(ImageData imageData)

      System.Data.SqlClient.SqlConnection conn = null;
      try

      //Image save to database code here

      catch (Exception ex)

      return Content(HttpStatusCode.NotModified, ex.Message);

      finally

      if (conn != null)
      conn.Close();

      return Content(HttpStatusCode.OK,"");



      //ImageData class
      public class ImageData

      public int Id get; set;
      public byte[] ImageValue get; set;



      I would like to test it from a client. As you can notice, the ImageValue property of the ImageData class is a byte array. Not sure how to pass the C# class parameter to this method. Ideally I would like to pass the parameter as json and I am not sure how to construct the json for this purpose. I am also not sure whether it could be tested using the chrome app called postman.










      share|improve this question
















      I have created an ASP.NET web API which has a controller named ImageSaveController. This has an InsertImage method which inserts data into database and is an HttpPost method. This method receives an object of type ImageData as a parameter. The code for the controller and the parameter class are given below:



      public class ImageSaveController : ApiController

      [HttpPost]
      public IHttpActionResult InsertImage(ImageData imageData)

      System.Data.SqlClient.SqlConnection conn = null;
      try

      //Image save to database code here

      catch (Exception ex)

      return Content(HttpStatusCode.NotModified, ex.Message);

      finally

      if (conn != null)
      conn.Close();

      return Content(HttpStatusCode.OK,"");



      //ImageData class
      public class ImageData

      public int Id get; set;
      public byte[] ImageValue get; set;



      I would like to test it from a client. As you can notice, the ImageValue property of the ImageData class is a byte array. Not sure how to pass the C# class parameter to this method. Ideally I would like to pass the parameter as json and I am not sure how to construct the json for this purpose. I am also not sure whether it could be tested using the chrome app called postman.







      c# json asp.net-web-api postman






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 1:22









      Dale Burrell

      4,2825 gold badges27 silver badges57 bronze badges




      4,2825 gold badges27 silver badges57 bronze badges










      asked Jul 17 '16 at 7:10









      MasseyMassey

      4961 gold badge9 silver badges23 bronze badges




      4961 gold badge9 silver badges23 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          2














          Open postman enter your url to the action:

          Add header: Content-Type - application/json.

          In body tab check "raw" (JSON) and type your data.



          POST /api/ImageSave/InsertImage/ HTTP/1.1
          Host: localhost:32378
          Content-Type: application/json
          Cache-Control: no-cache





          "id" : 1,
          "imageValue" : [11,141,123,121]



          source Web API 2 POST request simulation in POSTMAN Rest Client



          If you want to make good tests, the better solution is to write unit tests.






          share|improve this answer


































            2














            This is what I use to do:
            Use a REST client tester like Postman or Fiddler. I use Postman which is an app for Google Chrome.



            For easy construction of JSON you can create a HttpGet method on you controller and return a fake constructed ImageData and call it from Postman. Here you will see the JSON and use that for input to the POST method.



            public class ImageSaveController : ApiController

            public ImageData Get()

            return new ImageData

            // insert test data here
            ;


            [HttpPost]
            public IHttpActionResult InsertImage(ImageData imageData)

            System.Data.SqlClient.SqlConnection conn = null;
            try

            //Image save to database code here

            catch (Exception ex)

            return Content(HttpStatusCode.NotModified, ex.Message);

            finally

            if (conn != null)
            conn.Close();

            return Content(HttpStatusCode.OK,"");







            share|improve this answer



























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38418787%2fhow-to-test-a-web-api-post-method-which-receives-a-class%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Open postman enter your url to the action:

              Add header: Content-Type - application/json.

              In body tab check "raw" (JSON) and type your data.



              POST /api/ImageSave/InsertImage/ HTTP/1.1
              Host: localhost:32378
              Content-Type: application/json
              Cache-Control: no-cache





              "id" : 1,
              "imageValue" : [11,141,123,121]



              source Web API 2 POST request simulation in POSTMAN Rest Client



              If you want to make good tests, the better solution is to write unit tests.






              share|improve this answer































                2














                Open postman enter your url to the action:

                Add header: Content-Type - application/json.

                In body tab check "raw" (JSON) and type your data.



                POST /api/ImageSave/InsertImage/ HTTP/1.1
                Host: localhost:32378
                Content-Type: application/json
                Cache-Control: no-cache





                "id" : 1,
                "imageValue" : [11,141,123,121]



                source Web API 2 POST request simulation in POSTMAN Rest Client



                If you want to make good tests, the better solution is to write unit tests.






                share|improve this answer





























                  2












                  2








                  2







                  Open postman enter your url to the action:

                  Add header: Content-Type - application/json.

                  In body tab check "raw" (JSON) and type your data.



                  POST /api/ImageSave/InsertImage/ HTTP/1.1
                  Host: localhost:32378
                  Content-Type: application/json
                  Cache-Control: no-cache





                  "id" : 1,
                  "imageValue" : [11,141,123,121]



                  source Web API 2 POST request simulation in POSTMAN Rest Client



                  If you want to make good tests, the better solution is to write unit tests.






                  share|improve this answer















                  Open postman enter your url to the action:

                  Add header: Content-Type - application/json.

                  In body tab check "raw" (JSON) and type your data.



                  POST /api/ImageSave/InsertImage/ HTTP/1.1
                  Host: localhost:32378
                  Content-Type: application/json
                  Cache-Control: no-cache





                  "id" : 1,
                  "imageValue" : [11,141,123,121]



                  source Web API 2 POST request simulation in POSTMAN Rest Client



                  If you want to make good tests, the better solution is to write unit tests.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 '17 at 12:33









                  Community

                  11 silver badge




                  11 silver badge










                  answered Jul 17 '16 at 9:17









                  mihkovmihkov

                  7517 silver badges28 bronze badges




                  7517 silver badges28 bronze badges


























                      2














                      This is what I use to do:
                      Use a REST client tester like Postman or Fiddler. I use Postman which is an app for Google Chrome.



                      For easy construction of JSON you can create a HttpGet method on you controller and return a fake constructed ImageData and call it from Postman. Here you will see the JSON and use that for input to the POST method.



                      public class ImageSaveController : ApiController

                      public ImageData Get()

                      return new ImageData

                      // insert test data here
                      ;


                      [HttpPost]
                      public IHttpActionResult InsertImage(ImageData imageData)

                      System.Data.SqlClient.SqlConnection conn = null;
                      try

                      //Image save to database code here

                      catch (Exception ex)

                      return Content(HttpStatusCode.NotModified, ex.Message);

                      finally

                      if (conn != null)
                      conn.Close();

                      return Content(HttpStatusCode.OK,"");







                      share|improve this answer





























                        2














                        This is what I use to do:
                        Use a REST client tester like Postman or Fiddler. I use Postman which is an app for Google Chrome.



                        For easy construction of JSON you can create a HttpGet method on you controller and return a fake constructed ImageData and call it from Postman. Here you will see the JSON and use that for input to the POST method.



                        public class ImageSaveController : ApiController

                        public ImageData Get()

                        return new ImageData

                        // insert test data here
                        ;


                        [HttpPost]
                        public IHttpActionResult InsertImage(ImageData imageData)

                        System.Data.SqlClient.SqlConnection conn = null;
                        try

                        //Image save to database code here

                        catch (Exception ex)

                        return Content(HttpStatusCode.NotModified, ex.Message);

                        finally

                        if (conn != null)
                        conn.Close();

                        return Content(HttpStatusCode.OK,"");







                        share|improve this answer



























                          2












                          2








                          2







                          This is what I use to do:
                          Use a REST client tester like Postman or Fiddler. I use Postman which is an app for Google Chrome.



                          For easy construction of JSON you can create a HttpGet method on you controller and return a fake constructed ImageData and call it from Postman. Here you will see the JSON and use that for input to the POST method.



                          public class ImageSaveController : ApiController

                          public ImageData Get()

                          return new ImageData

                          // insert test data here
                          ;


                          [HttpPost]
                          public IHttpActionResult InsertImage(ImageData imageData)

                          System.Data.SqlClient.SqlConnection conn = null;
                          try

                          //Image save to database code here

                          catch (Exception ex)

                          return Content(HttpStatusCode.NotModified, ex.Message);

                          finally

                          if (conn != null)
                          conn.Close();

                          return Content(HttpStatusCode.OK,"");







                          share|improve this answer













                          This is what I use to do:
                          Use a REST client tester like Postman or Fiddler. I use Postman which is an app for Google Chrome.



                          For easy construction of JSON you can create a HttpGet method on you controller and return a fake constructed ImageData and call it from Postman. Here you will see the JSON and use that for input to the POST method.



                          public class ImageSaveController : ApiController

                          public ImageData Get()

                          return new ImageData

                          // insert test data here
                          ;


                          [HttpPost]
                          public IHttpActionResult InsertImage(ImageData imageData)

                          System.Data.SqlClient.SqlConnection conn = null;
                          try

                          //Image save to database code here

                          catch (Exception ex)

                          return Content(HttpStatusCode.NotModified, ex.Message);

                          finally

                          if (conn != null)
                          conn.Close();

                          return Content(HttpStatusCode.OK,"");








                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jul 17 '16 at 9:18









                          MichaelMichael

                          1,1898 silver badges21 bronze badges




                          1,1898 silver badges21 bronze badges






























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38418787%2fhow-to-test-a-web-api-post-method-which-receives-a-class%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현