`MultipartEntityBuilder` doesn't supported by application

Viewed 18

Am trying to upload an image file in the request body with the following script through a JSR223 Sampler. The below script is failing due to my application doesn't support multipart entinty. Is there any way I solve this?

import org.apache.http.HttpHeaders
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.HttpClients

def imageFileName = "C:\\Users\\IMAGES\\IMG00001.dcm"

def urlRequest = 'https://' + ${BASE_URL_1} + '/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=' + vars.get('SIDVALUE') + '&study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110'

def postRequest = new HttpPost(urlRequest);
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();
postRequest.setEntity(entity);

def header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/dicom");
def headers = Arrays.asList(header)
def client = HttpClients.custom().setDefaultHeaders(headers).build()

def response = client.execute(postRequest);
1 Answers

What are you trying to "solve" here?

If your application doesn't support multipart file uploads then you need to pass the file somehow else and we cannot suggest you how exactly because we know nothing about your application specifics.

If you can perform the file upload using browser or other application you can record it using JMeter's HTTP(S) Test Script Recorder or JMeter Chrome Extension

If you cannot and/or want to just copy and paste the code from StackOverflow without understanding what it's doing you can try using a different HttpEntity implementation, for example FileEntity seems to be a good candidate

Example code you can copy and paste is in Fundamentals chapter of Apache HttpComponents documentation:

File file = new File("C:\\Users\\IMAGES\\IMG00001.dcm");
FileEntity entity = new FileEntity(file, 
    ContentType.DEFAULT_BINARY);        

HttpPost httppost = new HttpPost("https://' + ${BASE_URL_1} + '/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=' + vars.get('SIDVALUE') + '&study_uid=1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121&image_uid=1.2.840.113704.7.1.0.1356918323635126.1521373008.110");
httppost.setEntity(entity);
Related