mercredi 1 mai 2019

How to perform POST request in Axios with the given headers, auth (username, password) & data(payload)?

I am generating access token from Authorization-(Type: OAuth 2.0) in Postman and with that token i am able to get the response by passing the headers, payload in body for my POST request But the same is not working in Javascript code using Axios for my protractor test? Below is my code:

this.postAxiosReq = function(){
    var headers = {
        'Authorization': 'Bearer myTokenWhichIsGeneratedInPostman',
        'Accept': 'application/json',
        'Content-Type' : 'application/json'
    }

//this data is the Body (raw) in JSON which i am passing in Postman as a Payload

    var data = {
        "from": "2019-04-18T18:30:00.000Z", "to": "2019-04-26T18:29:59.999Z", "countries": ["India"],"timeFrame": "Last7Days"
    }

/this is app uername and password,

    var auth= {
        username: "username@test.com",
        password: "somepassword"
    }



    const url = 'https:/myapiUrl';
    console.log('start post req:::::')

    axios.post(url,{
        headers,
        auth, 
        data })
    .then(res =>{
        console.log('NEED RES:::::',res)
        return res.data;
    })
    .then(response =>{
        console.log('FINAL RESPONSE::::::::',response.data);
    })
    .catch(err =>{
        console.log(err);
    })
}

I am using Axios to perform POST request but in this case i am not getting any response in console for the above code. Also i want to tell that i need to generate access Token first to perform the Request, the same is working in Postman no issue. So first i am using the same generated token (hardcoded) here in my Authorization': 'Bearer myTokenWhichIsGeneratedInPostman, just to check whether i m able to get response or not. Plz tell me guys where i am doing mistake. Explain me if possible. Please answer Thanks in advance

Aucun commentaire:

Enregistrer un commentaire