使用C#和Flurl对Joplin REST API进行多部分POST。

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

我目前正在开发一个控制台应用,使用C#和Flurl将数据导入Joplin for Windows 10。 此处.

我试图在Joplin中为我系统中的一个文件创建一个新的资源,这样它就可以附加到Joplin笔记中。

通过CURL,我可以使用命令创建资源。

curl -F  "data=@c:\\temp\\Test.pptx" -F  "props={\"title\":\"my resource title\"}" http://localhost:41184/resources?token=MyToken

(注意:它只适用于 "data=@c:\tempTest.pptx",而不是 "data=c:\tempTest.pptx")

当我用c#中的Flurl尝试这样做时,我从Joplin得到了400的响应,在日志中我发现。

错误,在Api.action_resources(C:\\Program\\)处没有文件就不能创建资源。Resource cannot be created without a file at Api.action_resources (C:\Program Files\Joplin\resources\app.asar\lib\services\restApi.js:351:37) at Api. route (C:\\Program Files\Joplin\resources\app.asarlib\services\restApi.js:140:42) at execRequest (C:\\\Program Files\Joplin\resources\app.asarlib\ClipperServer. js:157:39)在C:\\Program Files\Joplin\resources\app.asar\lib\ClipperServer.js:185:8在C:\\\Program Files\Joplin\resources\app.asar\node_modules\multiparty\index. js:136:9 at C:\Program Files\Joplin\resources\app.asar\node_modules\multiparty/index.js:115:9 at processTicksAndRejections (internalprocesstask_queues.js:75:11)"

目前我已经试过了。

        try
        {
            var url = BaseUrl
                .WithHeader("User_Agent", browserUserAgent)
                .AppendPathSegment("resources")
                .SetQueryParam("token", Token);

            using (var fs = new FileStream("c:\\temp\\Test.pptx", FileMode.Open, FileAccess.Read))
            {
                var resource = url.PostMultipartAsync(mp => mp
                        .AddJson("props", new { title = "test title" })
                        .AddFile("data", fs, "Test.pptx", "application/octet-stream")
                        )
                    .ReceiveJson<JoplinResource>()
                    .Result;
            }
        }

还有..:

        try
        {
            var url = BaseUrl
                .WithHeader("User_Agent", browserUserAgent)
                .AppendPathSegment("resources")
                .SetQueryParam("token", Token);

            var resource = url.PostMultipartAsync(mp => mp
                    .AddJson("props", new { title = "test title" })
                    .AddFile("data", "c:\\temp\\Test.pptx")
                    )
                .ReceiveJson<JoplinResource>()
                .Result;
        }

我连接了fiddler,看看我的应用和CURL有什么不同。

Curl。

POST http://127.0.0.1:41184/resources?token=MyToken HTTP/1.1
Host: 127.0.0.1:41184
User-Agent: curl/7.70.0
Accept: */*
Connection: Keep-Alive
Content-Length: 33648
Content-Type: multipart/form-data; boundary=------------------------91ab181cbb0247ba

--------------------------91ab181cbb0247ba
Content-Disposition: form-data; name="props"

{"title":"my resource title"}
--------------------------91ab181cbb0247ba
Content-Disposition: form-data; name="data"; filename="Test.pptx"
Content-Type: application/octet-stream
...

我的Console应用

POST http://localhost:41184/resources?token=MyToken HTTP/1.1
User_Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
Content-Type: multipart/form-data; boundary="f603841b-5c32-4e77-985a-69c2ffb6eed0"
Host: localhost:41184
Content-Length: 33612
Expect: 100-continue
Accept-Encoding: gzip, deflate

--f603841b-5c32-4e77-985a-69c2ffb6eed0
Content-Disposition: form-data; name=props

{"title":"My Resource"}
--f603841b-5c32-4e77-985a-69c2ffb6eed0
Content-Disposition: form-data; name=data; filename=Test.pptx; filename*=utf-8''Test.pptx
...

请注意这些区别

  1. 使用CURL时,道具和数据都是引号,而不是FLURL的引号
  2. FLURL会发送第二个文件名。filename*=utf-8''Test.pptx

我如何才能让它正常工作?

c# rest multipart flurl
1个回答
0
投票

问题在于 "data "和 "props "的引号缺失。

    try
    {
        var url = BaseUrl
            .WithHeader("User_Agent", browserUserAgent)
            .AppendPathSegment("resources")
            .SetQueryParam("token", Token);

            var resource = url.PostMultipartAsync(mp => mp
                    .AddJson("\"props\"", new { title = "My Resource" })
                    .AddFile("\"data\"", "c:\\temp\\Test.pptx")
                    )
                .ReceiveJson<JoplinResource>()
                .Result;
    }

原始请求头是现在。

POST http://localhost:41184/resources?token=MyToken HTTP/1.1
User_Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36
Content-Type: multipart/form-data; boundary="c6b2377a-1240-4ae3-872f-fa24b643d3e0"
Host: localhost:41184
Content-Length: 33616
Expect: 100-continue
Accept-Encoding: gzip, deflate

--c6b2377a-1240-4ae3-872f-fa24b643d3e0
Content-Disposition: form-data; name="props"

{"title":"My Resource"}
--c6b2377a-1240-4ae3-872f-fa24b643d3e0
Content-Disposition: form-data; name="data"; filename=Test.pptx; filename*=utf-8''Test.pptx
...

Joplin REST服务创建了一个新的资源... ...

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