'Getting 400 bad request in Axios but in Postman status 200

I'm trying to request a Power BI API using axios.

var configReportInfos = {
    method: 'get',
    url: `https://api.powerbi.com/v1.0/myorg/groups/${process.env.GROUP_ID}/reports/${process.env.DASHBOARD_ID}`,
    headers: {
        'Content-Type': 'multipart/form-data',
        'Authorization': 'Bearer ' + tokenResponse.accessToken
    },
    data: {}
};

let reportinfos
try {
    reportinfos = await axios(configReportInfos)
    tokenResponse['reportInfos'] = reportinfos
}

catch (error) {
    console.log(error);
};

Actually I'm getting this error

Request failed with status code 400"

But strangely in Postman I'm getting 200 status.

enter image description here

I'm using same token.



Solution 1:[1]

I had the same problem. When l switched content-type in my app to application.json everything worked.

Solution 2:[2]

I faced the same issue (not in Axios) and struggled for a few hours. I was able to make the API call from postman but from my java code I was getting a 400. Turns out, this was a content issue. My request body had some Cyrillic content and it needed to be encoded before sending. Postman was doing it by default and hence the requests were going through and responding with 200

Solution 3:[3]

I recommend to check your Request Header options in most cases some option is configured wrong.

In your case content-type seems wrong, did you try application/json in both calls.

To configure properly you can check the documentation Content Type Configuration

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 medisetty
Solution 3