@Override protected HttpHeaders getHttpHeaders() { return new HttpHeaders().setAccept(ACCEPT_HEADER).setBasicAuthentication(apiKey, ""); } }
protected static String uploadFile(String targetAppPath, String serverURL, String testDroidApiKey) throws IOException { final HttpHeaders headers = new HttpHeaders().setBasicAuthentication(testDroidApiKey, ""); HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(request -> { request.setParser(new JsonObjectParser(JSON_FACTORY)); request.setHeaders(headers); }); MultipartFormDataContent multipartContent = new MultipartFormDataContent(); FileContent fileContent = new FileContent("application/octet-stream", new File(targetAppPath)); MultipartFormDataContent.Part filePart = new MultipartFormDataContent.Part("file", fileContent); multipartContent.addPart(filePart); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(serverURL+"/upload"), multipartContent); AppiumResponse appiumResponse = request.execute().parseAs(AppiumResponse.class); logger.info("File id:" + appiumResponse.uploadStatus.fileInfo.file); return appiumResponse.uploadStatus.fileInfo.file; }
@Override public void initialize(HttpRequest request) throws IOException { request.setParser(new JsonObjectParser(new JacksonFactory())); request.getHeaders().setBasicAuthentication(getUsername(), getPassword()); } });
public static String uploadFile(String appPath, String serverURL, String username, String password) throws IOException { HttpHeaders headers = new HttpHeaders(); if (username != null) { headers.setBasicAuthentication(username, password); } HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory(request -> { request.setParser(new JsonObjectParser(new JacksonFactory())); request.setHeaders(headers); }); MultipartContent multipartContent = new MultipartContent(); FileContent fileContent = new FileContent("application/octet-stream", new File(appPath)); MultipartContent.Part filePart = new MultipartContent.Part(new HttpHeaders() .set("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileContent.getFile().getName() + "\""), fileContent); multipartContent.addPart(filePart); HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(serverURL), multipartContent); HttpResponse response = request.execute(); LoggerFactory.getLogger(AppiumDriverWrapper.class).info("response:" + response.parseAsString()); AppiumResponse appiumResponse = request.execute().parseAs(AppiumResponse.class); LoggerFactory.getLogger(AppiumDriverWrapper.class).info("File id:" + appiumResponse.uploadStatus.fileInfo.file); return appiumResponse.uploadStatus.fileInfo.file; }
headers.setBasicAuthentication(profile.getCredential_user(), profile.getCredential_pwd()); logger.info(PrintUtil.formatRequest(request));