简体   繁体   English

使用perl REST :: Client和多部分表单将图像发布到Confluence

[英]Use perl REST::Client and multi-part form to post image to Confluence

How can I attach an image to an existing Confluence page, using their latest REST API, from Perl via the REST::Client module? 我如何通过REST :: Client模块使用来自Perl的最新REST API,将图像附加到现有的Confluence页面上?

This may be a perl question (I may be posting an image incorrectly), or a Confluence question (their API docs may be missing a required detail such as a required header that is being quietly added by curl), or both. 这可能是一个perl问题(我可能未正确发布图像),或者是Confluence问题(他们的API文档可能缺少必需的详细信息,例如由curl悄悄添加的必需标头),或两者兼而有之。

I have established a connection object, $client, using the REST::Client module. 我已经使用REST :: Client模块建立了一个连接对象$ client。 I have verified that $client is a valid connection by performing a $client->GET to a known Confluence page ID, which correctly returns the page's details. 我通过对已知的Confluence页面ID执行$ client-> GET来验证$ client是有效的连接,该ID会正确返回页面的详细信息。

I attempt to upload an image, using: 我尝试使用以下方法上传图像:

$headers = {Accept => 'application/json',
        Authorization => 'Basic ' . encode_base64($user . ':' .
        $password),
        X_Atlassian_Token => 'no-check',
        Content_Type => 'form-data',
        Content => 
        [ file => ["file1.jpg"], ]};

$client->POST('rest/api/content/44073843/child/attachment', $headers);

... and the image doesn't appear on the attachments list. ...并且该图像未出现在附件列表中。

I've packet-sniffed the browser whilst uploading an image there, only to find that it uses the prototype API that is being deprecated. 我在浏览器上上传数据包时嗅到了数据包,却发现它使用了不推荐使用的原型API。 I'd hoped that I could just stand on Atlassian's shoulders in terms of seeing exactly what their post stream looks like, and replicating that... but I don't want to use the API that's being deprecated, since they recommend against it. 我希望我能站在Atlassian的肩膀上,准确地看到他们的帖子流是什么样子,然后复制它们……但是我不想使用已被弃用的API,因为他们建议不要这样做。

The curl example of calling the Confluence API to attach a file that they give at https://developer.atlassian.com/confdev/confluence-rest-api/confluence-rest-api-examples , when my host, filename, and page ID are substituted in, does post the attachment. 当我的主机,文件名和页面时,调用Confluence API附加他们在https://developer.atlassian.com/confdev/confluence-rest-api/confluence-rest-api-examples提供的文件的curl示例ID被替换,不会发布附件。

I formerly specified comment in my array of Content items, but removed that during debugging to simplify things since the documentation said it was optional. 我以前在我的Content项目数组中指定了注释,但是在调试过程中为了简化起见而删除了注释,因为文档说这是可选的。

One thing I'm unclear about is getting the contents of the file into the post stream. 我不清楚的一件事是将文件的内容放入帖子流中。 In the curl command, the @ accomplishes that. 在curl命令中,@完成了此操作。 In REST::Client, I'm not sure if I have to do something more than I did, to make that happen. 在REST :: Client中,我不确定是否需要做更多的事情来实现这一目标。

I can't packet-sniff the outgoing traffic because our server only allows https, and I don't know how (or if it's even possible) to set the REST::Client module or one of its underlying modules to record the SSL info to a log file so that Wireshark can pick it up and decode the resulting TLS traffic, the way one can with the environment variable for Chrome or Firefox. 我无法对发送的流量进行数据包嗅探,因为我们的服务器仅允许使用https,而且我不知道如何(或什至有可能)设置REST :: Client模块或其底层模块之一来记录SSL信息到一个日志文件,以便Wireshark可以选择它并解码生成的TLS流量,这与使用Chrome或Firefox的环境变量的方式相同。 I also don't have access to server logs. 我也无权访问服务器日志。 So I don't know what the request I'm actually sending looks like (if I did, I could probably say, "OK, it looks wrong right HERE" or "But it looks right?!"). 因此,我不知道我实际发送的请求是什么样的(如果我这样做了,我可能会说,“好,这里看起来错了”或“但是看起来正确吗?!”)。 I am therefore unfortunately at a loss as to how to debug it so blindly. 因此,不幸的是,我对如何盲目地调试它感到困惑。

A similar question about posting multipart forms using REST::Client was asked by someone else, more generically, back in April of last year, but received no responses. 去年4月,其他人也提出了类似的关于使用REST :: Client发布多部分表单的问题,但没有得到任何答复。 I'm hoping that since mine is more specific, someone can tell me what I might be doing wrong. 我希望由于我的说法更加具体,因此有人可以告诉我我可能做错了什么。

Any help would be appreciated. 任何帮助,将不胜感激。

You should be capturing your post response like so: 您应该像这样捕获您的帖子响应:

my $response = $client->POST('rest/api/content/44073843/child/attachment', $headers);   print Dumper $response;

Also, the url you are using in the POST call is incomplete and won't work, you need the full url. 另外,您在POST调用中使用的网址不完整,无法使用,您需要完整的网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM