Node http-proxy find proxied URL - node-http-proxy

I'm trying to debug why I get a 404 from a proxied server when use http-proxy. Is there a way I can find out what the proxied URL is?
This is my current config:
app.all('/api/*', function(request, response, next) {
return proxy.proxyRequest(request, response, {
host: 'foo.com',
port: 80
});
});

You can use request.url to get the URL of the request. For example, to print the URL to the console before proxying it:
app.all('/api/*', function(request, response, next) {
console.log('Proxying ' + request.url);
return proxy.proxyRequest(request, response, {
host: 'foo.com',
port: 80
});
});

Related

Fetch request headers vs. Postman headers

Using Postman I'm able to do the following call:
GET https://tickets.ramdom.com/api/events/95
Request Headers
Host: tickets.ramdom.com
Cookie: OptanonConsent=isIABGlobal=false
Cache-Control: no-cache
Postman-Token: db42ad48-62f1-4e3f-8903-66002116e8a3
The call works fine. Now I want to reproduce the same call using fetch and Firefox console.
So I create the following piece of code:
const opts = {
headers: {
Host: 'tickets.ramdom.com',
Cookie: 'OptanonConsent=isIABGlobal=false'
}
};
fetch('hhttps://tickets.ramdom.com/api/events/95', opts)
.then((response) => {
alert(response)
})
.catch((error) => {
alert(error)
});
But I always get the following error:
TypeError: NetworkError when attempting to fetch resource.
Do you know what I'm doing wrong?
Thanks

internal server error 500 when using axios post/get in azure web app for fullcalendar

Having trouble with get/post in axios when deployed to azure web app, everything works in local pc.
axios({
method: 'post',
headers: {
'content-type': 'application/json'
},
url: '/Admin/Events/AddEvent',
data: {...}
})
.then(res=>...)
.catch(err => alert(`8881 Something went wrong 88: ${err}`));
output
For post method => 400 (Bad Request)
Request URL: https://xxx/Admin/Events/AddEvent
Request Method: POST
Status Code: 400 Bad Request
Remote Address: xxxx
Referrer Policy: no-referrer-when-downgrade
For get method => 500 (Internal Server Error)
I've tried the following (all works locally)
adding CORS
services.AddCors(options =>
{ ...
Using stringify(data)
changing url to full https://xxx/Admin/Events/AddEvent
different style of post e.g.
axios.post('https://xxx/Admin/Events/AddEvent',{
Any other debugging methods will be much appreciated.

serverless: When 302 redirect the custom header or set-cookie header not getting passed

I am using aws api gateway with help of serverless.
I am able to redirect to another domain from my lambda function.
like : abc.com ====> API Gateway & Lambda ==[302 redirect]==> xyz.com
I am unable to set custom header or any header.
here is my code.
exports.create = async (event) => {
try {
const token ='uniqueValue';
const response = {
statusCode: 302,
headers: {
Location: 'http://localhost:1337',
bearer_token: token,
'Set-Cookie': token,
'Content-Type': 'application/json'
}
};
return response;
} catch (err) {
console.log(err);
return err;
}
};
my auth-handler.yml looks like this,
auth-token:
handler: modules/auth/endpoints/token.create
memorySize: 128
timeout: 30
events:
- http:
path: /rest/auth/token
method: get
I am unable to set cookie in xyz.com. please help...
Thanks in advance

Response headers in Angular interceptor

I have an interceptor for authentication.
I want to get a header out of the response when I get a 401 response error.
Interceptor is:
function ($httpProvider, fileUploadProvider) {
$httpProvider.interceptors.push(function($q, $localStorage) {
return {
'request': function(config) {
if ($localStorage.token) {
config.headers.Authorization = 'Bearer ' + $localStorage.token;
}
return config;
},
'responseError': function(response) {
if (response.status === 401) {
//$rootScope.$broadcast('unauthorized');
// WWW-Authenticate: Bearer error="invalid_token"
var authResult = response.headers('WWW-Authenticate');
if (authResult.indexOf("invalid_token")>-1) {
$localStorage.token = null;
$timeout(function(){
;
});
}
}
return response;
}
};
I want to get the WWW-Authenticate header from the response.
I can confirm the header is in the response of the web service call by looking at the network tab in Chrome developers tools. If I set a break point in the response handler function and then run console.log(response.headers()) in the console I get:
Object {}
undefined
How do I get to the response headers?
The responseError function receives rejection instead of response.
Therefore if you want to access response headers, what you need is like below.
'responseError': function(rejection) {
if (rejection.status === 401) {
console.log(rejection.config.headers);
}
}
I hope this would help you. :)
Although I know this is not answer and should post as comment, I post it here to use screen capture image.
I tried to get a response header with my test enviroment like below.
nodejs server
res.setHeader('WWW-Authenticate', 'invalid_token');
res.status(401).send();
angularjs
'responseError': function(rejection) {
if (rejection.status === 401) {
console.log(rejection.headers('WWW-Authenticate'));
}
}
Chrome dev tool screen capture
As you can see, I could get the response header correctly.
Therefore I think that there seems to be some problem in your server code where you set a response header.
Would you like to show us your chrome dev tool screen capture and your server code where you set the response header?

AJAX 504 when calling ASP.NET Web API

My AJAX call is returning a 504 error when calling an ASP.NET Web API action.
More info:
Here's my API action:
public HttpResponseMessage Get(string fileName, int feedID)
{
try
{
// create file...
return new HttpResponseMessage { Content = new StringContent("Complete."), StatusCode = HttpStatusCode.OK };
}
catch (Exception ex)
{
Log.WriteError(ex);
throw new HttpResponseException(new HttpResponseMessage
{
StatusCode = HttpStatusCode.InternalServerError,
Content = new StringContent("An error has occurred.")
});
}
}
Here's my AJAX call:
$.ajax({
url: url,
type: 'GET',
success: function () {
$("#lblProgressDownload").hide();
window.open("Previews/" + fileName);
},
error: function (xhr, status, error) {
$("#lblProgressDownload").hide();
alert("Error downloading feed preview: " + error);
}
});
I get a 504 error (viewed in fiddler/ chrome console) when the file takes too long to create. The "error" parameter in the error callback doesn't return anything.
I only get the 504 error when it's hosted - on my dev it works fine.
How do I prevent this 504 error?
Note, I already tried changing the executionTimeout property in my web.config, as well as the ajax timeout. Neither worked.
HTTP error 504 is a gateway timeout:
The server, while acting as a gateway or proxy, did not receive a timely response from the upstream server specified by the URI [...] in attempting to complete the request.
I suspect that means there is a proxy or gateway somewhere between you and the production server, but not your dev server, which is why it fails on the one but not the other.
Your choice is either to make your server code fast enough that it doesn't trigger the timeout, or get whoever is running the proxy server to relax their timeout restrictions (assuming it's something that you or your company controls).

Resources