Request failed with status code 400

xameeramir
xameeramir edited September 22 in API clients
We have a code to generate the access token after successful logins:
export let generateAccessToken = async (req: Request, res: Response) => {

try {
const api_key = process.env.ZERODHA_API_KEY;
const api_secret = process.env.ZERODHA_API_SECRET;
const request_token = req.query.request_token;
const checksumInput = `${api_key}${request_token}${api_secret}`;
const checksum = await Crypto
.createHash('sha256')
.update(checksumInput)
.digest('hex');

const accessTokenResponse = await axios.post('https://api.kite.trade/session/token',
{
"api_key": api_key,
"request_token": request_token,
"checksum": checksum
},
{
headers: {
"X-Kite-Version": 3
}
});

res.send(JSON.stringify(accessTokenResponse));
}
catch (ex) {
res.send('Error ' + ex);
}
}
And the result is always this:
Error: Request failed with status code 400

Can you guys please check why is this always coming up as 400 Missing or bad request parameters or values
Tagged:
  • ZI4453
    https://kite.trade/docs/connect/v3/user/
    I am not sure , but i think version 3 passes as int instead of string.. I might be wrong too.
  • sujith
    We just redirect to the given URL. This 400 is your response.
  • xameeramir
    xameeramir edited September 28
    @sujith @ZI4453 My question is why is the 400 a response? The parameters are perfectly all right I believe. I tried version to be a string too. Do you see any logs on your servers?
  • ZI4453
    @xameeramir ,these are protocols, please review your Params, payloads.400 error has got nothing to do with broker's. its totally code Error(mostly "params" because its able to reach broker's server (thats why 400)but we have sent wrong payload to API(means server rejeceted)) from client end.
  • Vivek
    @xameeramir Seems like issue is because Axios post request by default sends data as JSON payload but Kite API requires it to be `application/x-www-form-urlencoded`. You can check `axios.post` documentation on how to send POST request as `application/x-www-form-urlencoded` - https://github.com/axios/axios#using-applicationx-www-form-urlencoded-format
  • xameeramir
    Hi @Vivek I have tried setting headers to "content-type": "application/x-www-form-urlencoded" too but the result is the same
  • sujith
    The post request you are building has a syntax error. Just setting a header won't work. I suggest looking up for some examples of how to make a post request.
    You may need to use an additional library as well.
Sign In or Register to comment.