Unable to access MongoDB API via standard HTTP requests

I've been attempting to access the MongoDB API using an API key for authentication, but I keep receiving a 401 status code.

Below is the code snippet I've been using:

fetch(‘https://eu-west-2.aws.data.mongodb-api.com/app/application-0-dhdvm/endpoint/mydatabase’, {
method: ‘GET’,
mode: ‘cors’,
cache: ‘no-cache’,
headers: {
‘Content-Type’: ‘application/json’,
‘Authorization’: Bearer ${apiKey}, // Make sure to include your API key in the Authorization header
},
})

I have successfully generated the API key:

(I made sure to export the key value before its creation, so that is not the issue here)

Here is the endpoint URL I've set up:

Any assistance would be greatly appreciated, as I seem to be encountering difficulties in properly querying the database using the provided setup.

Answer №1

When using the Data API, you won't find

Authorization: Bearer <TOKEN>
in the setup. Instead, check out the examples provided in the documentation at https://www.mongodb.com/docs/atlas/app-services/data-api/examples/. Pay attention to the api-key header in the examples:

curl --location --request POST 'https://us-east-1.aws.data.mongodb-api.com/app/data-paviu/endpoint/data/v1/action/find' \
--header 'Content-Type: application/json' \
--header 'Access-Control-Request-Headers: *' \
--header 'api-key: <YourApiKey>' \
--data-raw '{
    "collection":"foo",
    "database":"testX",
    "dataSource":"test0",
    "projection": {"_id": 1}
}

Test your endpoints and API keys with a curl command in the terminal first, then proceed to integrate it into your code environment.

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

Sorts through nested arrays to uncover distinctive product assortments

Currently, I am in the process of developing a website using Next.js and Shopify. My objective is to create a page that displays all collections matching a specific productType. To achieve this, I have been exploring ways to extract this data from the Gra ...

The overlay background is being obscured by surrounding elements

Hey there! I'm experimenting with some basic coding and ran into an issue with an overlay. Here's the site link: Sorry for the strange language. :-) If you click on the button with the tiny icon, you'll notice that the overlay form isn&apos ...

What is the best way to transfer data from a parent component to a child component in ReactJs?

When dealing with nested react elements, how can you pass values from the parent element to a child element that is not directly inside the parent element? React.render( <MainLayout> <IndexDashboard /> </MainLayout>, document.b ...

Best Practices for Showing JSON Data from MongoDB in an Angular Material Table

Desire I'm trying to extract data from MongoDB and exhibit it in an Angular Material Table. Problem Even though I can view the results of my MongoDB query in the console/terminal, the Chrome browser console indicates that my JSON data has been save ...

The mongoose query appears to be functional in the mongo shell but is experiencing difficulties within a Node.js

Trying to retrieve a list of todos based on the 'userId' associated with each one is proving difficult. The code below does not return any rows: DB.TodoTable.find({ "userId" : ObjectId("54c12f5f3620c07019e6b144") }, function(err, todos) { if ...

A div positioned in front of a centrally-located div

Managing a website with numerous headlines can be a challenge. When a user clicks on a headline, a button located 10px to the left of the headline should appear. However, the catch is that the headlines must always remain centered, regardless of whether th ...

Incorporating unique numbers in the map reduce process

I have a CSV file containing information on which numbers called each other and the details of the calls like duration, time, etc. My goal is to compile all the numbers that a specific number has called into an array. Each element in this array should be ...

Issue with Angular ng-model not functioning as expected within ng-repeat block

Encountering an issue when trying to bind Json data into ng-model within ng-repeat. html : <div ng-controller="Ctrl"> <div> <table> <th> <td>add</td> <td>ed ...

Activate audio when the link is clicked

Recently, I created a compact web application and it's almost complete. However, there is one cool functionality that I am missing. My goal is to have a sound play when the user clicks a link, but after playing, I want the navigation to continue as us ...

Efficient File Upload Feature Compatible with all Browsers, Including Internet Explorer versions 7, 8, and 9

Can someone assist me with a problem I'm facing? I need a code script that enables multiple file uploading on all browsers. While HTML5 supports Chrome and Mozilla Firefox, it does not support IE. I am looking for a script/jquery solution that works ...

Issues with Autofocus in AngularJs on Firefox

Check out this straightforward directive to set autofocus: app.directive('autoFocus', function($timeout) { return { restrict: 'AC', link: function(_scope, _element) { $timeout(function(){ ...

Tips for showcasing MongoDB information on an HTML webpage

I have a project where I need to showcase user data on an HTML webpage. I've set up POST and GET methods for the API, but I'm unsure how to actually display the data. Constraints prevent me from using frameworks like Angular or React so I have to ...

Please input new items by clicking a button

I have a dilemma with handling an array of objects in my Vue component. I am using v-for to display the objects, but now I want to update certain items in the array and save only those changes in a new object. Currently, when I attempt this by mapping over ...

What is the best approach for querying an entry by a specific attribute, such as name, in a MongoDB database using Express?

In this setup, an express server is communicating with a mongoDb server through mongoose. The goal here is to locate and retrieve a specific entry by name in the /getCollege/:name route without resorting to the findbyid function. const express = require( ...

Breaking on newline, excluding newline-space in JavaScript

I want to divide my string into an array based on new lines, excluding those that start with a space. ORGANIZER;<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3758455056595e4d524577524f565a475b521954585a">[email prot ...

Is it possible to export a constant from within a default function to a different file?

As a newcomer to React and React Native, I am looking to pass a const variable from within a function to another file. I attempted defining it outside of the function and allowing it to be modified inside the function, but encountered an invalid Hook Call ...

Assigning a variable in jQuery to a PHP variable that has not been defined can halt the script

Here is the code snippet for a document ready function: $(document).ready(function(){ var id = <?php echo "" ?>; alert('boo'); if(id!=0){ $(' ...

What is the key to ensuring the content in your canvas adapts to different screen sizes

Greetings! I wanted to extract the values from this specific meta tag: <meta name="viewport" property="viewport" content="width-device-width, initial-scale=1"> To retrieve content from a meta tag using JavaScript, I used the following code snippet: ...

How can I retrieve the value of one variable using a dynamic variable in Meteor?

Currently, I am striving to enhance the modularity of my code for the sake of organization and efficiency. However, I have hit a roadblock due to a lack of knowledge in JavaScript. I have a method call that pertains to purchasing different types of units. ...

What is the best way to send a parameter from JavaScript to a UserControl?

Within my ASP.Net application, I have a UserControl named EmployeesUserControl that is displayed within a JQuery modal dialog. Prior to displaying the Employees, I need to provide a filter criteria indicating which entity the employee belongs to. My query ...