Trying to write some mocha tests for my restify server. Some of the services require an Authorization header.
I am trying to set it this way:
var client = restify.createJsonClient({
version: '1.0.0',
url: 'http://localhost:9000',
headers: {Authorization:'Bearer ' + global.access_token}
});
but inspecting the request headers shows its not getting set, and my test is failing because of invalid credentials.
reading here, i believe i have the headers option.
http://restifyjs.com/#jsonclient
global.access_token is being set correctly.
Can someone help with some options on how to set that header?
Thanks
The header was getting set. there was a _headers node higher up in the stack and i could see that the Authorization header was getting set, but as the value:
'Bearer undefined'
So for some reason when the restify client was getting created it can not get the value from global, although its getting set in my test 01-test.
In the body of the 02-test, i can console the value and see it.
So either.
The value isn't set by the time the next test starts.
The value can't be retrieved in the restify client setup
Either way, I solved it by actually writing the token synchronously to a tmp file and reading it subsequent tests. Seems hacky, but maybe something else will come to mind.
Related
In java, I can send request via proxy server as
HttpClient clinet = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("proxy.server", portNum)))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://server.I.want.to.send.request"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = clinet.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.statusCode());
System.out.println(response.body());
I have tested with code above and checked it works just fine.
But if I want to do the same thing in nodejs (react) it seems like it doesn't work.
const agent = httpsProxyAgent('proxy.server:portNum');
axios.request({
method: 'POST',
url: "https://server.I.want.to.send.request",
data: {body},
httpsAgent: agent
});
As far as I understand, code above is the best I can try with JavaScript in react.
I have tried it, but it fails with timeout error (which means request doesn't go through proxy server I guess)
Could anyone can give me an advise to solve this problem?
i think you can refer to this issue in github:
https://github.com/axios/axios/issues/925#issuecomment-513028175
If this still cannot fix your issue. You might change to use another library, node-fetch. The link is here:
https://www.scrapingbee.com/blog/proxy-node-fetch/
I'm trying to set a cookie in a node request. I have tried using packages like js-cookie, cookie-js, cookie and cookie-manager but none work.
The way I have tried it is very straight-forward, whenever my endpoint gets called i.e. https://develop.api/sess/init, I set the cookie at the very beggining of the endpoint with the following code
import * as Cookies from 'js-cookie';
export const init = async (event: APIGatewayEvent, context: Context) => {
...
Cookies.set('hello', 'hello');
...
}
As my endpoint has an auth header, I can not directly call it into my browser URL due to missing permissions, so I tried generating the fetch function with postman and pasting it into my browser's console. The function is the following
var myHeaders = new Headers();
myHeaders.append("Referer", "accepted.referer.com");
myHeaders.append("key", "somekey");
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
fetch("https://develop.api/sess/init", requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
Once called, my request successfully returns the expected response, but it never shows a Set-Cookie header in the network section, neither shows my cookie in the Application section.
I have to mention that I also tried looking for the cookie when making the call within Postman, but it never sets it neither.
Also, I have tried starting the application in localhost, and I have a successful response, but my cookie is still not being set.
About the package showed in the code, I said I have tried it with different ones and their implementations, so I don't think a broken package is the problem.
I'm starting to think that I have a wrong idea about how cookies work, or that someway I am completely blocking the sending of cookies within my code.
Environment
If it helps in any way, my endpoint is being hosted in a AWS Lambda application.
I know this should be trivial, but being battling with it for a day now.
I finally answered my own issue. The key here is that I'm using AWS lambdas as the proxy, therefore, the headers I were using to send the cookies were wrong, I was sending the cookies with the endpoint instead of within the lambda. Let me explain myself.
I was adding 'Set-Cookie':'cookieKey:cookieVal' in the headers of the Postman Call that I was using to test both my local and develop environments.
Instead of that, I needed to send the request within the response of the lambda for the cookies to be registered.
Please check at the following links for similar cases ->
https://aws.amazon.com/blogs/compute/simply-serverless-using-aws-lambda-to-expose-custom-cookies-with-api-gateway/
https://forum.serverless.com/t/how-to-send-a-cookie-as-a-response/1312/7
I am trying to send an auth header along with an axios POST request from inside a Vue application. I currently am getting a 401 from my back end with an auth header that works when I do a curl.
I've tried splitting it up into variables and putting it in but that did not work and resulted in the same error (401).
This is just the axios code I am trying to get to work. I have checked with console.log and all values I am trying to send exist, though I don't know how to check the axios headers before sending.
axios.defaults.headers.common["Authorization"] = JWTtoken;
axios.post(updateURL, {
token: result.token
});
The backend code can't be changed easily for testing so need to figure out why not sending from the front end
I'd like it to send the correct header along with my request so I don't get a 401 status code.
I think you need this..
axios.defaults.headers.common["Authorization"] = "Bearer " + JWTtoken;
axios.post(updateURL, {
token: result.token
});
Notice that I add Bearer in the Authorization. It is how JWT was meant to be used according to their introduction.
However, if the answer is wrong. Help us by providing more information about your response in Developer Console as #RuChernChong suggest. Any error logs would be helpful as well.
Another way by using Axios globals to set for example X-Auth-Token encoding from JWT.io directly like this:
axios.defaults.headers.common["X-Auth-Token"] = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
I am trying to consume a soap api using node soap. My response cannot be parsed and I wonder how to see the request and response data to console to ease the error finding process.
As node soap uses the request library, one can debug it via:
NODE_DEBUG=request node src/index.js
as pointed out request's Readme.md:
Debugging
There are at least three ways to debug the operation of request:
Launch the node process like NODE_DEBUG=request node script.js (lib,request,otherlib works too).
Set require('request').debug = true at any time (this does the same thing as #1).
Use the request-debug module to view request
and response headers and bodies.
To see the generated SOAP XML request you can use this:
Client.lastRequest - the property that contains last full soap request for client logging
I'm using Supertest (and thus Superagent) for some API testing on a Node.js project. I have a particular route that returns different content based on the type of request. Using expressJS's req.xhr, the code will return JSON if true or issue a redirect if false. I want to account for this in my tests but I'm struggling to get Superagent to mimic an XHR GET request.
According to ExpressJS documentation, it's looking for the X-Requested-With header in the request to be set to the value XMLHttpRequest. When I try to set this header using .set('X-Requested-With', 'XMLHttpRequest') I get
Unhandled rejection Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
Odd thing is, I can set other headers, even before this one, just fine
.set('Accept', 'application/json'). If the headers have already been written, this should fail too, but it doesn't. Also, checking res.request.req._headers, it appears nothing else is setting X-Requested-With so it's not like something else gave it a value either.