0

I am trying to create a Axios request where i will post json data. The format of the data will be

{"qBody":"aaa","qAnswer":"bbb","qOptions":[],"qType":"GAP","qClass":6,"qSubject":1,"qChapter":1,"qCreatorid":1,"qCreatorrole":"admin"}

But it is posting as

{"data":{"qBody":"aaa","qAnswer":"bbb","qOptions":[],"qType":"GAP","qClass":6,"qSubject":1,"qChapter":1,"qCreatorid":1,"qCreatorrole":"admin"}}

Here is my code snippet:

var data = {
              "qBody" : question,
              "qAnswer" : trueFalseAnswer,
              "qOptions" : qOptions,
              "qType" : questionCategory,
              "qClass" : className,
              "qSubject" : subjectName,
              "qChapter" : chapterName,
              "qCreatorid" : qCreatorid,
              "qCreatorrole" : qCreatorrole
            };

    const newData =  JSON.stringify(data)
    
     this.$axios.post("http://128.199.192.87:8081/api/v1/questions/add",{
                  newData
                },{
                  'Content-Type': "application/json"
                }).then((response)=>{
                  console.log(response)
                })

How can I make the format correct? Thanks in advance

4
  • 1
    have you read the documentation? Commented Apr 8, 2021 at 6:10
  • When you say "it is posting as..." do you mean: "that's what I see in the browser console"? Commented Apr 8, 2021 at 6:16
  • No. It is from Request Payload in Network Tab of developers Option of the browser Commented Apr 8, 2021 at 6:51
  • Could you please create a runnable minimal reproducible example using codesandbox.io (or a similar service), making sure it reproduces the described behavior? What you're saying is close to impossible. Commented Apr 8, 2021 at 7:00

1 Answer 1

2

What you are console.log()-ing is not what you're sending. It is the response that's getting back from the server. If you want to log what you're sending, use:

console.log(newData);

... just before making the POST request.

Most likely, you don't need to stringify the request.

What you're seeing in the console is the server response. According to the response-schema in their documentation, the server's response data is placed in the .data property of the response object.

So, apparently, the server sends back the same data. With most servers, this means no error has occurred.


In fewer words, you are not sending your data wrapped as { data: ... }. If you were, you would be getting back: { data: { data: ... } }

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

1 Comment

I have posted the above json from Request Payload in Network Tab of developers Option of the browser. I tried skipping stringify the data.

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.