Json Post从Django到Camunda [英] Json Post from Django to Camunda

查看:93
本文介绍了Json Post从Django到Camunda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我的上一篇文章的进一步内容:使用Django向外部Rest Service发布请求-使用返回的json更新模型

Further to my earlier post yesterday: Post request to external Rest Service using Django - use returned json to update model

使用Django将数据发布到camunda-request.post。使用以下脚本:

I have managed to post data to camunda using Django - request.post. Using the following script:

payload = "{\n    \"businessKey\": \"SomeValue\",\n    \"variables\": {\n        \"Organisation_ID\": {\n            \"value\": \"SOmeUUID\",\n            \"type\": \"String\"\n        },\n        \"UserID\": {\n            \"value\":\"Some User ID\",\n            \"type\": \"String\"\n        }\n    }\n}"

但是,当我开始使用表单中的变量并使用

However when I start to use variables from the form and format my payload using

class StartProcessView(View): 
template_name = 'startdeliveryphase.html'
def get(self,request, *args, **kwargs):
    form = IntStartDeliveryPhase
    return render(request, self.template_name,{'form':form})

def post(self,request, *args, **kwargs):      
    form = IntStartDeliveryPhase(request.POST or None)
    if form.is_valid():

        data = form.cleaned_data
        OrganisationID = data['Form_Field_OrganisationID']
        UserID = data['Form_Field_User_ID']
        BusinessKey = data['Form_Field_Business_Key']
        url = "http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start"
        payload  =  {"businessKey":BusinessKey,"variables":[{"Organisation":[{"value":OrganisationID, "type":"String"}]},[{"Startedby":[{"value":UserID,"type":"String"}]}]]}


        headers = {
        'Content-Type': 'application/json'
        }
        response = requests.request("POST", url, headers=headers, data = payload)
        #print(repsonse.errors)
        print(response.text.encode('utf8'))
        return render(request)    
    else:
        return render(request,self.template_name,{'form':form})

我从camunda引擎收到错误:-

I get an error from the camunda engine:-

b'{"type":"JsonParseException","message":"Unrecognized token \'businessKey\': was expecting (\'true\', \'false\' or \'null\')\\n at [Source: (org.camunda.bpm.engine.rest.filter.EmptyBodyFilter$1$1); line: 1, column: 13]"}'

本地变量显示以下内容:

the local vars shows the following:

▼ Local vars
Variable    Value
BusinessKey 
'1qaz'
OrganisationID  
<Organisation: Some Local Authoristy>
UserID  
<Actor_User: me@me.com>
args    
()
data    
{'Form_Field_Business_Key': '1qaz',
 'Form_Field_CamundaInstanceID': 'sss',
 'Form_Field_Camunda_HRef': 'ss',
 'Form_Field_Camunda_TenantID': '22',
 'Form_Field_DateCreated': datetime.datetime(2020, 4, 23, 19, 22, 30, tzinfo=<StaticTzInfo 'GMT'>),
 'Form_Field_OrganisationID': <Organisation: Some Local Authoristy>,
 'Form_Field_User_ID': <Actor_User: me@me.com>}
form    
<IntStartDeliveryPhase bound=True, valid=True, fields=(Form_Field_OrganisationID;Form_Field_DateCreated;Form_Field_CamundaInstanceID;Form_Field_Camunda_HRef;Form_Field_Camunda_TenantID;Form_Field_User_ID;Form_Field_Business_Key)>
headers 
{'Content-Type': 'application/json'}
kwargs  
{}
payload 
{'businessKey': '1qaz',
 'variables': [{'Organisation': [{'type': 'String',
                                  'value': <Organisation: Some Local Authoristy>}]},
               [{'Startedby': [{'type': 'String',
                                'value': <Actor_User: me@me.com>}]}]]}
request 
<WSGIRequest: POST '/bimProcess/'>
response    
<Response [400]>
self    
<bimProcess.views.StartProcessView object at 0x055B7898>
url 
'http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start'

如何获得camunda要求的正确格式,可以在变量中插入所需的双引号

How do I get the correct format as required by camunda into which I can insert my variables with the required double quotes

编辑Yay-我终于有了正确的序列!!!!!!

 def post(self,request, *args, **kwargs):      
    form = IntStartDeliveryPhase(request.POST or None)
    if form.is_valid():

        data = form.cleaned_data
        OrganisationID = str(data['Form_Field_OrganisationID'])
        UserID = str(data['Form_Field_User_ID'])
        BusinessKey = data['Form_Field_Business_Key']
        url = "http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start"
        payload =  {"businessKey":BusinessKey,"variables":{"Organisation":{"value":OrganisationID, "type":"String"},"Startedby":{"value":UserID,"type":"String"}}}
        headers = {
        'Content-Type': 'application/json'
        }
        payload2 = json.dumps(payload)
        print (payload2)

        response = requests.request("POST", url, headers=headers, data=payload2)
        #print(repsonse.errors)
        print(response.text.encode('utf8'))
        return render(request)    
    else:
        return render(request,self.template_name,{'form':form})

现在接下来的问题是:

1)我明白了来自Camunda的200响应:发布请求返回的有效载荷需要返回到表单数据,然后可以在不中断用户的情况下将其保存。

1) I get a response from Camunda as a 200: the payload back from the post request needs to go back into the form data where it can then be saved without user interruption.

2)我正在此表单上进行自我发布-实现顺序流程的最佳方法是什么?我应该重定向并通过数据传递,还是有更有效的方法?还有没有比我发布的更好的方法来实现view.py?

2)I am doing a post self on this form - what is the best way of achieving the sequence flow? should I do a redirect and pass the data through or is there a more efficient way? Also is there a better way to achieve the view.py than I have posted which is more efficient?

推荐答案

def帖子(自我,请求,* args,** kwargs):

的形式= IntStartDeliveryPhase (request.POST或无)
,如果form.is_valid():

def post(self,request, *args, **kwargs):
form = IntStartDeliveryPhase(request.POST or None) if form.is_valid():

    data = form.cleaned_data
    OrganisationID = str(data['Form_Field_OrganisationID'])
    UserID = str(data['Form_Field_User_ID'])
    BusinessKey = data['Form_Field_Business_Key']
    url = "http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start"
    payload =  {"businessKey":BusinessKey,"variables":{"Organisation":{"value":OrganisationID, "type":"String"},"Startedby":{"value":UserID,"type":"String"}}}
    headers = {
    'Content-Type': 'application/json'
    }
    payload2 = json.dumps(payload)
    print (payload2)

    response = requests.request("POST", url, headers=headers, data=payload2)
    #print(repsonse.errors)
    print(response.text.encode('utf8'))
    return render(request)    
else:
    return render(request,self.template_name,{'form':form})

这篇关于Json Post从Django到Camunda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