2017-04-09 19 views
2

Authorizationヘッダーをcurlから取得できますが、Node.jsのrequestまたはhttpsからは取得できません。サーバーはcurlでステータス200を返しますが、requestまたはhttpsで500を返します。 requestまたはhttpsからの電話は、curlとどのように異なる場合がありますか?どのようにサーバーがそれらを別々に読んでいるのでしょうか?request.jsが失敗しました。認証ヘッダー付きのGETリクエストでCURLが成功しました

次のcURLコマンドラインから成功:

curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource 

しかし、同じ要求がノードにrequest.jsで失敗し

var options = { 
    type: 'get', 
    url: "https://api.domain.com/path/to/resource", 
    headers: { 
    "Authorization": " Bearer abc123def456" 
    } 
} 
request(options, function (err, response, body) { 
    assert.equal(response.statusCode, 200) ; // 500 internal error 
}) 

次のようにもauthオプションを使用してrequest.jsで失敗します。

var options = { 
    type: 'get', 
    url: "https://api.domain.com/path/to/resource", 
    auth: { 
    "bearer": "abc123def456" 
    } 
} 
request(options, function (err, response, body) { 
    assert.equal(response.statusCode, 200) ; // 500 internal error 
}) 

なし:ノードから出て砲撃した場合

var options = { 
    host: 'api.domain.com', 
    port: 443, 
    path: '/path/to/info', 
    method: 'GET', 
    headers: { 
    "Authorization": " Bearer abc123def456" 
    } 
} 
var req = https.request(options, function (res) { 
    res.setEncoding('utf8'); 
    res.on('end', function() { 
    assert.equal(res.statusCode, 200) // 500 internal error 
    }) 
}); 

req.on('error', function (e) { 
    console.log('problem with request: ' + e.message); 
}); 

req.end(); 

しかし、カールの要求が成功します。

exec("curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource", function (error, stdout, stderr) { 
    var obj = JSON.parse(stdout) // successfully retrieved and parsed 
}); 

request-debugは、以下の情報提供します:

{ request: 
    { debugId: 1, 
    uri: 'https://api.domain.com/path/to/resource', 
    method: 'GET', 
    headers: 
     { host: 'api.domain.com', 
     authorization: 'Bearer abc123def456' } } } 
+0

500内部エラーは、通常、サーバー側でエラーが発生していることを意味します。 APIサーバーログを確認しましたか? 'User-Agent'ヘッダーを解析しようとしていますか? –

+0

大きな質問です。私はサーバーにアクセスすることはできません.OAuthルートだけです。 curlや要求が 'User-Agent'ヘッダを渡すのですか? – prototype

+0

カールは1つを渡します、あなたがそれを指定しない限り、リクエストはしないと思います。 –

答えて

2

500 internal errorを一般に誤りがあることを意味サーバー側。理想的には、サーバーログを調べる必要があります。

あなたがそれらのログへのアクセスを持っていない場合は、あなたがしようとしたオプションの各1によっ​​て送信された要求の違いを見て:

モジュール:(手動で指定された認証ヘッダー付き)要求:

GET /path/to/resource HTTP/1.1 
Authorization: Bearer abc123def456 
host: api.domain.com 

モジュール:要求(明示的に指定された認証ヘッダを有する):

GET /path/to/resource HTTP/1.1 
host: api.domain.com 
authorization: Bearer abc123def456 

モジュール:HTTP(手動で指定された認証ヘッダを有する):

GET /path/to/info HTTP/1.1 
Authorization: Bearer abc123def456 
Host: api.domain.com 

カール:

GET /path/to/resource HTTP/1.1 
Host: api.domain.com 
User-Agent: curl/7.51.0 
Accept: */* 
Authorization: Bearer abc123def456 

モジュールの残りの部分はHTTP headers「ユーザー・エージェント」を送ると「受け入れる]していないことは極めて明白です。したがって、サーバー上で動作しているアプリケーションが、少なくとも1つを解析して失敗する場合があります。

関連する問題