简体   繁体   English

如何从 Coffee 脚本中的 HTTP 响应访问“位置”?

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

I'm trying to integrate Jenkins with Slack, using Hubot.我正在尝试使用 Hubot 将 Jenkins 与 Slack 集成。 I've found jenkins.coffee script in Hubot scripts and it works fine for what I was planning on doing.我在Hubot脚本中找到了 jenkins.coffee 脚本,它对我计划做的工作很好。 Now, I would like to get the Build number of a triggered job after I run a command to build it from Slack.现在,我想在运行命令从 Slack 构建它后获取触发作业的构建号。 In order to do that, I would like to get the 'Location' from http response.为此,我想从 http 响应中获取“位置”。

This is the function that builds a job when I say @Hubot jenkins build <job name>这是 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}"

What exactly should I write in msg.reply to get the Location?我到底应该在msg.reply中写什么来获取位置?

TIA:)蒂亚:)

I finally did it with res.headers["location"]我终于用res.headers["location"]做到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从我的PHP脚本接收HTTP响应? - How can I receive an HTTP response from my PHP script? HTTP响应对象中缺少数据字段。 我该如何访问? - Data field is missing from HTTP response object. How can I access it? 如何从某些URL获取HTTP标头(位置)? - How can I get HTTP headers (Location) from some URL? 如何在JAX-RS中返回http创建的资源的位置以及响应状态消息? - How can I return the location of http created resource along with a response status message in JAX-RS? 如何阻止Angular $ http反序列化响应正文? - How can I stop Angular $http from deserializing the response body? 如何从HTTP响应获取正文? - How I can get the body from a HTTP response? 我怎样才能最容易地将这个 HTTP 响应解析为 JSON 然后访问 responseText 字段? - How can I most easily parse this HTTP response into JSON to then access responseText fields? 如何使用客户端JavaScript从服务器访问HTTP响应? - How can you access the HTTP response from a server using client-side JavaScript? ROR-从HTTP响应位置提取ID - ROR - Extract ID from HTTP response location 如何评估来自 bash/shell 脚本的 http 响应代码? - How to evaluate http response codes from bash/shell script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM