Sending Request body for GET method in AXIOS throws error

后端 未结 3 765
暖寄归人
暖寄归人 2020-12-20 07:47

I have a React application where I am changing POST method to GET with the request body as it is. It works fine with POST request however when I change the method to GET, it

3条回答
  •  我在风中等你
    2020-12-20 08:51

    If you want to send parameters with get request in axios, you should send parameters as params.

    If you want to set "Content-type":"application/json" and send params with get request, you should also send an empty data object.

    For example:

    const AUTH_TOKEN = 'Bearer token'
    const config = {
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Authorization': AUTH_TOKEN,
        },
        data: {},
        params: {
            "post_id": 1
        }
    }
    axios.get("http://localhost/api/v1/posts/", config)
    

提交回复
热议问题