How to disable the URL encoding in the JSR 223 sampler for HTTP Request

Viewed 11

I am trying to automate the upload operation for my application using JSR223 Sampler with the following groovy script. But the ? in that URI path is getting encoded and I am getting http-404 error.

Code Block:

import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.message.BasicHeader;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.ssl.SSLContextBuilder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

def storageProtocol = "https";
def storageServer = "localhost";
def namespace = "7c762733-009f-4527-81ea-571d1cf6e9d2"
def studyUid = "1.2.300.0.7230010.3.1.2.2595064201.8112.1216202112026121";
def imageUid = "1.2.840.113704.7.1.0.1356918323635126.1521373008.110"

def RequestConfig uploadRequestConfig = RequestConfig.custom()
            .setConnectTimeout(30000)
            .setSocketTimeout(30000)
            .build();
def uploadUriBuilder = new URIBuilder().setScheme(storageProtocol)
            .setHost(storageServer);

def URI uri = uploadUriBuilder.setPath("/api/v3/storage/namespace/" + namespace + "/image?sid="+"${SIDVALUE}")
            .setParameter("study_uid", studyUid)
            .setParameter("image_uid", imageUid)
            .build();

def  RequestBuilder requestBuilder = RequestBuilder.create("POST")
        .setConfig(uploadRequestConfig)
        .setUri(uri)
        .setHeader("X-AmbraHealth-SID", "${SIDVALUE}")
        .setHeader("X-Requested-With", "XMLHttpRequest")
        .setHeader("X-DicomGrid-Client", "USER")
        .setHeader("X-DicomGrid-Version", "3.22.3.0")
        .setHeader("X-File-Name", "IMG00001.dcm")
        .setHeader("Referer", "https://${BASE_URL_1}/load.html?namespace_id=7c762733-009f-4527-81ea-571d1cf6e9d2")
        .setHeader(HttpHeaders.CONTENT_TYPE, "application/dicom");

def InputStreamEntity entity = new InputStreamEntity(new FileInputStream("C://Users//IMAGES//IMG00001.dcm"));
requestBuilder.setEntity(entity);

def sslBuilder = new SSLContextBuilder();
sslBuilder.loadTrustMaterial(null, new TrustStrategy(){
    @Override
    public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        return true;
        }
    });


def trustAllFactory = new SSLConnectionSocketFactory(sslBuilder.build(), new NoopHostnameVerifier());

HttpClients.custom().setSSLSocketFactory(trustAllFactory).build().withCloseable { httpClient ->
        httpClient.execute(requestBuilder.build()).withCloseable { response -> {
                def resp = EntityUtils.toString(response.getEntity());
            }
        }
    }

Request URL generating from the script:

https://localhost/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image%3Fsid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14?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

There are two errors, /image%3Fsid should be /image?sid & *-6e4353163f14?study_uid= should be *-6e4353163f14&study_uid=

Working URL :

https://localhost/api/v3/storage/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image?sid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14&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

Error Details:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404 Not Found</h2>
<table>
<tr><th>URI:</th><td>/api/v2/namespace/7c762733-009f-4527-81ea-571d1cf6e9d2/image%3Fsid=fdbfbf07-bf4b-4ec5-b3cb-6e4353163f14</td></tr>
<tr><th>STATUS:</th><td>404</td></tr>
<tr><th>MESSAGE:</th><td>Not Found</td></tr>
<tr><th>SERVLET:</th><td>webservice</td></tr>
</table>
<hr><a href="https://eclipse.org/jetty">Powered by Jetty:// 9.4.43.v20210629</a><hr/>

</body>
</html>

How to handle this encoding?

1 Answers

It's not JSR223 Sampler who is "encoding" the URL Path, it's you who does this.

And I'm just wondering why you're using URIBuilder.setParameter() function for study_uid and image_uid and not doing this for the sid because once you start doing this the issue will go away.

Also don't inline JMeter Functions or Variables into Groovy scripts, i.e. change your ${SIDVALUE} to vars.get('SIDVALUE') otherwise only first occurrence will be cached and subsequent iterations will be using the same SIDVALUE and I don't think this is something you're looking for.

More information:

Related