I am sending a request for a token to Auth0 via a server proxy, and it worked wonderfully. However, today the same code will only return this:
{"error":"server_error","error_description":"Error when JSON serializing the result of the extension point"}
What am I missing here? This feels particularly silly, since this worked no problem last week, and the code hasn't changed since.
Thanks in advance to anyone who reads/comments.
original request:
{baseURL}/{API PATH}/post?path=https://apropostenantname.auth0.com/oauth/token
body:
{"grant_type":"client_credentials","client_id":"XXX","client_secret":"XXX","audience":"XXX"}
options, right before being sent to Auth0:
{ host: 'apropostenantname.auth0.com',
path: '/oauth/token',
port: undefined,
method: 'POST',
headers:
{ 'Content-Type': 'application/json',
'Content-Length': 200} }
Here is the proxy controller:
"use strict";
var rethinkdb = require('rethinkdb');
var async = require('async');
const deepmerge = require('deepmerge');
var https = require('https');
var util = require('util');
var querystring = require('querystring');
exports.proxyPost = (req,res)=>{
let req_url = req.query.path;
let body = JSON.stringify(req.body);
let auth = req.header('Authorization') ? req.header('Authorization') : null;
// no auth headers sent with this particular request.
let contenttype = req.header('Content-Type') ? req.header('Content-Type') : 'application/json';
var headers = {
'Content-Type': contenttype,
'Content-Length': body.length
};
if(auth != null){
headers['Authorization'] = auth;
}
var match = req_url.match(/^(?:(https?:)?\/\/)?(([^\/?]+?)(?::(\d{0,5})(?=[\/?]|$))?)([\/?][\S\s]*|$)/i);
// ^^^^^^^ ^^^^^^^^ ^^^^^^^ ^^^^^^^^^^^^
// 1:protocol 3:hostname 4:port 5:path + query string
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// 2:host
let protocol = match[1];
let host = match[3];
let port = match[4];
let path = match[5];
var options = {
host: host,
path: path,
port:port,
method: 'POST',
headers: headers
};
console.log(options);
var _req = https.request(options, function(_res) {
_res.setEncoding('utf-8');
let header_keys = Object.keys(_res.headers);
for(let header of header_keys){
res.setHeader(header, _res.headers[header]);
}
var responseString = '';
_res.on('data', function(data) {
responseString += data;
});
_res.on('end', function() {
var responseObject = responseString ? JSON.parse(responseString) : '';
res.send(responseObject);
});
});
_req.write(body);
// console.log(_req);
_req.end();
}
It turns out that one of my hooks was returning some weird data or something, which caused the error. Hook deleted, problem solved.