Support Topics Documentation Slack YouTube Blog

Custom API service throwing error when called but succeeding when invoked manually

I have a custom API service built with Codeless that runs as expected when invoked manually; however, when I call it from my front-end it throws an error.

The way I’m calling the service is pretty straight forward:

axios({
  url: `${axios.defaults.baseURL}/${process.env.VUE_APP_BACKENDLESS_APPLICATION_ID}/${process.env.VUE_APP_BACKENDLESS_REST_API_KEY}/services/StripeCustom/retrieveSession`,
  method: "POST",
  data: {
    id: state.getters.stripe.checkoutSessionId
  }
})

All variables have their expected values.

Is there certain header information that I need to pass when calling a custom API service? Or is there a way to call the custom services with the JS SDK that might make this simpler?

Backendless 5.5.1 Online
REST SDK
Application ID: B9941993-E38E-A647-FF0F-626E69BF9900

If this is a POST request with some JSON payload, you need to pass the following header:

Content-Type:application/json

It is and I’m already passing that header. It’s odd: I don’t have issues with the standard API calls, only custom ones.

When you invoke a service from Backendless Console. it uses the published REST route. Take a look in the Network tab of Chrome to see what the request looks like. Perhaps your call is missing something.

Btw, do you know there is a code generator for your published services? It can create JS, Java, ObjC and Swift code.

How does the code generator work? Where can I find it?

Not much for clues in the console. I get:

{"code":0,"message":"{\"error\":{\"message\":\"Invalid checkout.session id: {\",\"type\":\"invalid_request_error\"}}","errorData":{}}

which is very strange because that’s coming from Stripe, not the custom API service. It is a valid session ID and when used for invoking it manually, it works. It’s like the parameter is getting corrupted on the way to the API service. I can’t figure it out.

Here’s the info on code generator:
https://backendless.com/docs/bl-js/bl_client_sdks.html

Please let me know what parameters we can invoke the service with and we will take a look (more than likely sometime tomorrow).

Regards,
Mark

I’m 99% sure my issue is related to an Axios bug.

I had success by using:

Backendless.CustomServices.invoke(
  "StripeCustom",
  "retrieveSession",
  state.getters.stripe.checkoutSessionId
)

However Axios is passing parameters… with this particular scenario it fails. Odd.

If it works directly in console and with Backendless.CustomServices.invoke, we’ll consider it resolved :slight_smile:

1 Like