mig_08
mig_08

Reputation: 181

Passing through an array with axios

I am having an issue passing through an array through axios post call. The issue is that on the api endpoint the data received is null, when I try posting using postman it works fine so the endpoint is working. Example of the array enter image description here

I need to pass the data in this format:

{
    "UpdateItemList": [
        {
            "Text": 1,
            "Value": "5"
        },
        {
            "Text": 1,
            "Value": "5"
        }
    ]
}

Code:

export function createLogEntry(postData) {
    let payload = {
        UpdateItemList: postData
     };
     
    const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry', {
        data: payload
    });

    return {
        type: CREATE_LOG,
        payload: request
    }
}

Is there any issue with the way I am passing through the data with my current code?

Upvotes: 0

Views: 167

Answers (2)

Leo
Leo

Reputation: 1800

The issue is that you are confusing two ways axios can be used. Currently you are actually posting your data nested in an object within and the key data:

{
  data: {
    UpdateItemList: postData
  }
}

If you are using the axios.post function, you should just pass your object with the data to post as the second object like this:

const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry', payload);

If you are using the config object method, you should just pass one single object with url, method and data as keys.

// Send a POST request
axios({
  method: 'post',
  url: 'https://localhost:44312/api/Audit/AddLogEntry',
  data: payload
});

This behaviour is explained in the axios Readme here: https://github.com/axios/axios#axios-api

Upvotes: 0

Ish
Ish

Reputation: 123

Try with

const request = axios.post('https://localhost:44312/api/Audit/AddLogEntry',payload);

This worked for me!

Upvotes: 1

Related Questions