user3624378
user3624378

Reputation: 417

SoapUI POST to REST with attached file in Groovy

I'm trying to POST to a Sharepoint REST service an attached file with SoapUI Pro. I've tried the examples at: https://support.smartbear.com/readyapi/docs/requests/attachment/rest.html But with no luck. It should work with POST with byte-array as body. But how do I do that in SoapUI and Groovy? In the tool Insomnia it works with "Binary File".

I add these headers:

Accept: application/json;odata=verbose Content-Type: application/octet-stream

Media type = multipart/mixed and Post QueryString

enter image description here

But the file won't be uploaded to SharePoint.

PowerShell code that works:

$headers = @{
    'X-RequestDigest' = 'xxxxxxxxxxxxxxxxxxxxxxx'
    'Accept' = 'application/json;odata=verbose'
}
$document = [System.IO.File]::ReadAllBytes('C:\temp\myFile.docx')
Invoke-RestMethod -Method Post -UseDefaultCredentials -Uri "https://xxxx.xxx/add(url='myFile.docx',%20overwrite=true)" -Headers $headers -Body $document  

Upvotes: 0

Views: 1667

Answers (1)

A.Joly
A.Joly

Reputation: 2387

I tried to go through this as well a while ago but I found it easier to use HTTP to do this.

You may try to see if it fits your requirements

My groovy script for attachment :

// upload source file before import
// get uploading request
def source_file = context.expand( '${#TestCase#source_file_path}' )

log.info "upload $source_file"

def aPIToolsTestSuite = context.expand( '${#Project#APIToolsTestSuite}' ) // the test suite that contains the test case with the HTTP request
tc_name = "import - upload resource files"

request = testRunner.testCase.testSuite.project.testSuites[aPIToolsTestSuite].testCases[tc_name].getTestStepByName("Request 1").testRequest

// clear request from any existing attachment
for (a in request.attachments)
{
    request.removeAttachment(a)
}

// attach file to upload
def file = new File(source_file)

if (file == null)
{
    log.error "bad file name : $source_file"
}
else
{
    // attach file and set properties
    try{
        def attachment = request.attachFile (file, true)
        attachment.contentType = "application/octet-stream"
        attachment.setPart("upload file '$source_file'")
    }
    catch (Exception e){
        log.error "file ${file.name} : exception $e"
    }
}

// now upload file - launch the request
def jsonSlurper = new groovy.json.JsonSlurper()
def TC;
def async = false


TC = testRunner.testCase.testSuite.project.getTestSuiteByName(aPIToolsTestSuite).getTestCaseByName(tc_name)
result = TC.run (context.getProperties(), async) 

if (String.valueOf( result.status ) != "PASS")
{
    msg = "unexpected failure during $tc_name when uploading $source_file"
    testRunner.fail(msg)
    log.error msg
}
else
{
    // this part is for further processing
    // file uploaded, go through the import and properties backup process
    resource_to_import = TC.getPropertyValue("testResponse").split('\"')[1]

    // file uploaded, go through the import and properties backup process
    testRunner.testCase.setPropertyValue("resource_id", resource_to_import)
}

And the HTTP request contained in the test case APIToolsTestSuite/import - upload resource files

first step : get endpoint

def env = testRunner.testCase.testSuite.project.activeEnvironment

rest = env.getRestServiceAt(0)
config = rest.getEndpoint().config
endpoint = new XmlSlurper().parseText(config.toString())

testRunner.testCase.setPropertyValue("endpoint", endpoint.toString())

second step, HTTP request:

POST

with Request tab parameters :

name : metadata
value : {"storageType":"FILESYSTEM","itemName":"my_source_file"}
type : QUERY

media type : multipart/form-data Post QueryString

Headers : application/json

Good luck :)

Upvotes: 0

Related Questions