3

In my node.js app, I want to make an https api call. I am trying with the https module and it is not working, but then I try with a request module, and that works.

not work

var options = {
    host : 'myserver/platform-api/v1',
    port : 80,
    path : '/projects?access_token=38472',
    method : 'GET',
    headers : {
        'Accept' : 'application/json'
    }
};
var req = https.request(options, function(res) {
    res.on('data', function(chunk) {
        console.log(chunk);
    });
});
req.on('error', function(e) {
    console.log(e);
    console.log('problem with request:', e.message);
});
req.end();

I get this

problem with request: getaddrinfo ENOTFOUND myserver/platform-api/v1
 myserver/platform-api/v1:80

this works

request("https://myserver/platform-api/v1/projects?access_token=38472", function(error, response, body) {
    if (error) return console.log(error);
    //console.log(error);
    //console.log(response);
    console.log(body);
});

I can't figure out why it does not work on the first one. Does anyone know why?

Thanks

1
  • Instead of passing options as a param maybe pass options.path? Commented Sep 28, 2017 at 20:25

2 Answers 2

3

EDIT Switched to port 443 as well.


Your host seemed to include part of the path? Try this instead (left just the host in host and moved the path to path):

var options = {
    host : 'myserver',
    port : 443,
    path : '/platform-api/v1/projects?access_token=38472',
    method : 'GET',
    headers : {
        'Accept' : 'application/json'
    }
};
Sign up to request clarification or add additional context in comments.

6 Comments

Why are you passing everything in the object wouldn't all those properties be defined elsewhere?
I'm not sure what you're asking. port defaults to 443, so that could be omitted. Is that what you mean?
thanks, it was the port and I had part of the path in the host.
I am not as familiar with this format so I was just asking why you're defining host, path, method etc. all as a parameter in the objects properties in the HTTP request. My thought is this would be defined elsewhere, so you can just do the simple GET, PUT, DELETE, PATCH, etc. It looks more like an AJAX call.
Should also probably educate myself a bit more on the subject, node. It's a pretty general question.
|
2

Also, you're hitting port 80, which is usually not the HTTPS port.

1 Comment

ok thanks i figured it out, it was port 443 and I had part of the path in the host.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.