Forum Discussion
- LucianCommunity Hero
Could you provide a little bit more info on what you need?
- dsathishupmOccasional 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....
- LucianCommunity 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?
- dsathishupmOccasional 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...
- dsathishupmOccasional ContributorActually, 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... - JHuntCommunity 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" } } } }
- dsathishupmOccasional Contributor
really thanks for the sample, will try with this reference...
- suresh_aithaNew 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,
- JHuntCommunity 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() ]
Related Content
- 2 years ago
- 4 years ago
Recent Discussions
- 15 years ago