代码之家  ›  专栏  ›  技术社区  ›  Stan

使用httr组合两个POST请求

  •  0
  • Stan  · 技术社区  · 6 年前

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

    所以有两个不同的POST请求,不能单独执行。我怎样才能把这两个要求结合起来呢?

    Exact Online . 他们有一个 REST API documentation here . 有两个强制属性:“Supplier”和“PurchaseOrderLines”。供应商很容易说明,但采购订单行是用 a second POST request .

    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 Post的一部分。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Guido Leenders    6 年前

    通常,在Exact Online上,您可以使用OData批处理组合多个请求。这只在流量到达一个端点(URL)时有效。

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

    请检查 https://start.exactonline.nl/docs/HlpRestAPIResourcesDetails.aspx?name=PurchaseOrderPurchaseOrders 并在文本中搜索“集合”。

    需要插入行(至少1行)。

    如果不知道确切的语法,请使用 insert into exactonlinerest..purchaseorders ... identified by ... select * from sessionios@datadictionary 你会找到真正的电话。启用本机跟踪时,可以在“调试跟踪”视图中看到发送到Exact Online的负载。

    另外,对于上传数据,我通常更喜欢使用xmlapi。它更快更聪明。

    运行样本

    使用INVANTIVE\u trace\u ACTIVE=true环境变量启用跟踪日志记录。 启动Inventive控件或其他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. 
    

    还有更多的信息。