简体   繁体   中英

Powershell: Uploading file to Docparser API using Invoke-RestMethod

$header = @{'Authorization'='Basic <auth code value>'}
$ping = Invoke-RestMethod -Uri "https://api.docparser.com/v1/ping" -Headers $header

ping works fine...returns "pong". I then make a request for the Parser ID which is needed for uploading documents. I am able to retrieve this value successfully.

$parser = Invoke-RestMethod -Uri "https://api.docparser.com/v1/parsers" -Headers $header
$parserID = $parser.id

Now here is where I try to upload a pdf, which fails.

$fileToParse = "C:\test.pdf"
$body = @{'file'=$fileToParse}
$uploadDoc = Invoke-RestMethod -Uri "https://api.docparser.com/v1/document/upload/$parserID" -Method Post -Headers $header -ContentType 'multipart/form-data' -Body $body

API response keeps saying "Error: input empty"

This is the documentation from Docparser on how to upload pdfs: 在此处输入图片说明

Any thoughts on what I'm doing wrong here? Thanks in advance,

Eric

The problem is that your body currently just contains the path to your local file. Docparser expects however the content of the file as multipart/form-data.

I never worked with PowerShell, but I think something like this should work for you:

$body = @{
    "file" = Get-Content($fileToParse) -Raw
}

I got the code from this answer: How to send multipart/form-data with PowerShell Invoke-RestMethod

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.

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