Create and Upload a New Document (PDF) - (415) Unsupported Media Type

I'm using a Powershell script to interact with the Document Management API. The Create is working fine, but when I try to upload the pdf to the newly created documentId, I get a (415) Unsupported Media Type error. I've tried several iterations of the code below. If there is a better way to do this then the create, get documentId, then upload, I'm open to that too. This code is after the create has successfully processed.  What am I doing wrong? Thanks in advance for any help/advice.

# Construct the request body
$requestBody = @{
name = "TestFile"
parents = @(@{ id = $parentFolderId })
mimeType = "application/pdf"
description = "scan"
}

$uploadHeaders = @{}
$uploadHeaders.add("Content-Type","application/json")
$uploadHeaders.add("X-TR-API-APP-ID",$TR_api_key)
$uploadHeaders.Add("Authorization", "Basic " + $login.token)

try
{
$uploadUrl = "">api.thomsonreuters.com/.../" + $create.documentCreate.documentId + "/file"
$upload = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $uploadHeaders -Body ($requestBody | ConvertTo-Json)
}

  • Hi  ,

    First off I'll state I've never used this particular TR REST API (GoFileRoom DM API is it??) but generally when uploading content to a REST API, it tends to be as a multipart form data POST, but that there tends to be more details needed.

    Here is an example where I'm creating a multipart form, where the file data is in the form (name) "file" with a filename and the content-type specified.  Other API's may also require other form fields supplied, e.g. title, version notes, which are also supplied in other form parts.  What I don't see in your example above is the boundaries in the (body) payload of the request:

    So here is my example:

    $authToken = "**EXAMPLE**TOKEN**"
    $appID = "**EXAMPLE**APPID**"
    
    $uploadHeaders = @{}
    $uploadHeaders.add("X-TR-API-APP-ID", $appID)
    $uploadHeaders.Add("Authorization", "Basic " + $authToken)
    
    $filePath = "C:\Temp\"
    $fileName = "TestFile.txt"
    $mimeType = "text/plain"
    
    $theFile = [System.IO.File]::ReadAllBytes($filePath + $fileName)
    $theFileContent = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($theFile)
    
    $uploadUrl = "http://localhost:8000"
    $boundary = [System.Guid]::NewGuid().ToString()
    $LF = "`r`n"
    
    $payload = (
        "--$boundary",
        "Content-Disposition: form-data; name=`"file`"; filename=`"$fileName`"",
        "Content-Type: $mimeType",
        "",
        $theFileContent,
        "--$boundary--"
    ) -join $LF
    
    $upload = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers $uploadHeaders -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $payload

    Which will result in a request similar to this:

    The key thing is that, from the server-side, it needs to generally know which form field (of the multipart) the file data is in, which they would generally describe in the API documentation.  The only documentation I can see is that in the (new) developer portal where there is no mention about the structure the payload should be in...


    I don't know if this is of any help in your scenario, but I saw you using multipart and there being no boundary structure defined in the payload.

    Hope this helps,
    Andrew