How to send cookies with node-fetch? - javascript

I've got nodejs application which handles user's requests and receives cookies which i want to proxy to internal API service. How to approach this by using node-fetch?
Don't offer superagent please.

You should be able to pass along cookies by setting it in the header of your request:
const opts = {
headers: {
cookie: 'accessToken=1234abc; userId=1234'
}
};
const result = await fetch(`/some/url`, opts);

Read & write cookies like a bot
async function login() {
return fetch('<some_url>/login', {
'headers': {
'accept': '*/*',
'content-type': 'application/x-www-form-urlencoded; charset=UTF-8',
'cookie': '',
},
'body': 'username=foo&password=bar',
'method': 'POST',
});
}
(async() => {
const loginResponse = await login();
const loginCookies = parseCookies(loginResponse);
})();
You may want to include: accept-language, user-agent, referer, accept-encoding, etc. (check a sample request on your Chrome DevTools via the Network tab)
For some reason the resulting cookies of node-fetch requests are not compatible with new requests, but we can parse them like this:
function parseCookies(response) {
const raw = response.headers.raw()['set-cookie'];
return raw.map((entry) => {
const parts = entry.split(';');
const cookiePart = parts[0];
return cookiePart;
}).join(';');
}
Pass cookies in your future requests through the same headers:
return fetch('<some_url>/dashboard', {
'headers': {
'accept': '*/*',
'cookie': parsedCookies,
},
'method': 'GET',
});

For simple, you can write a middleware which will include the cookies to global.fetch, like below.
const realFetch = fetch;
function cookieFetch(fetch, cookie) {
return (url, opts) => {
opts = opts || {};
return fetch(url, Object.assign(opts, {
headers: Object.assign(opts.headers || {}, { cookie })
}));
};
}
function middleware(req, res, next) {
const kuki = req.headers.cookie;
global.fetch = kuki ?
cookieFetch(realFetch, kuki) :
realFetch;
next();
}
module.exports = middleware;

Related

TypeError Only Absolute Urls are supported [NEXTjs]

I am working on a next js project a client provided me. But when I start the project I get this error saying Only absolute urls are supported. oh I don't have the dot env file. is this error causing for the env file? I have tried using hardcoding localhost:XXXX but that throws another error saying Bad protocol expected htps instead got http here is the code.
I have tried hardcoding localhost that did not worked
import https from "https";
import {
getIp
} from "./ip";
const agent = new https.Agent({
rejectUnauthorized: false,
requestCert: false,
});
function createHttpClient(base) {
return async function client(
endpoint, {
data,
params,
headers: customHeaders,
request = {},
...customConfig
} = {}
) {
const config = {
agent,
body: data ? JSON.stringify(data) : undefined,
method: data ? "post" : "get",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"X-Real-IP": getIp(request),
// TODO: Authorization: `Bearer ${token}`,
...customHeaders,
},
...customConfig,
};
let url = [base, endpoint].join("/");
if (params) url = [url, new URLSearchParams(params)].join("?");
return fetch(url, config).then(async response => {
const data = await response.json();
if (response.ok) {
return data;
} else {
return Promise.reject(data);
}
});
};
}
const browserClient = createHttpClient(process.env.NEXT_PUBLIC_API_URL);
const serverClient = createHttpClient(process.env.API_URL);
export {
browserClient,
serverClient
};
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Bad respond when trying to get authentication token for Reddit api 'Application Only OAuth'

