2014-12-05 20 views
2

它已经花了我几个小时在URL MeteorJS上实现cookie。我需要做的是,传递像'CURLOPT_COOKIE'PHP这样的cookie数据。我无法在他们的文档甚至论坛上找到任何示例代码。现在我有这些功能:在MeteorJS HTTP请求上传递cookie数据

/* HTTP REQUEST */ 
Meteor.methods({ 
    httpRequest: function(type, uri, params){ 
    this.unblock(); 
    check(type, String); 
    check(uri, String); 
    check(params, Object); 
    try { 
     var result = HTTP.call(type, uri, {params: params}); 
     return result; 
    } catch (e) { 
     // Got a network error, time-out or HTTP error in the 400 or 500 range. 
     return e; 
    } 
    } 
}); 
// HTTP request with cooki 
getUserDetails: function(session_id, uid){ 
    var params = { 
    headers: { 
     Cookie: { 
     sessid: session_i 
     } 
    },  
    uid: uid 
    }; 
    var response = Meteor.call('httpRequest', "POST", "http://example.com/rest/wp /alt_wp_resources/loaduser.json", params); 
    //res = JSON.parse(response.content); 
    return response; 
} 
// call here 
Meteor.startup(function() { 
// delay for 5 sec 
Meteor.setTimeout(function(){ 
    Meteor.call('getUserCredentials', 'api12345', '123qweasd', function (error, result) {     
    // check user authentication 
    var success = result.success; 
    console.log(result); 
    // user has account from lpgp site, let's save him to meteor. 
    if (success){ 
     console.log('success'); 
     var session_id = result.session_id; 
     //console.log(_session_id); 
     Meteor.call('getUserDetails', 'SESSba071091c09f79fefd66e4884dcdde50', 68558, function (error, result) { 
      if (!error) 
       console.log(result); 
      else 
       console.log(error);   
     });   
    }else 
     // app can't find user account from lpgp site. 
     console.log(error);    
}); 
}, 5000); 
}); 

调用成功,但刚刚返回成功:false。

响应:

Object {statusCode: 200, content: "{"success":false}", headers: Object, data: Object} 

回答

3

在服务器侧流星的HTTP模块仅仅是用于所述npm module named request的包装。 request npm模块包含支持指定您自己的cookie以及将它们保存到cookie jar(只需按照链接并搜索'cookie')。默认的cookie jar是tough-cookie,有趣的是,Meteor包含它,尽管我没有看到从Meteor.HTTP使用它的任何方式。

的这些实施细则的结果是,你可以直接使用request。我采取了类似的方法来包装要求作为流星的HTTP模块,但代替的是HTTP提供的选项受限子集,我的包装可以完全访问的requesttough-cookie所有的能力。很酷的部分是,你甚至不需要直接添加request作为你自己的依赖,因为它已经是流星的依赖。当然,风险在于Meteor的更高版本可能会使用除request之外的其他东西,并且您的代码将会中断。

不管怎么说,这是我自己的包装为request。它包含一个用于进行Jenkins API调用的JSessionID cookie支持的示例。只需将其放入syncRequest.coffee文件夹下的\server文件夹中,并确保已添加coffeescript程序包(Meteor add coffeescript)...或编译我的代码并将其保存到\server文件夹中的.js文件中。

request = Npm.require('request') 

populateData = (response) -> 
    contentType = (response.headers["content-type"] or ";").split(";")[0] 
    if _.include([ "application/json", "text/javascript" ], contentType) 
    try 
     response.data = JSON.parse(response.content) 
    catch err 
     response.data = null 
    else 
    response.data = null 

normalizeOptions = (uri, options, callback) -> 
    unless uri? 
    throw new Error("undefined is not a valid uri or options object.") 

    if (typeof options is "function") and not callback 
    callback = options 

    if options and typeof options is "object" 
    options.uri = uri 
    else if typeof uri is "string" 
    options = uri: uri 
    else 
    options = uri 

    return {options, callback} 

normalizeResponse = (error, res, body) -> 
    response = null 
    unless error 
    response = {} 
    response.statusCode = res.statusCode 
    response.content = body 
    response.headers = res.headers 
    populateData(response) 
    if response.statusCode >= 400 
     error = makeErrorByStatus(response.statusCode, response.content) 
    return {error, response} 

wrappedRequest = (uri, options, callback) -> 
    {options, callback} = normalizeOptions(uri, options, callback) 
    request(options, (error, res, body) -> 
    {error, response} = normalizeResponse(error, res, body) 
    callback(error, response) 
) 

wrappedCall = (method, uri, options, callback) -> 
    options.method = method 
    wrappedRequest(uri, options, callback) 

wrappedGet = (uri, options, callback) -> wrappedCall("GET", uri, options, callback) 
wrappedPost = (uri, options, callback) -> wrappedCall("POST", uri, options, callback) 
wrappedPut = (uri, options, callback) -> wrappedCall("PUT", uri, options, callback) 
wrappedDelete = (uri, options, callback) -> wrappedCall("DELETE", uri, options, callback) 

getWithJSession = (j_username, j_password, securityCheckUri, uri, callback) -> 
    request = request.defaults({jar: true}) 

    form = {j_username, j_password} 

    request.post({uri: securityCheckUri, form: form}, (error, response, body) -> 
    if error? 
     throw new Error(error) 
    else if response.statusCode isnt 302 
     throw new Error("Expected response code 302 (forward). Got #{response.statusCode}") 
    else 
     request.get(uri, (error, res, body) -> 
     {error, response} = normalizeResponse(error, res, body) 
     callback(error, response) 
    ) 
) 

syncRequest = Meteor.wrapAsync(wrappedRequest) 
syncRequest.call = Meteor.wrapAsync(wrappedCall) 
syncRequest.get = Meteor.wrapAsync(wrappedGet) 
syncRequest.post = Meteor.wrapAsync(wrappedPost) 
syncRequest.put = Meteor.wrapAsync(wrappedPut) 
syncRequest.delete = Meteor.wrapAsync(wrappedDelete) 
syncRequest.del = syncRequest.delete 

syncRequest.getWithJSession = Meteor.wrapAsync(getWithJSession) 
syncRequest.getWithJsession = syncRequest.getWithJSession 

(exports ? this).syncRequest = syncRequest