How to send a token in the header

0 votes
In my API-based application, I need to send a token in the HTTP header for authentication purposes. What is the proper way to include this token in the header using various methods (e.g., JavaScript fetch, Axios, or curl)? Are there best practices for securely handling and sending tokens?

Examples of implementing token-based authentication in both frontend and backend environments would be helpful.
Nov 11, 2024 in Cyber Security & Ethical Hacking by Anupam
• 9,050 points
79 views

1 answer to this question.

0 votes

In API-based applications, sending a token in the HTTP header is standard procedure for authorization and authentication. Here are examples using different methods to include this token in requests for API-based applications.

1. JavaScript Fetch API

fetch('https://api.example.com/data', {
    method: 'GET',
    headers: {
        'Authorization': 'Bearer your_token_here',
        'Content-Type': 'application/json'
    }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

2. Axios

import axios from 'axios';

axios.get('https://api.example.com/data', {
    headers: {
        'Authorization': `Bearer your_token_here`
    }
})
.then(response => console.log(response.data))
.catch(error => console.error('Error:', error));

3. cURL

curl -H "Authorization: Bearer your_token_here" -H "Content-Type: application/json" https://api.example.com/data

4. Node.js

To verify tokens on the server side in an Express app:

app.get('/data', (req, res) => {
    const token = req.headers['authorization']?.split(' ')[1];
    if (token) {
        // Verify the token here
        res.send('Token received and verified.');
    } else {
        res.status(403).send('No token provided.');
    }
});

answered Nov 12, 2024 by CaLLmeDaDDY
• 13,760 points

Related Questions In Cyber Security & Ethical Hacking

0 votes
0 answers

How to determine the hashing algorithm of a public key in the certificate?

I’ve been looking at an SSL/TLS certificate ...READ MORE

Jan 7 in Cyber Security & Ethical Hacking by Anupam
• 9,050 points
32 views
0 votes
1 answer
0 votes
1 answer
+3 votes
1 answer

How to send the phishing link to friend?

The Social Engineer Toolkit (SET) is a ...READ MORE

answered Feb 6, 2020 in Cyber Security & Ethical Hacking by anonymous
1 flag 4,021 views
+1 vote
1 answer

How do you decrypt a ROT13 encryption on the terminal itself?

Yes, it's possible to decrypt a ROT13 ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 13,760 points
181 views
+1 vote
1 answer

How does the LIMIT clause in SQL queries lead to injection attacks?

The LIMIT clause in SQL can indeed ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 13,760 points
344 views
+1 vote
1 answer

Is it safe to use string concatenation for dynamic SQL queries in Python with psycopg2?

The use of string concatenation while building ...READ MORE

answered Oct 17, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 13,760 points
188 views
+1 vote
1 answer
+1 vote
1 answer

How to send a bearer token in a header?

To send a bearer token in an ...READ MORE

answered Nov 7, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 13,760 points
90 views
0 votes
1 answer

How to get a JWT token from the browser?

In order to securely retrieve and store ...READ MORE

answered Nov 12, 2024 in Cyber Security & Ethical Hacking by CaLLmeDaDDY
• 13,760 points
100 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP