简体   繁体   English

无法从函数返回响应

[英]Unable to return response from function

I am working on a simple script to return a JIRA Case ID after an issue is submitted to JIRA. 在将问题提交给JIRA之后,我正在使用一个简单的脚本来返回JIRA案例ID。

I have a created an issue function that takes JSON parameters from a RESTful web service and it then passes on said parameters to create a case in JIRA automatically. 我创建了一个问题函数,该函数从RESTful Web服务中获取JSON参数,然后传递所述参数以在JIRA中自动创建案例。 Here's a snippet of the function I've written: 这是我编写的函数的一个片段:

 def create_issue():

    issue = {
        'summary': request.json['summary'],
        'project': request.json['project'],
        'desc': request.json['desc'],
        'issuetype': request.json['issuetype']
    }

    issue_dump= json.dumps(issue)
    issue_source = json.loads(issue_dump)

    summ = (issue_source["summary"])
    proj = (issue_source["project"])
    desc = (issue_source["desc"])
    type = (issue_source["issuetype"])

    print(f"{summ}")
    print(f"{proj}")
    print(f"{desc}")
    print(f"{type}")


    issue_dict = {
            'project': proj,
            'summary': summ,
            'description':desc,
            'issuetype':type,
    }

    new_issue = jira.create_issue(fields=issue_dict)

    #Used to print response of new_issue
    print(f"{new_issue}")

    response = {'JIRA ID': new_issue}

    return jsonify(response),201

I added a print statement of new_issue and I see the JIRA ID (eg ID-2) does appear on the console, but for some reason when I try to include it into my JSON response as a variable, I get the Object of type Issue is not JSON serialisable. 我添加了一条new_issueprint语句,并且看到JIRA ID(例如ID-2)确实出现在控制台上,但是由于某些原因,当我尝试将其作为变量包含在JSON响应中时,我得到了Issue类型的Object不支持JSON可序列化。 I have tried substituting the new_issue variable in my response JSON with another defined test variable and it works just fine. 我尝试用另一个已定义的测试变量替换响应JSON中的new_issue变量,并且效果很好。

I am using Flask to host the web services. 我正在使用Flask托管Web服务。

Am I missing entirely on something here? 我在这里完全错过了什么吗?

From the code behind the Jira library it looks like the create_issue method returns an Issue object which contains lots of information. 从Jira库后面的代码中,看起来create_issue方法返回了一个Issue对象,其中包含很多信息。 When you try to print it, it prints the first human readable value it can find in the object. 当您尝试打印它时,它将打印在对象中可以找到的第一个人类可读值。

Try this instead: 尝试以下方法:

response = {'JIRA ID': new_issue.key}
return jsonify(response),201

The code for the returned Issue class can be found here beginning on line 404 此处的第404行开始,可以找到返回的Issue类的代码

I can't add a comment. 我无法添加评论。 So could you please share what type does new_issue return?. 所以,请您分享一下new_issue返回的类型是什么?

print(type(new_issue))

JSON requires a key, value Pair. JSON需要键,值对。 Since you've unpacked the new issue variable inside another dict the types are probably incompatible. 由于您已将新问题变量解压缩到另一个字典中,因此这些类型可能不兼容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM