Mayank Bansal
Mayank Bansal

Reputation: 2075

Error While Making API call In reactjs

I am using the NPM Request library to make an API call in which I want to pass Header as well as form. Until the time I wanted only formdata the API call was working fine but as soon as added the header in the API call it is showing the following error:-

" Failed to construct 'Headers': Please use the 'new' operator.... "

Here Is My API call:-

send:function(endpoint,callback, token, formdata, component){
                this._request.post(endpoint, Headers : { Authorization :token 
                      }, {form: formdata},function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        callback(response.body, component);
                    }else if(!error && response.statusCode == 400){

                    }
                })
            },

Upvotes: 0

Views: 66

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58553

Change your code with this :

You can pass the headers as simple as json object but the format should be like this :

this._request.post(endpoint, { headers : { Authorization : token } , form: formdata },

    function (error, response, body) {
        if (!error && response.statusCode == 200) {
            callback(response.body, component);
        }else if(!error && response.statusCode == 400){

        }
    })

}

Upvotes: 1

Related Questions