我试图为此Python代码创建PowerShell等效项:
import requests
requests.post('http://someCKANsite/api/action/resource_create',
data={"package_id":"my_dataset"},
headers={"X-CKAN-API-Key": "21a47217-6d7b-49c5-88f9-72ebd5a4d4bb"},
files=[('upload', file('/path/to/file/to/upload.csv'))])
我试过了:
Invoke-WebRequest -Method Post -Uri http://someCKANsite/api/action/resource_create -Headers $headers -InFile $myfile -Body $rcbody -ContentType "multipart/form-data"
... $headers
包含我的X-CKAN-API-Key
和$rcbody
,其中包含package_id
。但我收到错误Invoke-WebRequest : The cmdlet cannot run because the following conflicting parameters are specified: Body and InFile. Specify either Body or Infile, then retry.
我已经尝试将?package_id=...
放在Uri的末尾,但这并不起作用。我在Advanced REST client
尝试了各种组合,但也无济于事。我也试过Invoke-RestMethod
,但也有同样的麻烦。
答案 0 :(得分:1)
你不能同时指明两者;因为-InFile基本上只是将文件的内容作为正文(可能会与现有正文冲突)添加。
但是,您可以使用您的文件自己构建这样的主体(对于invoke-webrequest /或invoke-restmethod也是如此):
$body = "upload=$(get-content c:\yourfile.csv -Enc Byte -raw)&package_id=my_dataset"
Invoke-RestMethod -Method $method -Headers $headers -Uri ($server+$uri) -body $body
在这里,我只是通过连接它们(& package_id =)在你的身体中添加了更多的参数。您应该知道服务器如何处理您的参数。
他们是从身体中读取的(我在这里假设)还是作为网址字符串的一部分?您可以在调用python时轻松查看请求的传递方式(使用例如fiddler),然后调整invoke-webrequst / invoke-restmethod。
希望这会有所帮助:)