Python Requests and Bonanza API Node.js / Unable to Properly Make API Requests / No Node.js Documentation

I am following the Bonanza.com Bonapitit documentation and successfully got their Python fetchToken script to work. However, I am now trying to recreate this same script in Node.js because I want it in my ExpressJS backend (and not Python).

Here is what the working fetchToken Python script looks like...

import requests

url = 'https://api.bonanza.com/api_requests/secure_request'

# See your Bonapitit welcome e-mail or your Bonapitit dashboard for
# your developer and certificate identifiers
headers = {
    'X-BONANZLE-API-DEV-NAME' : 'my_dev_name',
    'X-BONANZLE-API-CERT-NAME' : 'my_cert_name'
}

request_name = 'fetchTokenRequest'

# make the request to Bonanza
response = requests.post(url, data=request_name, headers=headers)

# the json response as a dictionary
response_json = response.json()

# examine the response
if response_json['ack'] == 'Success' and 'fetchTokenResponse' in response_json:
    token_response = response_json['fetchTokenResponse']

    print("Your token: " + token_response['authToken'])
    print("Token expiration time: " + token_response['hardExpirationTime'])
    print("Authentication URL: " + token_response['authenticationURL'])
else:
    print(response_json)

So I thought, ok, this should be easy enough to convert over to JavaScript, so here is what I tried...

exports.fetchToken = async (req, res) => {
  let _id = req.params.userId
  let bonanza_devId
  let bonanza_certId
  try {
    let user = await User.findById({_id})
    bonanza_devId = user.bonanza_devId
    bonanza_certId = user.bonanza_certId
  } catch (e) {
    console.log(`Got an error when obtaining the user data in fetchToken...`, e)
  }
  try {
    let request_name = 'fetchTokenRequest'
    let headers = {
      'X-BONANZLE-API-DEV-NAME' : bonanza_devId,
      'X-BONANZLE-API-CERT-NAME' : bonanza_certId,
      Accept: "application/json",
      "Content-Type": "application/json",
      request_name: request_name
  }
    let url = `https://api.bonanza.com/api_requests/secure_request`
    let response = await fetch(url, {
      method: "POST",
      headers: headers,
      body: new URLSearchParams({
        request_name
      })
    })
    response = await response.json()
    res.json(response)
  } catch (e) {
    res.json({error: e})
  }
}

(the above script is fetching a user from the database, getting the api creds from that, then attempting to make the call)

Unfortunately I am getting this error when using the JavaScript version...

{
    "ack": "Failure",
    "version": "1.1.2",
    "timestamp": "2021-06-19T18:53:00.000Z",
    "errorMessage": {
        "message": "The request was not formatted correctly",
        "type": "Bonapitit::InvalidRequestFormat"
    }
}

I think request_name = 'fetchTokenRequest' is not being put into the script wrongly because in the Python script it is using data=request_name.

What is the equivalent in JavaScript fetch to `data=request_name?

Also, when I tried to change the Content-Type from "Content-Type": "application/json" to "Content-Type": "application/x-www-form-urlencoded", it changes the error message to this...

{
    "ack": "Failure",
    "version": "1.1.2",
    "timestamp": "2021-06-19T19:04:54.000Z",
    "errorMessage": {
        "message": "Bonapitit::CannotFindRequest",
        "type": "Bonapitit::CannotFindRequest"
    }
}

In the documentation, it states The name for your request should be fetchTokenRequest. How are you supposed to "name your request" like that?

If you can spot the issue in my script, please respond.

1 answer

  • answered 2021-06-19 19:36 rom

    Thanks to @rdas, I solved it in the following way...

    let headers = {
          'X-BONANZLE-API-DEV-NAME' : bonanza_devId,
          'X-BONANZLE-API-CERT-NAME' : bonanza_certId,
          Accept: "application/json",
          'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
      }
    let url = `https://api.bonanza.com/api_requests/secure_request`
    let response = await fetch(url, {
          method: "POST",
          headers: headers,
          body: encodeURIComponent("fetchTokenRequest")
        })
    
    

    With this I successfully obtained the token.

    This made all the difference... encodeURIComponent("fetchTokenRequest")

    I suspect all their API calls can be made in NodeJS this way.