通过Ruby写入Jira6 API(错误415)

时间:2014-01-15 17:50:44

标签: ruby api http rest jira

我对红宝石很新。说实话,我第一次尝试用http连接做一个ruby脚本。我迷失了一点。通过POST将数据发送到Jira6。这是我使用的代码

# If issues_id.count != 0 make a transition of the issues
if issue_id.count > 0
    issue_id.each do |id|
        transition_data = '{"transition": {"id": "666"}}'

        Net::HTTP.start(jira_domain, jira_port) do |http|
            ap jira_transition + id + jira_transition_query
            request = Net::HTTP::Post.new(jira_transition + id + jira_transition_query)
            request.basic_auth jira_user, jira_pass
            request["Content-Type"] = "application/json"
            ap transition_data
            request.set_form_data('data' => '{"transition": {"id": "841"}}');

            response = http.request(request)

            ap response.code
            ap response
        end
    end
end

测试会导致以下错误:

#<Net::HTTPUnsupportedMediaType 415 Unsupported Media Type readbody=true> error.

当我尝试使用curl时,它可以正常工作

curl -D- -u external_user:external_pass -X POST --data '{"transition": {"id": "841"}}' -H "Content-Type: application/json" http://jira.demo.com:80/rest/api/2/issue/17399/transitions\?expand\=transitions.fields

为了确保我没有像google群组那样获得相同的垃圾答案: 是的,我在成功尝试后重置了这个问题:-) 是的,transitionId 841是正确的: - )

有人可以向我发送如何将数据发送到Jira6 REST-API的正确方向吗?我认为这是一个边际错误,但我不承认。 非常非常感谢你。

2 个答案:

答案 0 :(得分:0)

我建议你试试jira-ruby gem

答案 1 :(得分:0)

我遇到了同样的问题。出于某种原因,您不能使用'request [“Content-Type”] =“application / json”'。您必须创建一个新的标头变量,然后将其作为第二个参数传递给Post.new。然后使用request.body包含更新参数。

if issue_id.count > 0
    issue_id.each do |id|
        transition_data = '{"transition": {"id": "666"}}'

        Net::HTTP.start(jira_domain, jira_port) do |http|
            ap jira_transition + id + jira_transition_query
            header = {'Content-Type': 'application/json'}
            request = Net::HTTP::Post.new(jira_transition + id + jira_transition_query, header)
            request.basic_auth jira_user, jira_pass
            #request["Content-Type"] = "application/json"
            ap transition_data
            #request.set_form_data('data' => '{"transition": {"id": "841"}}');
            params = {transistion: {id: 841}}
            request.body = params.to_json

            response = http.request(request)

            ap response.code
            ap response
        end
    end
end