使用Django将请求发布到外部Rest Service-使用返回的json更新模型 [英] Post request to external Rest Service using Django - use returned json to update model
问题描述
我需要一些使用Camunda Rest API和Django实现以下细节的最佳方法的建议:
I require some advice on the best way to implement the following details using Camunda Rest API and Django:
1)向用户显示一个表单-选择详细信息,然后使用以下命令向camunda发出POST请求' http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start '
1) User is presented with a form - Selects the details and then does a POST request to camunda using 'http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start'
此POST请求中发送的详细信息由JSON RAW中的3个变量组成:形式:
the details sent in this POST request consist of 3 variables in a JSON RAW : in the form of :
{"variables":
{"UserID" : {"value" : "user.", "type": "String"},
"OrganisationID" : {"value" : "some value", "type": "String"}
},
"businessKey" : "SomeBusinessKey"
}
from views.py
from views.py
从django.views.generic导入TemplateView从.forms导入StartProject
from django.shortcuts import render from django.views.generic import TemplateView from .forms import StartProject
import requests
class StartProcessView(TemplateView):
template_name = 'startdeliveryphase.html'
def get(self, request):
form = StartProject()
return render(request, self.template_name, {'form':form})
def post(self,request):
form = StartProject()
url = "http://localhost:8080/engine-rest/process-definition/key/Process_B_PerProject/start"
payload = "{\"variables\":\r\n {\"Username\" : {\"value\" : \"[form.Username]\", \"type\": \"String\"},\r\n \"OrganisationInitiating\" : {\"value\" : \"[form.OrganisationInitiator]\", \"type\": \"String\"}},\r\n \"businessKey\" : {form.businessKey}\r\n }"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = payload)
return render(response, self.template_name, {'form':form})
响应以200的形式返回,并带有JSON负载:
The response is returned as a 200 along with a JSON payload in the form:
{
"links": [
{
"method": "GET",
"href": "http://localhost:8080/engine-rest/process-instance/fbff8359-84cd-11ea-a2e1-00155d891509",
"rel": "self"
}
],
"id": "fbff8359-84cd-11ea-a2e1-00155d891509",
"definitionId": "Process_B_PerProject:1:71921815-737c-11ea-91a6-00155d891509",
"businessKey": "SomeBusinessKey",
"caseInstanceId": null,
"ended": false,
"suspended": false,
"tenantId": null
}
问题1-从这一部分开始-如何将表单中的变量放入有效负载中:我尝试过的方法得到500响应-所以这里出了点问题.
Question 1 - from this part - how do I get the variables from the form into the payload: the method I have tried gets a 500 response - so something going wrong here.
问题2-使用响应来更新模型的方法是什么?
Question 2 - what is the method to use the response to update a model?
推荐答案
问题1:使用 response.json()
从响应中获取json,查看请求文档
Question 1:
Use response.json()
to get json from response, review Request doc
问题2:为了保存,请使用 create或update方法
创建或更新Django
Question 2:
For save use create or update method
Create or update django
这篇关于使用Django将请求发布到外部Rest Service-使用返回的json更新模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!