使用RestSharp向SAP Business One服务层发送批量REST请求。

问题描述 投票:0回答:1

我正试图使用SAP Business One服务层批处理来实现REST事务.首先,在处理实际的数据修改请求之前,我想创建一个简单的职位,以便在同一个请求中获得两个业务伙伴。我试着写了一个c#应用程序,但我总是得到同样的错误。

{ "error" : { "code" : 242, "message" : { "lang" : "en-us", "value" : "Incomplete batch request body." } }

这是我使用的代码

        public IRestResponse TestMultipart()
        {
            var client = new RestClient(new Uri(SERVICE_ROOT_URL));
            client.CookieContainer = CookieCnt;
            var request = new RestRequest("$batch", Method.POST)
            {
                AlwaysMultipartFormData = true
            };

            request.AddHeader("Content-Type", "multipart/mixed;boundary=batch_test1");

            request.AddBody(@"--batch_test1
Content-Type: application/http
Content-Transfer-Encoding:binary
GET /b1s/v1/Invoices(1)
--batch_batch_test1
Content-Type: application/http
Content-Transfer-Encoding:binary
GET /b1s/v1/Invoices(2)
--batch_test1--");

            return client.Execute(request);
        }

(这里不包括认证代码,调用Login API并初始化CookieContainer,效果很好。) 我可以使用标准的GET方法获得单次(而不是多次)请求的响应)。

我无法在网上找到使用RestSharp库的多部分请求的示例。有谁能帮忙吗?

谢谢。

sap restsharp multipart
1个回答
1
投票

好吧,原来我把整个事情弄错了。我决定使用HttpClient .Net本地API,它允许非常灵活地创建Multipart REST请求。由于我找不到像样的文档来介绍如何创建多部分混合内容,而且把所有的部分组合在一起需要一点头绪,所以我把代码发布在这里,以备有人觉得有用。这是请求处理程序,它驻留在一个 SessioneSL 类,其中构造函数打开服务层会话,并将cookie存储到CookieContainer实例中,CookieContainer是该类的一个属性(这仍然是在RestSharp中完成的,因此我不会公布它。我很快就会把它转换为HttpClient版本,如果你需要的话可以问一下)。)

    public Task<string> TestMultipartHC()
    {
        using (var handler = new HttpClientHandler()
        {
            CookieContainer = CookieCnt,
            Proxy = new WebProxy("127.0.0.1", 8888),    // this is for catching the POST in Fiddler
        })
        {
            HttpClient client = new HttpClient(handler)
            {
                BaseAddress = new Uri(SERVICE_ROOT_URL),
            };

            var myBoundary = "--batch_test1";
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "$batch");

            request.Headers.Date = DateTime.UtcNow;
            request.Headers.Add("Accept", "application/json; charset=utf-8");

            MultipartContent mpContent = new MultipartContent("mixed", myBoundary);
            var sc = new StringContent("GET /b1s/v1/Invoices(1)");
            sc.Headers.ContentType = new MediaTypeHeaderValue("application/http");

            sc.Headers.Add("Content-Transfer-Encoding", "binary");
            mpContent.Add(sc);

            sc = new StringContent("GET /b1s/v1/Invoices(2)");
            sc.Headers.ContentType = new MediaTypeHeaderValue("application/http");
            sc.Headers.Add("Content-Transfer-Encoding", "binary");
            mpContent.Add(sc);

            request.Content = mpContent;

            var response = client.SendAsync(request).Result;
            return response.Content.ReadAsStringAsync();
        }
    }

要调用这个方法,你将需要这样的代码。

SessioneSL sessione = new SessioneSL();
Task<string> tk = sessione.TestMultipartHC();
tk.Wait();
Response.Write(tk.Result);

UPDATE 20200506:上面的示例实际上并没有返回两个对象,只是返回了第一个对象,我不知道为什么.无论如何,事务功能只发生在一个多部分段内嵌套的 "Changeset "中。Changeet只允许POST、PUT、DELETE、PATCH动词,不允许get。SAP的文档很糟糕,所以我还得做试验。

© www.soinside.com 2019 - 2024. All rights reserved.