How to use multiPart method of com.intuit.karate.http.HttpRequestBuilder class

Best Karate code snippet using com.intuit.karate.http.HttpRequestBuilder.multiPart

Source:HttpRequestBuilder.java Github

copy

Full Screen

...87 private String method;88 private List<String> paths;89 private Map<String, List<String>> params;90 private Map<String, List<String>> headers;91 private MultiPartBuilder multiPart;92 private Object body;93 private Set<Cookie> cookies;94 private String retryUntil;95 public final HttpClient client;96 public HttpRequestBuilder(HttpClient client) {97 this.client = client;98 }99 public HttpRequestBuilder reset() {100 // url will be retained101 method = null;102 paths = null;103 params = null;104 headers = null;105 multiPart = null;106 body = null;107 cookies = null;108 retryUntil = null;109 return this;110 }111 public HttpRequestBuilder copy() {112 HttpRequestBuilder hrb = new HttpRequestBuilder(client);113 hrb.url = url;114 hrb.method = method;115 hrb.paths = paths;116 hrb.params = params;117 hrb.headers = headers;118 hrb.multiPart = multiPart;119 hrb.body = body;120 hrb.cookies = cookies;121 hrb.retryUntil = retryUntil;122 return hrb;123 }124 public Response invoke(String method) {125 this.method = method;126 return invoke();127 }128 public Response invoke(String method, Object body) {129 this.method = method;130 this.body = body;131 return invoke();132 }133 public HttpRequest build() {134 HttpRequest request = new HttpRequest();135 if (method == null) {136 if (multiPart != null && multiPart.isMultipart()) {137 method = "POST";138 } else {139 method = "GET";140 }141 }142 method = method.toUpperCase();143 request.setMethod(method);144 if ("GET".equals(method) && multiPart != null) {145 Map<String, Object> parts = multiPart.getFormFields();146 if (parts != null) {147 parts.forEach((k, v) -> param(k, (String) v));148 }149 multiPart = null;150 }151 request.setUrl(getUri());152 if (multiPart != null) {153 if (body == null) { // this is not-null only for a re-try, don't rebuild multi-part154 body = multiPart.build();155 String userContentType = getHeader(HttpConstants.HDR_CONTENT_TYPE);156 if (userContentType != null) {157 String boundary = multiPart.getBoundary();158 if (boundary != null) {159 contentType(userContentType + "; boundary=" + boundary);160 }161 } else {162 contentType(multiPart.getContentTypeHeader());163 }164 }165 request.setBodyForDisplay(multiPart.getBodyForDisplay());166 }167 if (cookies != null && !cookies.isEmpty()) {168 List<String> cookieValues = new ArrayList<>(cookies.size());169 for (Cookie c : cookies) {170 String cookieValue = ClientCookieEncoder.LAX.encode(c);171 cookieValues.add(cookieValue);172 }173 header(HttpConstants.HDR_COOKIE, StringUtils.join(cookieValues, "; "));174 }175 if (body != null) {176 request.setBody(JsValue.toBytes(body));177 if (multiPart == null) {178 String contentType = getContentType();179 if (contentType == null) {180 ResourceType rt = ResourceType.fromObject(body);181 if (rt != null) {182 contentType = rt.contentType;183 }184 }185 Charset charset = contentType == null ? null : HttpUtils.parseContentTypeCharset(contentType);186 if (charset == null) {187 // client can be null when not in karate scenario188 charset = client == null ? null : client.getConfig().getCharset();189 if (charset != null) {190 // edge case, support setting content type to an empty string191 contentType = StringUtils.trimToNull(contentType);192 if (contentType != null) {193 contentType = contentType + "; charset=" + charset;194 }195 }196 }197 contentType(contentType);198 }199 }200 request.setHeaders(headers);201 return request;202 }203 public Response invoke() {204 return client.invoke(build());205 }206 public boolean isRetry() {207 return retryUntil != null;208 }209 public String getRetryUntil() {210 return retryUntil;211 }212 public void setRetryUntil(String retryUntil) {213 this.retryUntil = retryUntil;214 }215 public HttpRequestBuilder url(String value) {216 url = value;217 return this;218 }219 public HttpRequestBuilder method(String method) {220 this.method = method;221 return this;222 }223 public HttpRequestBuilder paths(String... paths) {224 for (String path : paths) {225 path(path);226 }227 return this;228 }229 public HttpRequestBuilder path(String path) {230 if (path == null) {231 return this;232 }233 if (paths == null) {234 paths = new ArrayList<>();235 }236 paths.add(path);237 return this;238 }239 public String getUri() {240 try {241 URIBuilder builder;242 if (url == null) {243 builder = new URIBuilder();244 } else {245 builder = new URIBuilder(url);246 }247 if (params != null) {248 params.forEach((key, values) -> values.forEach(value -> builder.addParameter(key, value)));249 }250 if (paths != null) {251 List<String> segments = new ArrayList();252 for (String item : builder.getPathSegments()) {253 if (!item.isEmpty()) {254 segments.add(item);255 }256 } 257 for (String item : paths) {258 for (String s : StringUtils.split(item, '/', true)) {259 segments.add(s);260 }261 }262 builder.setPathSegments(segments);263 }264 URI uri = builder.build();265 return uri.toASCIIString();266 } catch (URISyntaxException e) {267 throw new RuntimeException(e);268 }269 }270 public HttpRequestBuilder body(Object body) {271 this.body = body;272 return this;273 }274 public HttpRequestBuilder bodyJson(String json) {275 this.body = Json.of(json).value();276 return this;277 }278 public List<String> getHeaderValues(String name) {279 return StringUtils.getIgnoreKeyCase(headers, name); // TODO optimize280 }281 public String getHeader(String name) {282 List<String> list = getHeaderValues(name);283 if (list == null || list.isEmpty()) {284 return null;285 } else {286 return list.get(0);287 }288 }289 public String getContentType() {290 return getHeader(HttpConstants.HDR_CONTENT_TYPE);291 }292 public HttpRequestBuilder removeHeader(String name) {293 if (headers != null) {294 StringUtils.removeIgnoreKeyCase(headers, name);295 }296 return this;297 }298 public HttpRequestBuilder header(String name, String... values) {299 return header(name, Arrays.asList(values));300 }301 public HttpRequestBuilder header(String name, List<String> values) {302 if (headers == null) {303 headers = new LinkedHashMap<>();304 }305 for (String key : headers.keySet()) {306 if (key.equalsIgnoreCase(name)) {307 name = key;308 break;309 }310 }311 headers.put(name, values);312 return this;313 }314 public HttpRequestBuilder header(String name, String value) {315 return header(name, Collections.singletonList(value));316 }317 public HttpRequestBuilder headers(Map<String, Object> map) {318 map.forEach((k, v) -> {319 if (!k.startsWith(":")) { // strip (armeria) special headers320 if (v instanceof List) {321 header(k, (List) v);322 } else if (v != null) {323 header(k, v.toString());324 }325 }326 });327 return this;328 }329 public HttpRequestBuilder headers(Value value) {330 JsValue jv = new JsValue(value);331 if (jv.isObject()) {332 headers(jv.getAsMap());333 } else {334 logger.warn("unexpected headers() argument: {}", value);335 }336 return this;337 }338 public HttpRequestBuilder contentType(String contentType) {339 if (contentType != null) {340 header(HttpConstants.HDR_CONTENT_TYPE, contentType);341 }342 return this;343 }344 public List<String> getParam(String name) {345 if (params == null || name == null) {346 return null;347 }348 return params.get(name);349 }350 public HttpRequestBuilder param(String name, String... values) {351 return param(name, Arrays.asList(values));352 }353 public HttpRequestBuilder param(String name, List<String> values) {354 if (params == null) {355 params = new HashMap<>();356 }357 List<String> notNullValues = values.stream().filter(v -> v != null).collect(Collectors.toList());358 if (!notNullValues.isEmpty()) {359 params.put(name, notNullValues);360 }361 return this;362 }363 public HttpRequestBuilder params(Map<String, List<String>> params) {364 this.params = params;365 return this;366 }367 public HttpRequestBuilder cookies(Collection<Map> cookies) {368 for (Map<String, Object> map : cookies) {369 cookie(map);370 }371 return this;372 }373 public HttpRequestBuilder cookie(Map<String, Object> map) {374 return cookie(Cookies.fromMap(map));375 }376 public HttpRequestBuilder cookie(Cookie cookie) {377 if (cookies == null) {378 cookies = new HashSet<>();379 }380 cookies.add(cookie);381 return this;382 }383 public HttpRequestBuilder cookie(String name, String value) {384 return cookie(new DefaultCookie(name, value));385 }386 public HttpRequestBuilder formField(String name, Object value) {387 if (multiPart == null) {388 multiPart = new MultiPartBuilder(false, client);389 }390 multiPart.part(name, value);391 return this;392 }393 public HttpRequestBuilder multiPartJson(String json) {394 return multiPart(Json.of(json).value());395 }396 public HttpRequestBuilder multiPart(Map<String, Object> map) {397 if (multiPart == null) {398 multiPart = new MultiPartBuilder(true, client);399 }400 multiPart.part(map);401 return this;402 }403 //==========================================================================404 //405 private final Methods.FunVar PATH_FUNCTION = args -> {406 if (args.length == 0) {407 return getUri();408 } else {409 for (Object o : args) {410 if (o != null) {411 path(o.toString());412 }413 }414 return this;...

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1def multiPart = {2 def builder = new com.intuit.karate.http.HttpRequestBuilder()3 builder.multiPart('file', file)4 builder.multiPart('file2', file2)5 builder.multiPart('file3', file3)6 builder.multiPart('file4', file4)7 builder.multiPart('file5', file5)8 builder.multiPart('file6', file6)9 builder.multiPart('file7', file7)10 builder.multiPart('file8', file8)11 builder.multiPart('file9', file9)12 builder.multiPart('file10', file10)13 builder.multiPart('file11', file11)14 builder.multiPart('file12', file12)15 builder.multiPart('file13', file13)16 builder.multiPart('file14', file14)17 builder.multiPart('file15', file15)18 builder.multiPart('file16', file16)19 builder.multiPart('file17', file17)20 builder.multiPart('file18', file18)21 builder.multiPart('file19', file19)22 builder.multiPart('file20', file20)23 builder.multiPart('file21', file21)24 builder.multiPart('file22', file22)25 builder.multiPart('file23', file23)26 builder.multiPart('file24', file24)27 builder.multiPart('file25', file25)28 builder.multiPart('file26', file26)29 builder.multiPart('file27', file27)30 builder.multiPart('file28', file28)31 builder.multiPart('file29', file29)32 builder.multiPart('file30', file30)33 builder.multiPart('file31', file31)34 builder.multiPart('file32', file32)35 builder.multiPart('file33', file33)36 builder.multiPart('file34', file34)37 builder.multiPart('file35', file35)38 builder.multiPart('file36', file36)39 builder.multiPart('file37', file37)40 builder.multiPart('file38', file38)41 builder.multiPart('file39', file39)42 builder.multiPart('file40', file40)43 builder.multiPart('file41', file41)44 builder.multiPart('file42', file42)45 builder.multiPart('file43', file43)46 builder.multiPart('file

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1 * def sample = read('classpath:sample.txt')2 * def sample2 = read('classpath:sample2.txt')3 * def sample3 = read('classpath:sample3.txt')4 * def sample4 = read('classpath:sample4.txt')5 * def sample5 = read('classpath:sample5.txt')6 * def sample6 = read('classpath:sample6.txt')7 * def sample7 = read('classpath:sample7.txt')8 * def sample8 = read('classpath:sample8.txt')9 * def sample9 = read('classpath:sample9.txt')10 * def sample10 = read('classpath:sample10.txt')11 * def sample11 = read('classpath:sample11.txt')12 * def sample12 = read('classpath:sample12.txt')13 * def sample13 = read('classpath:sample13.txt')14 * def sample14 = read('classpath:sample14.txt')15 * def sample15 = read('classpath:sample15.txt')16 * def sample16 = read('classpath:sample16.txt')17 * def sample17 = read('classpath:sample17.txt')18 * def sample18 = read('classpath:sample18.txt')19 * def sample19 = read('classpath:sample19.txt')20 * def sample20 = read('classpath:sample20.txt')21 * def sample21 = read('classpath:sample21.txt')22 * def sample22 = read('classpath:sample22.txt')23 * def sample23 = read('classpath:sample23.txt')24 * def sample24 = read('classpath:sample24.txt')25 * def sample25 = read('classpath:sample25.txt')26 * def sample26 = read('classpath:sample26.txt')27 * def sample27 = read('classpath:sample27.txt')28 * def sample28 = read('classpath:sample28.txt')29 * def sample29 = read('classpath:sample29.txt')30 * def sample30 = read('classpath:sample30.txt')31 * def sample31 = read('classpath:sample31.txt')32 * def sample32 = read('classpath:sample32.txt')33 * def sample33 = read('classpath:sample33.txt')34 * def sample34 = read('classpath:sample34.txt')35 * def sample35 = read('classpath:sample35.txt')

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1* def request = karate.call('classpath:com/intuit/karate/http/http-request-builder.feature')2* def multiPart1 = multiPart.addFile('file1', 'file1.txt', 'text/plain')3* def multiPart2 = multiPart.addFile('file2', 'file2.txt', 'text/plain')4* def multiPart3 = multiPart.addFile('file3', 'file3.txt', 'text/plain')5* def multiPart4 = multiPart.addFile('file4', 'file4.txt', 'text/plain')6* def multiPart5 = multiPart.addFile('file5', 'file5.txt', 'text/plain')7* def response = karate.call('classpath:com/intuit/karate/http/http-request.feature', { multiPart: multiPart })8* response.jsonPath('$.headers.Content-Type').contains('multipart/form-data')9* response.jsonPath('$.headers.Content-Length') == 35410* def client = karate.call('classpath:com/intuit/karate/http/http-client.feature')11* def multiPart1 = multiPart.addFile('file1', 'file1.txt', 'text/plain')12* def multiPart2 = multiPart.addFile('file2', 'file2.txt', 'text/plain')13* def multiPart3 = multiPart.addFile('file3', 'file3.txt', 'text/plain')14* def multiPart4 = multiPart.addFile('file4', 'file4.txt', 'text/plain')15* def multiPart5 = multiPart.addFile('file5', 'file5.txt', 'text/plain')16* response.jsonPath('$.headers.Content-Type').contains('multipart/form-data')17* response.jsonPath('$.headers.Content-Length') == 35418* def client = karate.call('classpath:com/intuit/karate/http/http-client.feature')19* def multiPart1 = multiPart.addFile('

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1 .httpRequest()2 .multiPart('name', 'John Doe')3 .multiPart('age', '33')4 .multiPart('photo', 'karate-logo.png')5 .post()6* match response.jsonPath('name') == 'John Doe'7* match response.jsonPath('age') == 338* match response.jsonPath('photo') == 'karate-logo.png'9 .httpRequest()10 .multiPart('name', 'John Doe')11 .multiPart('age', '33')12 .multiPart('photo', 'karate-logo.png')13 .post()14* match response.jsonPath('name') == 'John Doe'15* match response.jsonPath('age') == 3316* match response.jsonPath('photo') == 'karate-logo.png'17 .httpRequest()18 .multiPart('name', 'John Doe')19 .multiPart('age', '33')20 .multiPart('photo', 'karate-logo.png')21 .post()22* match response.jsonPath('name') == 'John Doe'23* match response.jsonPath('age') == 3324* match response.jsonPath('photo') == 'karate-logo.png'

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1* def request = karate.call('classpath:multipart/prepareRequest.feature')2* def headers = {'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'}3* def headers = {'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'}4* def params = {'param1': 'value1', 'param2': 'value2'}5* def headers = {'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW'}6* def params = {'param1': 'value1', 'param2': 'value2'}7* def cookie = {'cookie1': 'value1', 'cookie2': 'value2'}8* def response = request.multiPart('file', 'file content', 'text/plain', 'file.txt').multiPart('file2', '

Full Screen

Full Screen

multiPart

Using AI Code Generation

copy

Full Screen

1def response = req.send()2def response = req.send()3def response = req.send()4def response = req.send()5def response = req.send()6def response = req.send()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful