簡體   English   中英

如何從 Coffee 腳本中的 HTTP 響應訪問“位置”?

[英]How can I access 'Location' from HTTP response in Coffee script?

我正在嘗試使用 Hubot 將 Jenkins 與 Slack 集成。 我在Hubot腳本中找到了 jenkins.coffee 腳本,它對我計划做的工作很好。 現在,我想在運行命令從 Slack 構建它后獲取觸發作業的構建號。 為此,我想從 http 響應中獲取“位置”。

這是 function 當我說@Hubot jenkins build <job name>

jenkinsBuild = (msg, buildWithEmptyParameters) ->
    url = process.env.HUBOT_JENKINS_URL
    job = querystring.escape msg.match[1]
    params = msg.match[3]
    command = if buildWithEmptyParameters then "buildWithParameters" else "build"
    path = if params then "#{url}/job/#{job}/buildWithParameters?#{params}" else "#{url}/job/#{job}/#{command}"

    req = msg.http(path)

    if process.env.HUBOT_JENKINS_AUTH
      auth = new Buffer(process.env.HUBOT_JENKINS_AUTH).toString('base64')
      req.headers Authorization: "Basic #{auth}"

    req.header('Content-Length', 0)
    req.post() (err, res, body) ->
        if err
          msg.reply "Jenkins says: #{err}"
        else if 200 <= res.statusCode < 400 # Or, not an error code.
          msg.reply "(#{res.statusCode}) Build started for #{job} #{url}/job/#{job}"
        else if 400 == res.statusCode
          jenkinsBuild(msg, true)
        else if 404 == res.statusCode
          msg.reply "Build not found, double check that it exists and is spelt correctly."
        else
          msg.reply "Jenkins says: Status #{res.statusCode} #{body}"

我到底應該在msg.reply中寫什么來獲取位置?

蒂亞:)

我終於用res.headers["location"]做到了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM