【问题标题】:Combining two POST requests using httr使用 httr 组合两个 POST 请求
【发布时间】:2018-12-08 10:09:41
【问题描述】:

我需要创建一个 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 的一部分。

【问题讨论】:

    标签: r httr exact-online


    【解决方案1】:

    一般来说,在 Exact Online 上,您可以使用 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 环境变量启用跟踪日志记录。 启动invantive control或其他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 
    

    结果:

    原生跟踪:

    [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. 
    

    还有更多消息。

    【讨论】:

    • 那么我如何“将这些行作为消息的一部分发送”?你的意思是这样的:body = list(Supplier = "SupplierName", PurchaseOrderID = "999999", PurchaseOrderLines = c(Item = "ItemCode", PurchaseOrderId = "999999", QuantityInPurchaseUnits = "1.0"))?
    • API 是 rest so Json。最好根据您选择的语言添加 Json 数据集,或尝试描述的场景以获得一些工作负载,然后使用您的语言模仿该 Json。
    • 我在 POST 请求中使用encode = "json",因此无需将行添加为 Json。
    • 添加了示例。 encode = json 是 R 的东西,我可能会赌它的作用,但不知道。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-22
    相关资源
    最近更新 更多