Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

How to Send PDF file in a request field using Parasoft SOA test tool

Neil
Neil Posts: 38

Hello, I am trying to send a PDF file in one of the request field(fileContent).

tool : Parasoft SOA test 9.9
{
"fileMetadata" : "sfdsf",
"fileName" : "dsdfsd",
"fileContent" : ""
}

note that I am not just sending PDF file in the req, I also have to send some more fields along with the PDF file in the req. it;s just in one field I am sending PDF file.
please advise

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited July 2017

    Since this is JSON which is a text-based message format, I image you probably require your PDF file (which is binary) to be base64 encoded. Is this right? If so, you probably want to use the Form JSON view and script the field to read in the PDF file as bytes then base64 encode it to a string. I recommend using the java.util.Base64 class. The script engines we provide are able to access java APIs such as this.

    On a side note, if your REST Client is constrained to a RAML or Swagger that defines the message schema, and correctly defines your "fileContent" property with "binary" format, then the Form JSON would show you a file input control (no scripting required in that case).

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited July 2017

    Example Groovy script:

    import com.parasoft.api.*
    import java.nio.file.*
    import java.util.*
    
    String base64Decode(ScriptingContext context) {
        File file = context.getAbsolutePathFile("myfile.pdf")
        byte[] fileContent = Files.readAllBytes(file.toPath())
        return Base64.getEncoder().encodeToString(fileContent)
    }