2016-04-03 16 views
3

requestjs numaralı telefonu kullanarak JSON tabanlı bir API'yi aramaya çalışıyoruz, ancak sunucudan bir hata alıyoruz.node.js requestjs hatası vs curl

request.get({ 
      url: 'https://api.myapi.com', 
      timeout: 120000, 
      headers: { 
       username: 'myuser', 
       password: 'mypassword' 
      } 
     }, function (err, result) { 
      ... 
     } 
) 

hatası:

java.lang.IndexOutOfBoundsException: Index: 0 
    java.util.Collections$EmptyList.get(Collections.java:4454) 
    com.itemmaster.api.rest.svc.authentication.OpenSSOUserAuthServiceImpl.authenticate(OpenSSOUserAuthServiceImpl.java:159) 
    com.itemmaster.api.rest.profiler.SecurityProfiler.profile(SecurityProfiler.java:58) 

herhangi bir sorun olmadan bukle döner kullanarak aynı çağrıyı çalışırken.

curl --header "username:myuser" --header "password:mypassword" 'https://api.myapi.com' 
+1

Ve hata nedir? – MinusFour

+0

üstbilgiler altında kullanıcı adı/parola girmeyin, bu hatayı çağırmalısınız – JordanHendrix

cevap

0

Curl komutu sağladığınız sözdizimi ile çalışıyorsa, aşağıdaki kod node.js.

var request = require('request'); 
 

 
var headers = { 
 
    'username': 'myuser', 
 
    'password': 'mypassword' 
 
}; 
 

 
var options = { 
 
    url: 'https://api.myapi.com', 
 
    headers: headers 
 
}; 
 

 
function callback(error, response, body) { 
 
    if (!error && response.statusCode == 200) { 
 
     console.log(body); 
 
    } 
 
} 
 

 
request(options, callback);