Error 400 on Production but working in Local Env

axios

  .get(apiUrl, {

    auth: {

      username: apiKey,

      password: "",

    },

    headers: {

      "Content-Type": "application/json",

      "Access-Control-Allow-Origin": "*",

    },

  })

  .then((response: any) => {

    return res.send({

      status: 200,

      data: response.data,

      message: "Business information retrived Successfully",

    });

  })

  .catch((error: any) => {

    return res.send({

      status: 400,

      data: error.message,

      message: "Error retriving business information",

      err: error,

    });

  });

This is the code i am using to call the get company by CRN api. Now to protect the credentials i have an encapsulation of my own api on it. Now the issue i am facing is that the api call is not going through and giving an error of 400 on the request both on the frontend and postman

Bit difficult to help you here, lack of details.
Are you posting to the correct live URL and using a 8 character company number ?
Do you have a ‘:’ at the end of your APIKey ?
Can you do a simple curl like the following?
curl -u : https://api.company-information.service.gov.uk/company/15366432

  • Yes i am posting it to the URL that you have listed in curl

  • Yes i do have that : at the end of my token

I used axios in my server and this is the error i am getting

{
“status”: 400,
“data”: “Request failed with status code 400”,
“message”: “Error retriving business information”,
“err”: {
“message”: “Request failed with status code 400”,
“name”: “AxiosError”,
“config”: {
“transitional”: {
“silentJSONParsing”: true,
“forcedJSONParsing”: true,
“clarifyTimeoutError”: false
},
“adapter”: [
“xhr”,
“http”
],
“transformRequest”: [
null
],
“transformResponse”: [
null
],
“timeout”: 0,
“xsrfCookieName”: “XSRF-TOKEN”,
“xsrfHeaderName”: “X-XSRF-TOKEN”,
“maxContentLength”: -1,
“maxBodyLength”: -1,
“env”: {
“Blob”: null
},
“headers”: {
“Accept”: “application/json, text/plain, /”,
“Content-Type”: “application/json”,
“Access-Control-Allow-Origin”: “*”,
“User-Agent”: “axios/1.6.7”,
“Accept-Encoding”: “gzip, compress, deflate, br”
},
“auth”: {
“password”: “”
},
“method”: “get”,
“url”: “https://api.company-information.service.gov.uk/company/14602507
},
“code”: “ERR_BAD_REQUEST”,
“status”: 400
}
}

I cant debug the problem and i have tried nearly everything

Sorry, we have no idea what axios is.
I can see you have the password as empty, but there is no username/api key in there. Have you removed that for public posting?
Are you able to by-pass all of your code and systems and do a simple curl command or a Postman call? That is the only way to debug this really…unless another Forum member has any experience with your type of system/situation…

Change my company name from RG Investments to RG Investment.

I’ve no idea what’s going on here nor am I an Axios user. However an error 400 suggests that the request is incorrectly formed in some way - the url is not right and/or the parameters are wrong / you are supplying parameters which don’t exist.

Perhaps you have already set a base URL? See this thread example for an example of fixing an issue with this:

For another Axios example with a 400 error from this forum - here someone had an incorrect URL:

Also I’m not sure you need the headers you have there if you are calling from a server. Certainly not the Content-Type as you aren’t sending any JSON - if you wanted to say you wanted to receive JSON (not that you get the choice here) you would use the Accept header.

So just try a minimal example, something like:

const axios = require('axios');
apiKey = "YOUR_API_KEY_HERE";
apiUrl = "https://api.company-information.service.gov.uk/company/15366432";

axios.get(
  apiUrl,
  {
    auth: { username: apiKey , password: "" }
  })
  .then(function (response) {
    // handle success
    console.log(response);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  });

If you were getting an error 401 that would suggest issues with authentication (e.g. you were calling from an IP / URL you had not registered with Companies House (if calling from a server you should register the external IP of that), or you were not using a live application, or there was an issue with the authentication e.g. incorrect API key, incorrect encoding / use of http Basic auth.

As @MArkWilliams has suggested it would be best to try this e.g. via curl / Postman and make a note of exactly what you are requesting, how you are entering your API key and the response.

Again - the issue doesn’t seem to be an authentication one but when you get to that:
Looking at the Axios documentation that suggests - at least as far as the Authorization part - you have two separate ways you could do this:

  1. “Automatically” e.g. let Axios do this for you e.g. how you’re doing this in your initial example. So you would supply your API key in username (without any terminating “:” or additional encoding) and “” as the password. This assumes that Axios allows you to supply an empty password…

  2. “Manually” - do not supply an “auth” section but instead do this yourself by setting a header in the headers section. Assuming this works like normal javascript and that the btoa function is available that would be:

headers: {
      'Authorization': 'Basic ' + btoa( apikey + ":")
    }

Good luck.