2

I am trying to send a nested json data with get method using axios, but the problem is that the backend considers the children as a string.

const TOKEN = "token"
const config = {
    headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Authorization': TOKEN,
    },
    data: {},
    params: {
        "page_id": 1,
        "filter": {
            "search": "name"
        }
    }
};
axios.get("http://localhost/api/pages", config)

What I get if I want to print filter in backend:

"{"search": "name"}"
1
  • config.params.filter Commented Feb 5, 2020 at 8:58

3 Answers 3

8

You may have two options:

1- The first option is to decode the string you receive to json.

e.g.

---json_decode() in php

--- JSONObject() in java

--- JSON.parse() in nodejs

or any other method depending on your backend language...

2- The second option is to send your object in this format:

params: {
    "page_id": 1,
    "filter[search]": "name"
}

And pay attention not to put search in quotes!

Sign up to request clarification or add additional context in comments.

1 Comment

for option 2, it throws error: Invalid character found in the request target
0

You can use req.query on the server side:

function get(req, res, next) {
  const { filter } = req.query;
  console.log(filter);
  ...
}

Comments

0

Do a JSON.parse() of your Request.query.filter. Note that Request should be the request variable in your backend.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.