powershell invoke-restmethod multipart/form-data [英] powershell invoke-restmethod multipart/form-data

查看:45
本文介绍了powershell invoke-restmethod multipart/form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用 REST API 将文件上传到网络服务器.如前所述,我为此使用了 PowerShell.使用 curl 这没问题.调用如下所示:

I'm currently trying to upload a file to a Webserver by using a REST API. And as mentioned I'm using PowerShell for this. With curl this is no problem. The call looks like this:

curl -H "Auth_token:"$AUTH_TOKEN -H "Content-Type:multipart/form-data" -X POST -F appInfo='{"name": "test","description": "test"}' -F uploadFile=@/test/test.test https://server/api/

但是在使用 Invoke-Restmethod 命令将其导出到 powershell 时,我完全无能为力.据我搜索,不可能为此使用 Invoke-Restmethod.https://www.snip2code.com/Snippet/396726/PowerShell-V3-Multipart-formdata-example但即使使用了 Snipped,我也不够聪明,无法让它工作,因为我不想上传两个文件,而是一个文件和一些参数.

But I'm completely helpless when it comes to exporting this to powershell with a Invoke-Restmethod command. As far as I searched it is not possible to use the Invoke-Restmethod for this. https://www.snip2code.com/Snippet/396726/PowerShell-V3-Multipart-formdata-example But even with that Snipped I'm not smart enough to get this working since I don´t want to upload two files but instead one file and some arguments.

如果有人能让我回到正轨,我将非常感激:o谢谢!

I would be very thankful if someone could get me back on the track with this :o Thanks!

推荐答案

应该很简单.取自这个答案:

$Uri = 'https://server/api/';
$Headers = @{'Auth_token'=$AUTH_TOKEN};
$FileContent = [IO.File]::ReadAllText('C:	est	est.test');
$Fields = @{'appInfo'='{"name": "test","description": "test"}';'uploadFile'=$FileContent};

Invoke-RestMethod -Uri $Uri -ContentType 'multipart/form-data' -Method Post -Headers $Headers -Body $Fields;

如果文件不是文本文件,您可能需要使用 [IO.File]::ReadAllBytes().

You may want to use [IO.File]::ReadAllBytes() if the file isn't a text file.

如果您上传的文件很大,这也可能无法正常工作.

This also may not work well if you're uploading a huge file.

这篇关于powershell invoke-restmethod multipart/form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