使用httr合并两个POST请求

时间:2018-12-08 10:09:42

标签: r httr exact-online

我需要创建一个POST请求,该请求需要另一个POST请求。

因此,有两个不同的POST请求,无法单独执行。我将如何合并这两个请求?

作为参考,我需要发布到Exact Online。他们有一个REST API documentation应该可以使事情变得简单。在我的示例中,我想创建一个新的采购订单,找到here。有两个必填属性:“ Supplier”和“ PurchaseOrderLines”。供应商很容易陈述,但是采购订单行是用a second POST request创建的。

例如,对PurchaseOrderLines的POST请求例如如下:

POST(url = paste("https://start.exactonline.nl/api/v1/", division, "/purchaseorder/PurchaseOrderLines", sep = ""),
     add_headers(Key = "authorization", Authorization = paste("Bearer", accessToken, sep = " ")),
     body = list(Item = "ItemCode", PurchaseOrderId = "999999", QuantityInPurchaseUnits = "1.0"),
     encode = "json")

到目前为止,对于实际的PurchaseOrder请求,我有以下信息:

POST(url = paste("https://start.exactonline.nl/api/v1/", division, "/purchaseorder/PurchaseOrders", sep = ""),
     add_headers(Key = "authorization", Authorization = paste("Bearer", accessToken, sep = " ")),
     body = list(Supplier = "SupplierName", PurchaseOrderID = "999999"), encode = "json") 

如文档中所述:“采购订单行不能单独过帐。它们应该是PurchaseOrder过帐的一部分。”

所以问题是:如何使采购订单行成为PurchaseOrder Post的一部分。

1 个答案:

答案 0 :(得分:0)

通常,在“精确在线”上,您可以使用OData批处理合并多个请求。仅当流量到达一个端点(URL)时,此方法才有效。

但是,您的问题本质上更具实用性。确切地要求每笔交易至少要有一行(我知道这很愚蠢,但可以接受)。这适用于用户界面以及反映此业务规则的API。

在PurchaseOrder创建事件中,您需要将行作为消息的一部分发送出去。

请选中https://start.exactonline.nl/docs/HlpRestAPIResourcesDetails.aspx?name=PurchaseOrderPurchaseOrders,然后在文本中搜索“收藏夹”。

需要插入至少1行。

如果语法不正确,请在Invantive SQL中使用insert into exactonlinerest..purchaseorders ... identified by ...,然后运行它。在select * from sessionios@datadictionary中,您会找到实际的呼叫。启用本机跟踪时,您可以在调试跟踪视图中看到发送到Exact Online的有效负载。

作为一个旁注,对于上传数据,我通常更喜欢使用XML API。它更快,更聪明。

运行样本

启动dbgview。 使用INVANTIVE_TRACE_ACTIVE = true环境变量启用跟踪日志记录。 启动主动控制或其他SQL产品。 运行以下代码以在新订单中创建每个销售订单的副本:

set trace-native-calls true

begin transaction

insert into exactonlinerest..salesorders
( description
, orderedby
, deliverto
, deliveryaddress
, invoiceto
)
select 'EDI order '
       || ' van '
       || to_char(sysdate, 'DD-MM-YYYY')
       description
,      sor.orderedby
       label 'Ordered by account ID'
,      sor.deliverto
       label 'Deliver to account ID'
,      sor.deliveryaddress
       label 'Deliver to address ID'
,      sor.invoiceto
       label 'Invoice to account ID'
from   ( select orderid, orderedby, invoiceto, deliverto, deliveryaddress  from exactonlinerest..salesorders ) sor
identified
by     sor.orderid

insert into exactonlinerest..salesorderlines
( description
, item
, quantity
, unitcode
)
select sle.description
,      sle.item
,      sle.quantity
,      sle.unitcode
from   exactonlinerest..salesorderlines sle
attach
to     sle.orderid

commit 

结果: Sales order

本机迹线:

[49784] 15:02:21.82979-45: POST 962 bytes of data to URL '/api/v1/868047/salesorder/SalesOrders'. Disk cache: False/False, memory cache False/False. 
[49784] 15:02:21.82979-45: Native request body on POST to 'https://start.exactonline.nl/api/v1/868047/salesorder/SalesOrders': 
[49784] 15:02:21.82979-45: {"DeliverTo":"3ee8cdbc-e71c-4e8b-b4ee-922e84b05abf","DeliveryAddress":"fa6f3eb2-ba09-4c9f-aeb0-c967bdd8004f","Description":"EDI order  van 08-12-2018","InvoiceTo":"3ee8cdbc-e71c-4e8b-b4ee-922e84b05abf","OrderedBy":"3ee8cdbc-e71c-4e8b-b4ee-922e84b05abf","SalesOrderLines":[{"Description":"Basebal handschoen links","Item":"ee4d1e32-4296-4460-908c-d109b9c2ac77","Quantity":25.0,"UnitCode":"stuk"},{"Description":"Basebal handschoen rechts","Item":"8ee41235-7688-4201-a142-ba56feb393c6","Quantity":10.0,"UnitCode":"stuk"},{"Description":"Basebal","Item":"1f61a71a-4503-407f-8b32-0f63f51a1dae","Quantity":50.0,"UnitCode":"stuk"},{"Description":"Energydrink","Item":"2d37742d-1101-49c6-8c09-59055a808415","Quantity":150.0,"UnitCode":"stuk"},{"Description":"Basebal knuppel","Item":"858cbee5-f597-4333-8679-4083c896cf9f","Quantity":30.0,"UnitCode":"stuk"},{"Description":"Energy reep","Item":"1cb1c87c-2dc4-4be9-8be6-54347403680d","Quantity":100.0,"UnitCode":"stuk"}]} 
[49784] 15:02:21.89579-3: Response status 'OK', length 226. 

还有更多消息。