Reputation: 2076
This is a repetition from my two previous questions (Question:1 & Question:2), creating a separate thread as its objective is different.
Thanks @Dmitri, for your quick answers.
After fixing all the code, I am getting Precondition Failed, mostly am expecting because of the content-type
[this is just based on assumption after carefully seeing the error].
Error Detail
HttpResponseProxy{HTTP/1.1 412 Precondition Failed [Date: Thu, 01 Sep 2022 13:39:18 GMT, Content-Type: application/json, Transfer-Encoding: chunked, Connection: keep-alive, Server: openresty, Access-Control-Allow-Origin: *, Access-Control-Expose-Headers: Date, Access-Control-Allow-Headers: X-Acc-Cached,X-File-Name,X-File-Size,X-Requested-With,X-DicomGrid-Client,X-AmbraHealth-part-checksum,X-DicomGrid-Version,X-AmbraHealth-SID,Cache-Control,Content-Encoding,Content-Type,If-None-Match, Access-Control-Request-Headers: X-DicomGrid-Client,X-DicomGrid-Version, Access-Control-Allow-Methods: DELETE,GET,OPTIONS,POST,PATCH,PUT, Vary: Accept-Encoding, Cache-Control: max-age=0, no-cache, Expires: Thu, 01 Sep 2022 13:39:23 GMT] ResponseEntityProxy{[Content-Type: application/json,Chunked: true]}}
Code Base
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 = "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 response = HttpClientBuilder.create().build().execute(postRequest);
Do I need to add a header with content-type:application/dicom
? Which I already mentioned in the header manager.
Upvotes: 0
Views: 315
Reputation: 168197
You're very good at copying and pasting the code but for some reason you completely ignore the reference material I'm sharing.
JSR223 Sampler doesn't know anything about the headers you define in the HTTP Header Manager, you need to add the headers in the Groovy code like it's described in the How to Send Groovy HTTP and HTTPS Requests
Something like:
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);
I also might be suffering from a form of a mental fatigue but I'm pretty sure I told you it's not the best idea to inline JMeter Functions and Variables into Groovy scripts otherwise only first occurrence will be compiled and cached.
More information: 412 Precondition Failed status code explanation.
Upvotes: 1