DEV Community

loading...

Unable to POST array of objects in axios

gurupal profile image Gurupal Singh Updated on ・1 min read

why this post method is showing data: {key: "data"} ? Anyone ?

handleUpload() {
    const companyuid = this.props.match.params.id;
    var raw = 
                {
                    "data":[
                                {
                                    "type": "CRN",
                                    "value": this.state.cr_number,
                                    "files": [this.state.UUID_CRN]
                                },
                                {
                                    "type": "VRN",
                                    "value": this.state.vat,
                                    "files": [this.state.UUID_VAT]
                                },
                                {
                                    "type": "CON",
                                    "value": this.state.vat,
                                    "files": [this.state.UUID_VAT]
                                }
                            ]
                };

    let AUTH_TOKEN = window.localStorage.getItem("ACCESSTOKEN");

    fetch(
      "http://xxxx/documents",
      {
        method: "POST",
        headers: {
          Accept: "application/json",
          "Content-Type": "application/json",
          Authorization: Bearer ${AUTH_TOKEN}
        },
        body: JSON.stringify(raw)
      }
    )
      .then(response => response.json())
      .then(responseJson => {
        console.log(responseJson);
      })
      .catch(error => console.log("error", error));
  }
Enter fullscreen mode Exit fullscreen mode

Discussion (3)

pic
Editor guide
Collapse
adityamitra profile image
Aditya Mitra

Is this error showing when you are parsing it back to JSON?

Collapse
gurupal profile image
Gurupal Singh Author

It's resolved .. Thanks for the reply though :)

Collapse
difafebri profile image
difa febri

hello, sir, can you tell me how you solved it? i got stuck in this similar situation. Any kind of response from you would be much appreciated. Thank you!