小编Fra*_*ath的帖子

如何使用Python/Django的BonitaSoft REST API

我在网上找不到关于如何做到这一点的信息,所以我决定发布我是如何做到这一点的.请随时分享您的建议或经验.

首先,在settings.py中,我设置了一些我可以重用的变量.如果您在本地使用Bonita Studio,则这些是默认设置.

BPM_HOST = 'http://localhost:9090/bonita-server-rest/'
BPM_USERNAME = 'restuser'
BPM_PASSWORD = 'restbpm'
Run Code Online (Sandbox Code Playgroud)

在views.py中,我设置了一个我可以随时使用的功能.它使用设置文件中的变量并接受登录用户的参数,要调用的url和post_data的字典.它以Bonitasoft期望的方式设置Basic身份验证和内容类型标头.

from django.conf import settings
import urllib
import urllib2
import base64

def restcall(user,url,post_data={}):
    #create bpm_request
    bpm_request = urllib2.Request(settings.BPM_HOST + url)

    #encode username and password and add to header
    authKey = base64.b64encode(settings.BPM_USERNAME + ':' + settings.BPM_PASSWORD)
    bpm_request.add_header("Authorization","Basic " + authKey)

    #add content type to header
    bpm_request.add_header("Content-Type","application/x-www-form-urlencoded")

    #must send current user in options
    current_user = 'user:' + 'user'
    post_data['options'] = current_user

    bpm_request.add_data(urllib.urlencode(post_data))

    response = urllib2.urlopen(bpm_request)

    try:
        return response
    except Exception, …
Run Code Online (Sandbox Code Playgroud)

python django rest

5
推荐指数
0
解决办法
1062
查看次数

标签 统计

django ×1

python ×1

rest ×1