Custom Headers on Restler Get - node.js

I'm using Dan Wrong's Restler for Node Express to connect to an API. One of my endpoints requires a custom header is sent, but I'm having no luck in sending this:-
restler.get('api.co.uk/user',
{ username: 'xx', password: 'xx' },
{ headers: { 'token': cookie }})
.on('complete', function(user) {
res.render('dashboard.ejs', { user: user });
})
.on('error', function(error) {
res.render('500.ejs', { error: error });
})
I've tried variations of how to include the headers - but nothing seems to work, the header never seems to pass. Does anyone have any suggestions on how to achieve this please?
Help appreciated.

Try this:
restler.get('http://api.co.uk/user', {
query : { username: 'xx', password: 'xx' },
headers : { token: cookie }
}).on(...)

Related

How to login using Nuxt-auth module with a Node back and mongoDB

I'm currently creating an auth system using Mongo as database, Node with express as backend, and Nuxt as frontend.
I've found this authentication module https://auth.nuxtjs.org/ followed the doc, and the youtube video. I'm using Local scheme for Jwt system.
My issue is the login doesn't work successfully.
I think it doesn't work because of my user route on the backend, but not sure about that.
I also read that their is a user.autoFetch but dont know how to use it.
nuxt.config.js
modules: [
// https://go.nuxtjs.dev/axios
'#nuxtjs/axios',
'#nuxtjs/auth'
],
// Axios module configuration: https://go.nuxtjs.dev/config-axios
axios: {
baseURL: 'http://localhost:3000/'
},
auth: {
strategies: {
local: {
endpoints: {
login: { url: 'api/auth/login', method: 'post', propertyName: 'token' },
user: { url: 'api/auth/user', method: 'get', propertyName: 'data.attributes' },
logout: false
},
tokenType: ''
}
}
},
login.vue
async submit() {
try {
let response = await this.$auth.loginWith('local', { data: this.login })
console.log(response)
} catch (err) {
console.log(err)
}
// await this.$router.push('/dashboard');
},
The response send me back a 200 status, a token, and the userId (which is in my db) but the $auth still send me loggedIn at false and I can't get information like $auth.user.email.
I tried to change the auth endpoints in the nuxt.config.js
Changed tokenType: '' to tokentype: 'Bearer'
Changed the #nuxtjs/auth version to the same as the video tutorial. (4.8.4)
I added a controller for the route user :
route :
router.get('/user', userCtrl.getUser);
controller:
exports.getUser = (req, res, next) => {
User.findOne({ _id: req.params.id })
.then(thing => res.status(200).json(thing))
.catch(error => res.status(404).json({ error }));
}
Do not hesitate to ask me more details, on the configuration, other parts of the code, or anything like that.
Thank you in advance for any kind of help !

How to make a curl request using axios with curl options

I have to make a curl request to jenkins to get the job done but I am not sure how to do that using axios I am currently using nestjs as my backend framework I have tried googling it not much luck If anybody please help. The curl request looks a bit like below
curl -u pp:11d7b5072400da385981e24283472834 -H "${JENKINS_CRUMB}" -i -X POST $JENKINS_URL/job/$JOB_NAME/buildWithParameters --user pp:1232343454564123453 --data token=123456 --data
According to axis documents you can create request like that:
const JENKINS_URL = '***';
const JOB_NAME = '***';
const options = {
method: 'POST',
url: `${JENKINS_URL}/job/${JOB_NAME}/buildWithParameters`,
headers: {
JENKINS_SAMPLE_HEADER: 'jenkins_smaple_header'
},
withCredentials: true,
auth: {
username: 'pp',
password: '11d7b5072400da385981e24283472834'
},
data: {
token: '123456',
}
};
axios (options)
.then(function(response) {
console.log(response);
}).catch(function(error) {
console.error('Error', error);
});
I used this lovely tool and obtained this - after fixing a couple of errors in the original request:
const axios = require('axios');
const response = await axios.post(
'http://path',
new URLSearchParams({
'token': '123456'
}),
{
auth: {
username: 'pp',
password: '1232343454564123453'
}
}
);
It's quite useful to help out when you don't know how to translate a request!

Problem to integrate send in blue api with my nestjs application

