I'm trying to make a PowerShell equivalent for this Python code:
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'))])
I have tried:
Invoke-WebRequest -Method Post -Uri http://someCKANsite/api/action/resource_create -Headers $headers -InFile $myfile -Body $rcbody -ContentType "multipart/form-data"
...with $headers
containing my X-CKAN-API-Key
and $rcbody
containing the package_id
. But I get the error Invoke-WebRequest : The cmdlet cannot run because the following conflicting parameters are specified: Body and InFile. Specify either Body or Infile, then retry.
Invoke-WebRequest : The cmdlet cannot run because the following conflicting parameters are specified: Body and InFile. Specify either Body or Infile, then retry.
I've tried putting ?package_id=...
on the end of the Uri, but that doesn't work. I've tried all kinds of combinations in the Advanced REST client
, but also to no avail. I've also tried Invoke-RestMethod
, but it has the same hassles.
You can't specify both; as the -InFile basically just adds the content of the file as a body (which would conflict) with your existing body.
You can however, construct the body with your file yourself doing something like this (same for invoke-webrequest/or 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
Here I just added more parameters in your body by concatting them (&package_id=). You should know how the server processes your parameters.
Are they read from the body (as I'm assuming here) or as a part of the url string? You can easily see how the request is being passed (using eg fiddler) when you invoke the python and then adjust the invoke-webrequst/invoke-restmethod thereafter.
Hope this helps:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.