当前位置: 首页 > 知识库问答 >
问题:

Json从Django到Camunda的帖子

皇甫才良
2023-03-14

关于我昨天的文章:使用Django将请求发送到外部Rest服务-使用返回的json来更新模型

我已经设法使用django-request.post向camunda发布了数据。使用以下脚本:

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}"
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引擎得到一个错误:-

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]"}'

本地变量显示如下:

▼ 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所需的正确格式,以便在其中插入带有所需双引号的变量

 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个答案

赫连明诚
2023-03-14

def post(self,request,*args,**kwargs):
form=IntStartDeliveryPhase(request.post或None)如果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})
 类似资料:
  • 我想像本文所描述的那样从UserTask中挂钩事件:https://docs.camunda.org/manual/latest/user-guide/spring-boot-integration/the-spring-event-bridge/ 我的听众: 我添加了属性: 谢了。

  • 我试图从邮递员这里发送json正文,我有简单的php代码获取json正文 当我从邮递员那里发送json尸体时,它给出了这些东西 注意:未定义的偏移量:第7行D:\Xampp\htdocs\Ass\index.php中的0 数组 那么如何从php文件中获取json值呢?

  • 我的Rest控制器 我的异常处理无效的原始 json 帖子。我尝试使用请求。 IllegalStateException:已经为此请求调用了getInputStream() 我想在这个异常上保留日志输入原始json数据。 有人能帮我吗?谢谢。 更新 作为@Sean Carrol的建议。我尝试按照建议使用HttpServletRequest estWrapper,但它仍然不起作用。 我在InputS

  • 我用的是WordPress的插件json-api,它有一个获取最近帖子的功能。我假设它只在过去24小时内被调用,但是我想修改它,这样我就可以给它一个参数来专门拉过去24小时或1天,然后2天或48小时,等等..... 这是插件:https://wordpress.org/plugins/json-api/ 如果需要,我也可以修改插件。 这是它的函数: 下面是指向get_recent_posts插件的

  • 基本上,我希望能够发布一个表单与一些字段(JSON)和附件(多部分)。下面是实际工作的代码,问题是我不喜欢它,所以它主要功能是因为变通方法。 null

  • 问题内容: 在Django views.py文件中处理POST请求时,有时需要将其重定向到另一个URL。我重定向到的网址是由同一Django views.py文件中的另一个函数处理的。有办法做到这一点并维护原始POST数据吗? 更新:我为什么要这样做的更多解释。我有两个Web应用程序(我们称它们为AppA和AppB),它们接受用户在文本字段中输入的数据。当用户单击提交时,将处理数据并显示详细结果。