Forum Discussion
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" } } } }
- dsathishupm7 years agoOccasional Contributor
really thanks for the sample, will try with this reference...
- suresh_aitha7 years agoNew 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,
- JHunt7 years agoCommunity 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() ]
- suresh_aitha7 years agoNew Contributor
Thanks for providing the solution but still its not working. We may need to provide pre-emptive Authentication - Authenticate pre-emptively. How to add this in above header. Please find attached screenshot from the soapui tool.
If you have Automation framework for Groovy script like getting the urls from xml, excel or properties files and send the request and verify the response and send those results to some file. Can you please share and help me on this. Thank you.
Related Content
- 2 years ago
- 4 years ago
- 5 years ago
- 7 years ago
Recent Discussions
- 15 years ago