Forum Discussion

dsathishupm's avatar
dsathishupm
Occasional Contributor
7 years ago

Groovy Script to test Rest Post Method with Json file and xml file with headers

please provide some samples for the below task,

 

Groovy Script to test Rest Post Method with Json file and xml file with headers

 

i have to create a groovy step, for Rest.

  • Lucian's avatar
    Lucian
    Community Hero

    Could you provide a little bit more info on what you need?

  • dsathishupm's avatar
    dsathishupm
    Occasional Contributor

     

     

    i want to test the rest services using soap Ui  with groovy scripting.

     

    i have the code for get method , like below sample 

    import com.eviware.soapui.model.iface.Submit

    def uri = "http://jsonplaceholder.typicode.com/posts/1"

    WsdlProject project = new WsdlProject()
    RestServiceBuilder serviceBuilder = new RestServiceBuilder()
    serviceBuilder.createRestService(project, uri)

    println "service name: "+project.getInterfaceList()[0].name

    println "Resource: "+project.getInterfaceList()[0].getOperationList()[0]
    RestRequest request = project.getInterfaceList()[0].getOperationList()[0].getRequestList()[0]
    Submit submit = (Submit) request.submit( new WsdlSubmitContext(), false );

    Response response = submit.getResponse();

    String responseContent = response.getContentAsString();

    println( responseContent );

     

     

     

    but i dont know how to start with Post Method with Json or xml attachement....

    • Lucian's avatar
      Lucian
      Community Hero

      Still don't get it. Is the problem that you don't know how to attach a file to a request? Or is it that you don't know how to send a JSON/xml body?

      • dsathishupm's avatar
        dsathishupm
        Occasional Contributor

        please tell me 

        how to attach a file to a request and how to send a JSON/xml body.

         

        how to add headers (authorisation key, content type) on request body...

         

         

         

  • dsathishupm's avatar
    dsathishupm
    Occasional Contributor
    Actually, using the above sample I am able to call the get method,

    Buy I don't know to to set a method as post, and how to attach the JSON file for post request, and hot to set headers....

    Earlier I used rest assured API for rest web services, but now I tried the same for soap ui with Groovy scripting not an soap ui interface...
  • JHunt's avatar
    JHunt
    Community Hero
    import com.eviware.soapui.impl.actions.RestServiceBuilder
    import com.eviware.soapui.impl.wsdl.WsdlProject
    import com.eviware.soapui.impl.wsdl.WsdlSubmitContext
    import com.eviware.soapui.impl.rest.support.RestURIParserImpl
    
    f = new File('myexamplefile.txt')
    f.text = "This is an example file"
    encodedFile = f.bytes.encodeBase64()
    f.delete()
    
    service = new RestURIParserImpl("http://jsonplaceholder.typicode.com/posts")
    project = new WsdlProject()
    
    new RestServiceBuilder()
     .createRestService(project, service.endpoint + service.resourcePath)
    
    project
     .getInterfaceByName(service.endpoint)
     .getOperationByName(service.resourceName)
     .addNewMethod("Add New Post")
     .with {
     
        setMethod (com.eviware.soapui.impl.rest.RestRequestInterface.HttpMethod.POST)
     
        addNewRequest("Request 1")
        .with {
    
            requestHeaders << ["Content-Type":"application/json"]        
            requestHeaders << ["Accept":"application/json"]
    
            requestContent = """
                {
                    "content":"Hi, this is my new post.",
                    "attachment":"$encodedFile"
                }
            """
    
            
            submit(new WsdlSubmitContext(), false).response
            .with {
            
                assert statusCode == 201 // Created
                assert statusCode == javax.servlet.http.HttpServletResponse.SC_CREATED
    
                new groovy.json.JsonSlurper().parseText(contentAsString)
                 .with { 
    
                     assert id in Integer
                     assert id == 101
                     
                     assert content == "Hi, this is my new post."
                     
                     assert attachment == 'VGhpcyBpcyBhbiBleGFtcGxlIGZpbGU='
                     assert new String(attachment.decodeBase64()) == "This is an example file"
                     
                 }
                 
             }
             
        }
        
    }
    • dsathishupm's avatar
      dsathishupm
      Occasional Contributor

      really thanks for the sample, will try with this reference...

    • suresh_aitha's avatar
      suresh_aitha
      New Contributor

      Hi,

       

      I used  below mention code but i need to add authorization (username, password, basic type). Can you please provide solution how to add authorization for below code.

       

      Thanks,

      • JHunt's avatar
        JHunt
        Community Hero

        Basic Authentication just means adding an extra header called Authorization. The value is the username and password encoded to Base64.

         

        requestHeaders << ["Authorization": "myusername:mypassword".getBytes().encodeBase64() ]