Describe the bug
Problem to integrate sendinblue api with my nestjs app. I need to integrate with a custom authentication and in the axios's api says that i need to use a header for custom authentication, in postman i'm using api key for authorization and it works,but it throws a error 401.
To Reproduce
async SendEmailToSendInBlue(email: {
sender: { email: string };
to: [{ email: string }];
subject: string;
textContent: string;
}): Promise<any> {
const url = `${SENDINBLUE_URL}`;
return this.httpService
.post<any>(url, email, {
headers: {
Key: 'api-key',
Value: process.env.SENDBLUE_API_KEY,
'Add To': 'Header',
},
/* auth: {
username: 'apikey',
password: process.env.SENDBLUE_API_KEY,
}, */
})
.toPromise()
.then((data) => {
return data;
});
}
Expected behavior
Send an transational email to client.
Environment
-Axios Version 0.19.2
-Node.js Version 14+
-Docker Version 19.03.12-ce
-Additional Library Versions
Additional context/Screenshots
enter image description here
enter image description here
The Sendinblue documentation itself explains how to do this.
The link below explained how to pass api-key in header:
https://developers.sendinblue.com/docs/send-a-transactional-email#send-a-transactional-email-using-a-basic-html-content
headers: {
'api-key': 'YOUR_API_KEY'
}

Discord Profil Picture Update from ElectronJS using request PATCH

I'm trying to code an application into Electron JS to allow the person to change their profile picture at the same time on several applications.
For this I use the APIs of each platform.
For Twitter it works correctly, but I block at the level of Discord.
I can make a GET request on the profile, but I can't do a : PATCH/users/#me
https://discordapp.com/developers/docs/resources/user#modify-current-user
I do not know if it's the token that does not offer enough power, because I only asked for Identity as permission on my application.
I tried to pass JSON between true and false,
to add a content type, but I still have the same answer: {code: 0, message: '401: Unauthorized'}
function postDiscord(image) {
const imageDataURI = require('image-data-uri')
let {token} = store.get('discordToken') //get stored token
imageDataURI.encodeFromFile(image)
.then(res => {
request({
method: 'PATCH',
url: 'https://discordapp.com/api/v6/users/#me',
headers: {
'Authorization': 'Bearer '+token,
'User-Agent': 'someBot (site, v0.1)'
},
body: {
'avatar': res
},
json: true
}, function(err, res) {
if(err) {
console.error(err);
} else {
console.log(res.body)
}
}
);
})
}
{code: 0, message: '401: Unauthorized'}
Refering to Discord :https://github.com/discordapp/discord-api-docs/issues/1057
Cannot upload new pics with Oauth :/

How to pass data from angular to node js server?

My app.component.ts contains
login() {
var user = `{ email: ${this.email}, password: ${this.password} }`
const headers = new HttpHeaders()
.set('Authorization', 'my-auth-token')
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/signin', user, {
headers: headers
}).subscribe(data => {
});
console.log(`email: ${this.email} password: ${this.password}`)
}
When trying to get the data in node I get
error: SyntaxError: Unexpected token e in JSON at position 2
I am using
req.body
to get the data.
What is the correct way to parse the JSON data? Also wanted to know if this is the correct way to pass form data from angular to node?
var user = {
email: this.email,
password: this.password
}
...
this.http.post('http://localhost:3000/signin',user).subscribe(...)
With Angular 6 HttpClient, you don't need to stringify your object (see POST example from official doc). In the same way, for a GET request, HttpClient parse json data for you.
Try this :
login() {
const user = { email: this.email, password: this.password };
const headers = new HttpHeaders({
'Authorization': 'my-auth-token',
'Content-Type': 'application/json'
});
this.http.post('http://localhost:3000/signin', user, {
headers: headers
}).subscribe(data => {});
console.log(`email: ${this.email} password: ${this.password}`) }
user= {
email: aka#gmail.com,
password: 123,
}
$http({
method: "POST",
url: http://localhost:3000/signin,
data: JSON.stringify(user),
headers: headers
}).success(function(data) {
// you can print data hera
}
}).error(function() {
});
First i thought you using an object for the user, than a saw it's a string.
Your data is not with correct json format.
Try to put quotes on the fields name
var user = `{ "email": ${this.email}, "password": ${this.password} }`
WRONG!
You should stringify the data - JSON.stringify(user)
this.http.post('http://localhost:3000/signin', JSON.stringify(user), {
headers: headers
}).subscribe(data => {
});

Resources