SoapUI POST到带有Groovy中附加文件的REST

问题描述 投票:0回答:1

我正在尝试使用SoapUI Pro将附加文件发布到Sharepoint REST服务。我已经在https://support.smartbear.com/readyapi/docs/requests/attachment/rest.html尝试了这些示例但是没有运气。它应该与以字节数组为主体的POST一起使用。但是如何在SoapUI和Groovy中做到这一点?在工具Insomnia中,它可以与“二进制文件”一起使用。

我添加了这些标题:

接受:application / json; odata = verbose内容类型:应用程序/八位字节流

媒体类型=多部分/混合后的QueryString

enter image description here

但是文件不会上载到SharePoint。

有效的PowerShell代码:

$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  
rest groovy soapui
1个回答
0
投票

我也曾尝试过此操作,但是我发现使用HTTP进行此操作更容易。

您可以尝试查看它是否符合您的要求

我的附件常规脚本:

// 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)
}

以及测试用例APIToolsTestSuite / import中包含的HTTP请求-上传资源文件

第一步:获取端点

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())

第二步,HTTP请求:

POST

带有“请求”选项卡参数:

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

媒体类型:多部分/表单数据发表QueryString

标题:application / json

祝你好运:)

© www.soinside.com 2019 - 2024. All rights reserved.