How can I access to Web API GET method via using axios?
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
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
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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
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
asp.net-web-api vue.js asp.net-core axios
edited Nov 22 at 17:33
Foo
1
1
asked Nov 22 at 16:50
nam vo
1,26872958
1,26872958
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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);
}
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
up vote
0
down vote
accepted
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);
}
add a comment |
up vote
0
down vote
accepted
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);
}
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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);
}
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);
}
edited Nov 22 at 19:10
answered Nov 22 at 18:07
Shyju
143k87329434
143k87329434
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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