63
/*Making http request to the api (Git hub)
create request
parse responce
wrap in a function
*/
var https = require("https");

var username = 'lynndor';
//CREATING AN OBJECT
var options = {
    host: 'api.github.com',
    path: ' /users/'+ username +'/repos',
    method: 'GET'
};

var request = https.request(options, function(responce){
    var body = ''
    responce.on("data", function(chunk){
        body += chunk.toString('utf8')
    });
    responce.on("end", function(){
        console.log("Body", body);
    });
});
request.end();

Im trying to create a request to the git hub api, the aim is to get the list repository for the specified you, but i keep getting the above mentioned error, please help

David Deutsch
  • 17,443
  • 4
  • 47
  • 54

6 Answers6

108

for other situation can be helpful

JavaScript encodeURI() Function

var uri = "my test.asp?name=ståle&car=saab";
var res = encodeURI(uri); 
qwabra
  • 2,094
  • 2
  • 15
  • 23
  • 3
    Thanks. Facebook graph api works for hebrew (and I guess all other RTL languages) only if you use this approach. – dang Jun 10 '19 at 12:15
  • another weird use case where this was useful is when I used supertest and jest to test my typescript API and my test URL had emojis in it. I wasn't able to send a request in that test without encoding it. – Vaibhav Chopra Oct 13 '20 at 21:35
28

Your "path" variable contains space

path: ' /users/'+ username +'/repos',

Instead it should be

path: '/users/'+ username +'/repos',

Matt
  • 3,617
  • 2
  • 27
  • 39
pkd
  • 504
  • 5
  • 13
  • where can i get a list of characters that need to be escaped in the path? – Akah Dec 28 '16 at 22:00
  • I think i get it. Sometimes it could be necessary to have strings url encoded before transmitting via node's http methods – Akah Dec 28 '16 at 22:26
12

Use encodeURIComponent() to encode uri

and decodeURIComponent() to decode uri

Its because there are reserved characters in your uri. You will need to encode uri using inbuilt javascript function encodeURIComponent()

var options = {
    host: 'api.github.com',
    path: encodeURIComponent('/users/'+ username +'/repos'),
    method: 'GET'
};

to decode encoded uri component you can use decodeURIComponent(url)

noone
  • 6,168
  • 2
  • 42
  • 51
6

Typically, you do not want to use encodeURI() directly. Instead, use fixedEncodeURI(). To quote MDN encodeURI() Documentation...

If one wishes to follow the more recent RFC3986 for URLs, which makes square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following code snippet may help:

function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }

There is a similar issue with encodeURIComponent() (source: MDN encodeURIComponent() Documentation), as well as a similar fixedEncodeURIComponent() function. These should be used, rather than the actual encodeURI() or encodeURIComponent() function calls...

To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
1

I was getting this error while trying to hit Elasticsearch's API. For me, it was due to Chinese characters in the Document's Title (in the Request I was sending). Switching to all English characters fixed the issue.

Gene
  • 10,819
  • 1
  • 66
  • 58
0

Sometimes browser inspector uses abbreviation of long JSON object. In my case, the data included unescaped characters such as '…' which should not be in http request.

Dave Lee
  • 316
  • 3
  • 9