Submit body định dạng JSON bằng axios

Xin chào !
Mình gặp vấn đề khi truyền data lên server bằng axios, mình xác định được lỗi là do Content-Type:application/json ở phần header (mã lỗi 400415).

Mình test bằng POSTMAN server vẫn hoạt động bình thương ( status code 200OK)
Đây là phần header của request


Đây là phần body

Đây là code Axios của mình

 axios({
        method: 'GET',
        url: 'https://localhost:44376/api/Fee',
        headers: {
            'Content-Type': 'application/json'
        },
        data: {
            from_district_id: 1454,
            service_id: 53320,
            to_district_id: 1452,
            to_ward_code: 21012,
            height: 50,
            length: 20,
            weight: 200,
            width: 20,
            insurance_fee: 10000
        }
    }).then(res => {
        console.log(res)
    }).catch(err => {
        console.log(err)
    })

Bị lỗi

xhr.js:177 GET https://localhost:44376/api/Fee 400
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:62)

JSON.stringify data

let data= JSON.stringify({
    from_district_id: 1454,
    service_id: 53320,
    to_district_id: 1452,
    to_ward_code: 21012,
    height: 50,
    length: 20,
    weight: 200,
    width: 20,
    insurance_fee: 10000
});

    axios.get('https://localhost:44376/api/Fee', data,
    {
        headers : {
            'Content-Type': 'application/json'
        }
    }
    ).then(res => {
        console.log(res)
    }).catch(err => {
        console.log(err)
    })
};

Bị lỗi như này :

TypeError: Cannot use 'in' operator to search for 'validateStatus' in {"from_district_id":1454,"service_id":53320,"to_district_id":1452,"to_ward_code":21012,"height":50,"length":20,"weight":200,"width":20,"insurance_fee":10000}

Cảm ơn các bạn!

#lep

Nếu đã xài postman rồi thì sao không xem code của mình với postman gen ra khác nhau chỗ nào
cuộc sống thật dễ dàng


4 Likes

Đây là code do postman generate giống như trong ảnh của bạn (khác url) nhưng vẫn lỗi 400

var axios = require('axios');
var data = JSON.stringify({
  "from_district_id": 1454,
  "service_id": 53320,
  "to_district_id": 1452,
  "to_ward_code": 21012,
  "height": 50,
  "length": 20,
  "weight": 200,
  "width": 20,
  "insurance_fee": 10000
});

var config = {
  method: 'get',
  url: 'https://localhost:44376/api/Fee',
  headers: { 
    'Content-Type': 'application/json'
  },
  data : data
};

axios(config)
.then(function (response) {
  console.log(JSON.stringify(response.data));
})
.catch(function (error) {
  console.log(error);
});

Không biết có phải lỗi ở phía server không?

Đây là code ASP.NET của server

[HttpGet]
    public async Task<IActionResult> GetFee([FromBody] ProductFeeShipping info)
    {

        var client = _clientFactory.CreateClient("GiaoHangNhanhProduction");
        var fee = await client.PostAsJsonAsync("v2/shipping-order/fee", info);
        string result = fee.Content.ReadAsStringAsync().Result;
        return Ok(result);
    }

Đây là Class tương ứng với JSON data

 public class ProductFeeShipping
    {

        public int from_district_id { set; get; }
        public int service_id = 53320;
        public int to_district_id { set; get; }
        public int to_ward_code { set; get; }
        public int length { set; get; }
        public int weight { set; get; }
        public int width { set; get; }
        public int insurance_fee { set; get; }
    }

Nhưng tại sao POSTMAN không lỗi?

vậy [request mà server nhận được] khi chạy đoạn code được gen đó so với khi bấm nút send có giống nhau hay không?
Nếu không => code gen không đúng
Nếu có => kiểm tra trong quá trình xử lý ở server có bị lỗi hay exception ở đâu hay không?

4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?