2

In my API request, with the request payload the complete image is also getting loaded. Is there any way I can script it through JMeter using Groovy?

UI Call where I am uploading image: enter image description here

Main Request POST Call: enter image description here

Image in the request payload: enter image description here

Can it be done through groovy on JMeter.

Dev030126
  • 315
  • 1
  • 8

1 Answers1

3

Out of interest, why do you want to do this in Groovy? I believe using normal HTTP Request sampler would be much easier, moreover you will be able to even record the request(s) using HTTP(S) Test Script Recorder, this way you will get more metrics and won't need to handle the request and response.

If you still want to do this in Groovy I believe the best option would be calling relevant Apache HttpComponents functions, example code:

import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ContentType
import org.apache.http.entity.mime.HttpMultipartMode
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.impl.client.HttpClientBuilder

def imageFileName = "/path/to/your/image.png"

def post = new HttpPost("http://your-server");
def file = new File(imageFileName);
def builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, imageFileName);
def entity = builder.build();
post.setEntity(entity);
def response = HttpClientBuilder.create().build().execute(post)

//do what you need with the response here

More information: How to Send Groovy HTTP and HTTPS Requests

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks , i tried with normal http request it is not working. Thought may be we can handle it with groovy script – Dev030126 Aug 30 '22 at 14:58