How can I access to Web API GET method via using axios?












0















I try a get api call to asp.net core 2.1 webapi from axios



webapi controller



    [Route("api/[controller]")]
[ApiController]
[HttpsRequirement(SslRequirement.Yes)]
public class GHTKController : ControllerBase
{
[HttpGet("GetShippingFee")]
[Produces("application/json")]
public async Task<IActionResult> GetShippingFee([FromBody]GhtkAddress address)
{

return Ok();
}
}


It passed the test with Postman with json(application/json) body




{

"pick_province":"asda"
}



However it failed to run from axios with VueJs and return status 400



import axios from 'axios'
export default {
async getShippingFee(address) {
console.log(address)
const request = await axios.get('/api/ghtk/getshippingfee', {
pick_province: "asda"
})
.then(response => response)
.catch(error => {
console.log(error)
});
return request;
},
}









share|improve this question





























    0















    I try a get api call to asp.net core 2.1 webapi from axios



    webapi controller



        [Route("api/[controller]")]
    [ApiController]
    [HttpsRequirement(SslRequirement.Yes)]
    public class GHTKController : ControllerBase
    {
    [HttpGet("GetShippingFee")]
    [Produces("application/json")]
    public async Task<IActionResult> GetShippingFee([FromBody]GhtkAddress address)
    {

    return Ok();
    }
    }


    It passed the test with Postman with json(application/json) body




    {

    "pick_province":"asda"
    }



    However it failed to run from axios with VueJs and return status 400



    import axios from 'axios'
    export default {
    async getShippingFee(address) {
    console.log(address)
    const request = await axios.get('/api/ghtk/getshippingfee', {
    pick_province: "asda"
    })
    .then(response => response)
    .catch(error => {
    console.log(error)
    });
    return request;
    },
    }









    share|improve this question



























      0












      0








      0








      I try a get api call to asp.net core 2.1 webapi from axios



      webapi controller



          [Route("api/[controller]")]
      [ApiController]
      [HttpsRequirement(SslRequirement.Yes)]
      public class GHTKController : ControllerBase
      {
      [HttpGet("GetShippingFee")]
      [Produces("application/json")]
      public async Task<IActionResult> GetShippingFee([FromBody]GhtkAddress address)
      {

      return Ok();
      }
      }


      It passed the test with Postman with json(application/json) body




      {

      "pick_province":"asda"
      }



      However it failed to run from axios with VueJs and return status 400



      import axios from 'axios'
      export default {
      async getShippingFee(address) {
      console.log(address)
      const request = await axios.get('/api/ghtk/getshippingfee', {
      pick_province: "asda"
      })
      .then(response => response)
      .catch(error => {
      console.log(error)
      });
      return request;
      },
      }









      share|improve this question
















      I try a get api call to asp.net core 2.1 webapi from axios



      webapi controller



          [Route("api/[controller]")]
      [ApiController]
      [HttpsRequirement(SslRequirement.Yes)]
      public class GHTKController : ControllerBase
      {
      [HttpGet("GetShippingFee")]
      [Produces("application/json")]
      public async Task<IActionResult> GetShippingFee([FromBody]GhtkAddress address)
      {

      return Ok();
      }
      }


      It passed the test with Postman with json(application/json) body




      {

      "pick_province":"asda"
      }



      However it failed to run from axios with VueJs and return status 400



      import axios from 'axios'
      export default {
      async getShippingFee(address) {
      console.log(address)
      const request = await axios.get('/api/ghtk/getshippingfee', {
      pick_province: "asda"
      })
      .then(response => response)
      .catch(error => {
      console.log(error)
      });
      return request;
      },
      }






      asp.net-web-api vue.js asp.net-core axios






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 '18 at 17:33









      Foo

      1




      1










      asked Nov 22 '18 at 16:50









      nam vonam vo

      1,28673058




      1,28673058
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Your current action method parameter is decorated with the FromBody attribute. This tells the model binder that it should read the data from the request body and do the mapping during the model binding process. But from your client side code,you are making a GET call, in which your data will be sent in the request URL as query string parameters. If you inspect your network call you can see it like below



          /api/ghtk/getshippingfee?pick_province=asda


          Since you are making a GET call, you should use the FromQuery attribute. The FromQuery attribute tells the model binder to read the data from the request querystring and do the mapping.



          [HttpGet("GetShippingFee")]
          [Produces("application/json")]
          public async Task<IActionResult> GetShippingFee([FromQuery]Profile address)
          {
          return Ok(address);
          }


          Or



          If you want to send a complex object, consider making a POST call from client side code. You need to convert your complex JavaScript object to it's JSON string version and send that as the data for the POST call. Make sure you are specifying application/json as the Content-Type header for the call. You can use the post method on axios.



          const url = '/api/ghtk/getshippingfee';

          const ajaxHheaders = {
          'Content-Type': 'application/json',
          };

          let data = JSON.stringify({
          pick_province: "redmond",
          first_name: "shyju"
          });

          const request = await axios.post(url, data, {
          headers: ajaxHheaders
          })


          The above code will make an Http POST call and send your data in the request body. Since we are making a POST call, make sure your action method is decorated with HttpPost attribute.



          [HttpPost("GetShippingFee")]
          [Produces("application/json")]
          public async Task<IActionResult> GetShippingFee([FromBody]Profile address)
          {
          return Ok(address);
          }





          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%2f53435369%2fhow-can-i-access-to-web-api-get-method-via-using-axios%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Your current action method parameter is decorated with the FromBody attribute. This tells the model binder that it should read the data from the request body and do the mapping during the model binding process. But from your client side code,you are making a GET call, in which your data will be sent in the request URL as query string parameters. If you inspect your network call you can see it like below



            /api/ghtk/getshippingfee?pick_province=asda


            Since you are making a GET call, you should use the FromQuery attribute. The FromQuery attribute tells the model binder to read the data from the request querystring and do the mapping.



            [HttpGet("GetShippingFee")]
            [Produces("application/json")]
            public async Task<IActionResult> GetShippingFee([FromQuery]Profile address)
            {
            return Ok(address);
            }


            Or



            If you want to send a complex object, consider making a POST call from client side code. You need to convert your complex JavaScript object to it's JSON string version and send that as the data for the POST call. Make sure you are specifying application/json as the Content-Type header for the call. You can use the post method on axios.



            const url = '/api/ghtk/getshippingfee';

            const ajaxHheaders = {
            'Content-Type': 'application/json',
            };

            let data = JSON.stringify({
            pick_province: "redmond",
            first_name: "shyju"
            });

            const request = await axios.post(url, data, {
            headers: ajaxHheaders
            })


            The above code will make an Http POST call and send your data in the request body. Since we are making a POST call, make sure your action method is decorated with HttpPost attribute.



            [HttpPost("GetShippingFee")]
            [Produces("application/json")]
            public async Task<IActionResult> GetShippingFee([FromBody]Profile address)
            {
            return Ok(address);
            }





            share|improve this answer






























              0














              Your current action method parameter is decorated with the FromBody attribute. This tells the model binder that it should read the data from the request body and do the mapping during the model binding process. But from your client side code,you are making a GET call, in which your data will be sent in the request URL as query string parameters. If you inspect your network call you can see it like below



              /api/ghtk/getshippingfee?pick_province=asda


              Since you are making a GET call, you should use the FromQuery attribute. The FromQuery attribute tells the model binder to read the data from the request querystring and do the mapping.



              [HttpGet("GetShippingFee")]
              [Produces("application/json")]
              public async Task<IActionResult> GetShippingFee([FromQuery]Profile address)
              {
              return Ok(address);
              }


              Or



              If you want to send a complex object, consider making a POST call from client side code. You need to convert your complex JavaScript object to it's JSON string version and send that as the data for the POST call. Make sure you are specifying application/json as the Content-Type header for the call. You can use the post method on axios.



              const url = '/api/ghtk/getshippingfee';

              const ajaxHheaders = {
              'Content-Type': 'application/json',
              };

              let data = JSON.stringify({
              pick_province: "redmond",
              first_name: "shyju"
              });

              const request = await axios.post(url, data, {
              headers: ajaxHheaders
              })


              The above code will make an Http POST call and send your data in the request body. Since we are making a POST call, make sure your action method is decorated with HttpPost attribute.



              [HttpPost("GetShippingFee")]
              [Produces("application/json")]
              public async Task<IActionResult> GetShippingFee([FromBody]Profile address)
              {
              return Ok(address);
              }





              share|improve this answer




























                0












                0








                0







                Your current action method parameter is decorated with the FromBody attribute. This tells the model binder that it should read the data from the request body and do the mapping during the model binding process. But from your client side code,you are making a GET call, in which your data will be sent in the request URL as query string parameters. If you inspect your network call you can see it like below



                /api/ghtk/getshippingfee?pick_province=asda


                Since you are making a GET call, you should use the FromQuery attribute. The FromQuery attribute tells the model binder to read the data from the request querystring and do the mapping.



                [HttpGet("GetShippingFee")]
                [Produces("application/json")]
                public async Task<IActionResult> GetShippingFee([FromQuery]Profile address)
                {
                return Ok(address);
                }


                Or



                If you want to send a complex object, consider making a POST call from client side code. You need to convert your complex JavaScript object to it's JSON string version and send that as the data for the POST call. Make sure you are specifying application/json as the Content-Type header for the call. You can use the post method on axios.



                const url = '/api/ghtk/getshippingfee';

                const ajaxHheaders = {
                'Content-Type': 'application/json',
                };

                let data = JSON.stringify({
                pick_province: "redmond",
                first_name: "shyju"
                });

                const request = await axios.post(url, data, {
                headers: ajaxHheaders
                })


                The above code will make an Http POST call and send your data in the request body. Since we are making a POST call, make sure your action method is decorated with HttpPost attribute.



                [HttpPost("GetShippingFee")]
                [Produces("application/json")]
                public async Task<IActionResult> GetShippingFee([FromBody]Profile address)
                {
                return Ok(address);
                }





                share|improve this answer















                Your current action method parameter is decorated with the FromBody attribute. This tells the model binder that it should read the data from the request body and do the mapping during the model binding process. But from your client side code,you are making a GET call, in which your data will be sent in the request URL as query string parameters. If you inspect your network call you can see it like below



                /api/ghtk/getshippingfee?pick_province=asda


                Since you are making a GET call, you should use the FromQuery attribute. The FromQuery attribute tells the model binder to read the data from the request querystring and do the mapping.



                [HttpGet("GetShippingFee")]
                [Produces("application/json")]
                public async Task<IActionResult> GetShippingFee([FromQuery]Profile address)
                {
                return Ok(address);
                }


                Or



                If you want to send a complex object, consider making a POST call from client side code. You need to convert your complex JavaScript object to it's JSON string version and send that as the data for the POST call. Make sure you are specifying application/json as the Content-Type header for the call. You can use the post method on axios.



                const url = '/api/ghtk/getshippingfee';

                const ajaxHheaders = {
                'Content-Type': 'application/json',
                };

                let data = JSON.stringify({
                pick_province: "redmond",
                first_name: "shyju"
                });

                const request = await axios.post(url, data, {
                headers: ajaxHheaders
                })


                The above code will make an Http POST call and send your data in the request body. Since we are making a POST call, make sure your action method is decorated with HttpPost attribute.



                [HttpPost("GetShippingFee")]
                [Produces("application/json")]
                public async Task<IActionResult> GetShippingFee([FromBody]Profile address)
                {
                return Ok(address);
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 '18 at 19:10

























                answered Nov 22 '18 at 18:07









                ShyjuShyju

                146k87333442




                146k87333442
































                    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%2f53435369%2fhow-can-i-access-to-web-api-get-method-via-using-axios%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

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]