Localhost being injected into the pathname by Axios

I am trying to implement an axios request using their official documentation.

Here is my code snippet:

const REFERRAL_API_URL= "https://referrals.gen.com"


export function createReferral() {
  axios({
    data: {},
    headers: {
      'Content-Type': 'application/json',
      ApiKey: ReferralApiKey,
    },
    method: 'post',
    url: `${REFERRAL_API_URL}/api/ReferralMasters`,
    withCredentials: true,
  })
    .then(function (response) {
      console.log(response)
    })
    .catch(function (error) {
      console.log(error)
    })
}

However, when I execute the function, I encounter a 404 error message:

NOT FOUND https://localhost:3000/referrals.gen.com/api/ReferralMasters

I'm confused as to why localhost:3000 is included in the URL path. Any ideas on what could be causing this issue?

Answer №1

I encountered a similar issue and managed to resolve it using the following steps. Start by creating an API client using axios.create(), as shown in the code snippet below:

const apiClient = axios.create({
    baseURL: REFERRAL_API_URL,
    headers: {
        'Content-Type': 'application/json',
        ApiKey: ReferralApiKey
    }
});

Once you have set up the client, proceed to send a request like so:

const config = {
    url,      // Assuming it is "ReferralMasters"
    method,   // Can be "GET", "POST", "PUT", or "DELETE"
    data,     // Custom body
    params    // Object with key-value pairs corresponding to required parameters
};

const response = await apiClient.request(config);

To further customize your configuration, refer to the axios documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Searching for a streamlined approach to sending out numerous HTTP requests in a node.js environment

I'm new to the world of JS/node.js after working with .Net. I have an existing Web API host that I want to stress test with different payloads. I am aware of load testing tools available for this purpose, but my focus right now is on finding an effic ...

What is the best way to repair a react filter?

Recently, I successfully created a database in Firebase and managed to fetch it in React. However, my current challenge is to integrate a search bar for filtering elements. The issue arises when I search for an element - everything functions as expected. ...

When you use ReactDOM.render inside a <div>, it does not instantly generate HTML code

I am currently working with React version 16.7 and attempting to embed React components into a third-party JavaScript library (specifically, Highcharts) that requires me to output HTML from a function. This is the approach I am taking at the moment: funct ...

Set a timeout for a single asynchronous request

Is there a way to implement a setTimeout for only one asynchronous call? I need to set a timeout before calling the GetData function from the dataservice, but it should be specific to only one asynchronous call. Any suggestions? Thank you. #html code < ...

Associating mongoose object with a DTO object in an Express.js application

I am looking for a way to transform mongoose result objects into DTOs for my views. Here is an example query that returns a mongoose object: const returnedData = (err, result) => { //Result object is a schema from Moongose cb(err, re ...

Setting an Angular Directive on a dynamically generated DOM element using Javascript

One of my directives enables the drag-and-drop functionality for DOM elements with the following JavaScript: JS: angular.module('animationApp').directive('myDraggable', ['$document', function($document){return{link: function ...

Guide on how to clear and upload personalized information to Stormpath

After receiving JSON data from my client, I am looking to store it in Stormpath's custom data using node.js with express.js: I have set up a basic post route: app.post('/post', stormpath.loginRequired, function(req, res){ var data = req.b ...

Tips for receiving a success notification post form submission on CodeIgniter

I'm currently working on a form using CodeIgniter. Below is the code I have so far: <?php echo form_open_multipart(''); ?> <div class="input-group"> <input maxlength="30" type="text" name="name" placeholder="Name" class ...

What is the best way to access the display property of a DOM element?

<html> <style type="text/css"> a { display: none; } </style> <body> <p id="p"> a paragraph </p> <a href="http://www.google.com" id="a">google</a> &l ...

What is the best way to insert additional divs into a box div that contains tabs?

My current challenge is as follows: On a webpage, I have a box with jQuery tabs labeled "Enter" and "About" designed to switch the content displayed within the box. UPDATE: The jQuery script in use is shown below: <script type="text/javascript"> ...

Vue js version 2.5.16 will automatically detect an available port

Every time I run the npm run dev command in Vue.js, a new port is automatically selected for the development build. It seems to ignore the port specified in the config/index.js file. port: 8080, // can be overwritten by process.env.PORT, if port is in u ...

The error message "this.props.navigation" is not defined and cannot be evaluated as an object

Recently, I encountered an issue with my navigation system. Within my application, there are 3 screens or components that I navigate between using react-navigation. The first screen prompts the user to enter their mobile phone number and password, which is ...

The React application encounters errors when the API URL provided is incorrect

Embarking on my first React application journey, I am faced with a challenge. A form with two input fields prompts the user to enter the name of a country and a city before submitting the form. The city and country variables are then passed to the subseque ...

Implement validation for dynamically generated input fields in JavaScript and PHP

Looking for help with dynamic text boxes in jQuery. Trying to validate them using PHP or JavaScript to prevent empty values from being stored in the database. Despite several attempts, empty values still get entered into the database when text boxes are le ...

Employing AJAX, execute a synchronous function asynchronously (Javascript)

Here's a code snippet for the sync function. I've been calling it inside an Ajax, but apparently it's deprecated because it's synchronous. Is there any way to make it run as if it were asynchronous? Or is there a way to convert it into ...

Exposing external variables within the setInterval function

Here is the snippet of code I am working with: update: function(e,d) { var element = $(this); var data = element.data(); var actor = element.find('.actor'); var value = basicdesign.defaultUpdate( e, d, element, true ); var on = templateEn ...

CORS blocking Axios POST request to Heroku causing a Network Error 503

Using the MERN Stack, everything was functioning correctly until modifications were made to the UI (such as relocating code to different components and altering styles). The issue lies with a specific POST request, while other requests that utilize Axio ...

Detecting collisions on a tile map using only JavaScript

Currently, I am developing a tile-based game using a traditional method (utilizing two for loops) and struggling with writing collision detection. I want the blue square to remain on the screen and appear on top of the tiles once the start button is clicke ...

Searching for the desktop location using Node.js

As discussed in the Node.js - Find home directory in a platform-agnostic way question, we can locate the home directory using the following code: const homedir = require('os').homedir(); However, how can I locate the desktop folder in Windows r ...

Here is a step-by-step guide on how to use JavaScript to eliminate the page title, URL, date and

When printing a page using window.print, is there a way to exclude the page title, URL, page number, and date/time from appearing? ...