So i been trying to get access to the reddit api.
I registered to reddit. verified my mail. opened an app and got my credentials.
Followed this official documentation and also came across to this tutorial
All my efforts have failed and don't get any respond.
I am using nodejs. but also tried in postman and failed.
Tried 2 options using fetch and using axios:
const axios = require('axios');
const fetch = require('node-fetch')
const { URLSearchParams } = require('url')
class RedditApi {
clientId2 = "get ur own credentials by opening an app here https://www.reddit.com/prefs/apps";
clientSecret2 = "get ur own credentials by opening an app here https://www.reddit.com/prefs/apps";
authenticationUrl = `https://www.reddit.com/api/v1/access_token`;
BASE_URL = 'https://www.reddit.com/';
tokenAuth = null;
tokenExpirationTime = null;
currencyObj = null;
constructor(currencyObj) {
this.currencyObj = currencyObj;
console.log("constructor service")
}
async getAuthToken() {
const bodyParams = new URLSearchParams({
grant_type: "https://oauth.reddit.com/grants/installed_client",
device_id: "DO_NOT_TRACK_THIS_DEVICE"
});
console.log(this.clientId2, 'this.clientId');
debugger;
const headersObj = {
'Authorization': `Basic ${Buffer.from(`${this.clientId2}:`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
};
let response = null;
try {
response = await axios.post(this.authenticationUrl,
bodyParams,
{
headers: headersObj
});
debugger;
} catch (error) {
debugger;
console.error(error);
console.log(error.stack);
return null;
}
}
async getAuthToken2() {
try {
// Creating Body for the POST request which are URL encoded
const params = new URLSearchParams()
params.append('grant_type', 'https://www.reddit.com/api/v1/access_token')
params.append('device_id', 'DO_NOT_TRACK_THIS_DEVICE')
// Trigger POST to get the access token
const tokenData = await fetch('https://oauth.reddit.com/grants/installed_client', {
method: 'POST',
body: params,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': `Basic ${Buffer.from(`${this.clientId2}:`).toString('base64')}` // Put password as empty
}
}).then(res => {
debugger;
return res.text()
})
debugger;
if (!tokenData.error) {
debugger;
res.send(trendingResult)
}
res.status(tokenData.error).send(tokenData.message)
} catch (error) {
debugger;
console.log(error)
}
}
}
module.exports = RedditApi;
when using axios i get this respond: "Request failed with status code 401"
When using fetch i get this respond: "'403 Forbidden\nRequest forbidden by administrative rules.\n\n'"
Anybody knows what is the problem and how can i fix it?
Many thanks!

How to set Authorization token with axios in node.js

I want to use axios in api test. To initialize client I need first to set auth token, which I expect to get with axios as well.How to get it form async code?
const a = require('axios');
getToken = () => {
var axios =a.create({
baseURL: 'http://local.v2.app.com/api/auth/v1.0',
headers: {'Content-Type': 'application/json'},
});
return axios.post('/credentials/login', {
username: '1#test.com',
password: 'Test#1234'
})
.then(function (response) {
return response.data.data.token;
})
.catch(function (error) {
console.log(error);
});
}
const client=a.create({
baseURL: 'http://local.v2.app.com/api/auth/v1.0',
headers: {
'Content-Type': 'application/json',
'Authorization': getToken()
},
});
module.exports = client;
First, getToken should be async (or just return promise):
async getToken() {
var axios = a.create({
baseURL: 'http://local.v2.app.com/api/auth/v1.0',
headers: {'Content-Type': 'application/json'},
});
try {
const response = await axios.post('/credentials/login', {
username: '1#test.com',
password: 'Test#1234'
})
return response.data.data.token;
} catch (error) {
console.error(error);
}
}
Then you can mock request:
const client = a.create({
baseURL: 'http://local.v2.app.com/api/auth/v1.0',
headers: {
'Content-Type': 'application/json'
}
})
const request = client.request
const addToken = (token) => (data, headers) => {
headers.Authorization = token
return data
}
client.request = (config = {}) => new Promise(async (resolve, reject) => {
token = await getToken()
if (!config.transformRequest) config.transformRequest = []
config.transformRequest.push(addToken(token))
request(config).then(resolve, reject)
})
module.exports = client
Yes, bit messy, feel free to refactor!
axios has request function which makes requests, others like get, post are aliases. This code patches request to get token first and then continue request.
transformRequest is took from axious readme on github, there is a comment "You may modify the headers object" so it is legal.
P.S good question, don't know why -1

How to use the requestJS get method to set the cookie?

let headers = {
'Accept': xxxxx,
'Host': 'xxxx',
'Referer': 'https://xxxx/',
}
request.get(url, {
json: true,
headers: headers
}).on('response', function (response) {
let headers = response.headers['set-cookie'];
})
If I use
headers = {
'Accept': xxx,
'cookie':xxx
}
it will memory leak
According to the README of request, you can use request.jar() to set your own cookie to the request.
var j = request.jar();
var cookie = request.cookie('key1=value1');
var url = 'http://www.google.com';
j.setCookie(cookie, url);
request({url: url, jar: j}, function () {
request('http://images.google.com')
})
UPDATE:
They said,
Cookies are disabled by default (else, they would be used in subsequent requests). To enable cookies, set jar to true (either in defaults or options).
So you can enable request to use cookies just to set { jar: true }.
var request = request.defaults({jar: true})
request('http://www.google.com', function () {
request('http://images.google.com')
})

https.request ignoring rejectUnauthorized

I'm trying to connect to a remote server using nodejs 0.12, and i keep getting the response SELF_SIGNED_CERT_IN_CHAIN. I have looked at similar questions 1 2 but somehow their solutions don't work on my server.
I am connecting to a test environment outside of my control setted up with a self signed certificate. This is my request:
var https = require("https");
var fs = require('fs');
start();
function start()
{
var listadebancos =
{
language:"es",
command:"GET_BANKS_LIST",
merchant:
{
apiLogin:"111111111111111",
apiKey:"11111111111111111111111111",
},
test:true,
bankListInformation:
{
paymentMethod:"PSE",
paymentCountry:"CO"
}
};
var listadebancosString = JSON.stringify(listadebancos);
var headers =
{
'Content-Type': 'application/json',
'Content-Length': listadebancosString.length
};
var options= {
host: 'stg.api.payulatam.com',
rejectUnauthorized: false,
agent:false,
path: '/payments-api/4.0/service.cgi',
method: 'POST',
cert: fs.readFileSync('./stg.gateway.payulatam.crt'),
}
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var req= https.request(options, funcionRespuesta);
req.write(listadebancosString);
req.end();
function funcionRespuesta(res)
{ console.log(res);
}
}
Am i missing something obvious?
I decided to use a library call needle to make the request and this time i was able to receive the response with no SSL errors. Just in case anyone is in the same situation here is the code i used:
var listadebancos =
{
"language":"es",
"command":"GET_BANKS_LIST",
"merchant":{
"apiLogin:"111111111111111",
"apiKey:"11111111111111111111111111",
},
"test":false,
"bankListInformation":{
"paymentMethod":"PSE",
"paymentCountry":"CO"
}
};
};
// var listadebancosString = JSON.stringify(listadebancos);
var headers =
{
'Content-Type': 'application/json'
};
var options = {
host: 'stg.api.payulatam.com',
**json:true,**
path: '/payments-api/4.0/service.cgi',
method: 'GET',
headers: headers,
rejectUnauthorized: false,
requestCert: true,
agent: false,
strictSSL: false,
}
needle
.post('stg.api.payulatam.com/payments-api/4.0/service.cgi',listadebancos, options, funcionRespuesta)
.on('end', function() {
console.log('Ready-o, friend-o.');
})
function funcionRespuesta(err, resp, body)
{
console.log(err);
console.log(body);
}

Categories