admin管理员组

文章数量:1414614

I recently encounter a stupid problem in my app width an axios request.

Here is the query:

const data =  { name: name }
const headers = { headers: { Authorization: `Bearer ${token}` } }

axios.post('http://localhost:3333/list/add', data, headers)

This request works but doesn't insert the name value...

When I console log the data variable I have the correct value and when I test this request on postman, it works perfectly.

Request on Postman

Request on my app

My API code:

async add({ request, auth, response }) {
        try {
            const list = new List()

            let user = await User.find(auth.current.user.id)
            let name = request.get('name')

            list.fill(name)

            const result = await list.save()

            await Database
                .insert({'user_id': user.id, 'list_id': list.id})
                .into('users_has_lists')
            
            return response.json({
                query_name: 'add',
                status: 'success',
                data: list
            })

        } catch (error) {
            return response.status(400).json({
                status: 'error',
                message: error.message
            })
        }
    }

Could someone tell me what is wrong with this request?

I recently encounter a stupid problem in my app width an axios request.

Here is the query:

const data =  { name: name }
const headers = { headers: { Authorization: `Bearer ${token}` } }

axios.post('http://localhost:3333/list/add', data, headers)

This request works but doesn't insert the name value...

When I console log the data variable I have the correct value and when I test this request on postman, it works perfectly.

Request on Postman

Request on my app

My API code:

async add({ request, auth, response }) {
        try {
            const list = new List()

            let user = await User.find(auth.current.user.id)
            let name = request.get('name')

            list.fill(name)

            const result = await list.save()

            await Database
                .insert({'user_id': user.id, 'list_id': list.id})
                .into('users_has_lists')
            
            return response.json({
                query_name: 'add',
                status: 'success',
                data: list
            })

        } catch (error) {
            return response.status(400).json({
                status: 'error',
                message: error.message
            })
        }
    }

Could someone tell me what is wrong with this request?

Share Improve this question edited Jan 27 at 16:20 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Mar 28, 2020 at 8:08 Renaud MonellRenaud Monell 1351 silver badge11 bronze badges 9
  • "doesn't insert the name value" could you elaborate that? – user2030471 Commented Mar 28, 2020 at 8:13
  • Did you try to log token? Its value could be the problem. – user2030471 Commented Mar 28, 2020 at 8:15
  • I mean that the token is good given that the request works fine (She insert the good user id) The only problem is that the params are ignored and don't insert in the database. When I test this request on Postman using the same token and the same params, it works. – Renaud Monell Commented Mar 28, 2020 at 8:18
  • Did you check that params in the netwrok tab? – Muhammad Haseeb Commented Mar 28, 2020 at 8:24
  • 1 From postman the data is being sent as query parameters while from axios it it being sent as post body. You need to make it consistent. – user2030471 Commented Mar 28, 2020 at 9:10
 |  Show 4 more ments

1 Answer 1

Reset to default 3

2nd parameter in a post request count as body. To add params, you need to use another param. For your example, it should be

axios.post('http://localhost:3333/list/add', null, {headers:headers, params:data})

本文标签: javascriptSet Params and Headers in axios post requestStack Overflow