How to use post method of com.testsigma.util.HttpClient class

Best Testsigma code snippet using com.testsigma.util.HttpClient.post

Source:RestAPIUtil.java Github

copy

Full Screen

1package com.testsigma.plugins.util;2import com.google.gson.JsonObject;3import com.google.gson.JsonParser;4import hudson.model.BuildListener;5import hudson.util.Secret;6import org.apache.commons.httpclient.HttpStatus;7import org.apache.commons.io.FileUtils;8import org.apache.commons.lang.exception.ExceptionUtils;9import org.apache.http.HttpEntity;10import org.apache.http.HttpHeaders;11import org.apache.http.client.methods.CloseableHttpResponse;12import org.apache.http.client.methods.HttpGet;13import org.apache.http.client.methods.HttpPost;14import org.apache.http.entity.ContentType;15import org.apache.http.entity.StringEntity;16import org.apache.http.impl.client.CloseableHttpClient;17import org.apache.http.impl.client.HttpClients;18import org.apache.http.util.EntityUtils;19import java.io.File;20import java.io.IOException;21import java.io.InputStream;22import java.io.PrintStream;23import java.nio.charset.StandardCharsets;24import java.util.Properties;25public class RestAPIUtil {26 private Properties properties = new Properties();27 BuildListener listener = null;28 private String apiEndPoint = null;29 public RestAPIUtil(BuildListener listener,String apiEndPoint) throws IOException {30 this.listener = listener;31 this.apiEndPoint = apiEndPoint;32 InputStream is = null;33 try {34 is = getClass().getResourceAsStream("/testsigma.properties");35 properties.load(is);36 } finally {37 if (is != null) is.close();38 }39 }40 public static boolean isNullOrEmpty(String val) {41 return (val == null || val.trim().length() == 0);42 }43 public Object getDataFromResponse(CloseableHttpResponse response, boolean isReport) throws IOException {44 if (response != null) {45 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {46 String jsonString = EntityUtils.toString(response.getEntity());47 if (isReport) {48 return jsonString;49 } else {50 return new JsonParser().parse(jsonString).getAsJsonObject();51 }52 } else {53 listener.getLogger().println(EntityUtils.toString(response.getEntity()));54 throw new RuntimeException("Failed : HTTP error code : "55 + response.getStatusLine().getStatusCode());56 }57 } else {58 throw new RuntimeException("Http response is null");59 }60 }61 public String startTestSuiteExecution(String testPlanId, Secret apiKey)62 throws IOException {63 String executionTriggerURL = getExecutionAPI();64 JsonObject jsonData = new JsonObject();65 jsonData.addProperty("executionId", testPlanId);66 CloseableHttpClient httpclient = null;67 JsonObject dataObject = null;68 try {69 httpclient = HttpClients.createDefault();70 HttpPost httpPost = new HttpPost(executionTriggerURL);71 httpPost.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getPlainText().trim());72 httpPost.setHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, "application/json; " + StandardCharsets.UTF_8);73 httpPost.setHeader(HttpHeaders.ACCEPT, "application/json; " + StandardCharsets.UTF_8);74 HttpEntity stringEntity = new StringEntity(jsonData.toString(), ContentType.APPLICATION_JSON);75 httpPost.setEntity(stringEntity);76 CloseableHttpResponse response = httpclient.execute(httpPost);77 dataObject = (JsonObject) getDataFromResponse(response, false);78 } finally {79 if (httpclient != null) httpclient.close();80 }81 listener.getLogger().println("Execution started:" + dataObject.toString());82 return dataObject.get("id").toString();83 }84 private String getExecutionAPI() {85 return String.format("%s%s",apiEndPoint,properties.getProperty("testsigma.execution.trigger.restapi"));86 }87 public boolean runExecutionStatusCheck(BuildListener listener, Secret apiKey, String runId, int maxWaitTimeInMinutes, int pollIntervalInMins)88 throws IOException, InterruptedException {89 // Safe check, if max build wait time is less than pre-defined poll interval90 pollIntervalInMins = (maxWaitTimeInMinutes < pollIntervalInMins) ? maxWaitTimeInMinutes : pollIntervalInMins;91 PrintStream consoleOut = listener.getLogger();92 String statusURL = String.format("%s/%s",getExecutionAPI(), runId);93 JsonObject responseObj;94 int noOfPolls = maxWaitTimeInMinutes / pollIntervalInMins;95 for (int i = 1; i <= noOfPolls; i++) {96 responseObj = (JsonObject) getTestPlanExecutionStatus(statusURL, apiKey.getPlainText().trim());97 String status = responseObj.get("status").toString();98 consoleOut.println("Test execution Status..." + status);99 if (status.trim().contains("STATUS_IN_PROGRESS")) {100 try {101 Thread.sleep(pollIntervalInMins * 1000 * 60L);102 consoleOut.println("Total time waited so far(in minutes)::" + ((i) * pollIntervalInMins));103 } catch (InterruptedException e) {104 consoleOut.println("Thread interrupted by Jenkins..");105 throw e;106 }107 } else {108 consoleOut.println("Test suites Execution completed");109 return true;110 }111 }112 return false;113 }114 private Object getTestPlanExecutionStatus(String statusURL, String apiKey) throws IOException {115 CloseableHttpClient httpclient = null;116 Object responseObj;117 try {118 httpclient = HttpClients.createDefault();119 HttpGet httpGet = new HttpGet(statusURL);120 httpGet.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.trim());121 httpGet.setHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, "application/json; " + StandardCharsets.UTF_8);122 httpGet.setHeader(HttpHeaders.ACCEPT, "application/json; " + StandardCharsets.UTF_8);123 CloseableHttpResponse response = httpclient.execute(httpGet);124 responseObj = getDataFromResponse(response, false);125 } finally {126 if (httpclient != null) httpclient.close();127 }128 return responseObj;129 }130 public String getReportsFilePath(BuildListener listener, String reportsFolder, Long buildID,131 String reportFileName) {132 String tempDir = System.getProperty("java.io.tmpdir");133 if (!isNullOrEmpty(reportsFolder)) {134 File givenDir = new File(reportsFolder);135 if (givenDir.exists() && givenDir.isDirectory()) {136 return String.format("%s%s%s%s%s", reportsFolder, File.separator, buildID, File.separator, reportFileName);137 }138 }139 listener.getLogger().println("System Temp dir:" + tempDir);140 return String.format("%s%s%s%s%s", tempDir, File.separator, buildID, File.separator, reportFileName);141 }142 public void saveTestReports(Secret apiKey, String runId, String reportsFilePath) throws IOException {143 CloseableHttpClient httpclient = null;144 Object responseObj = null;145 String reportsAPI = String.format("%s/%s",getReportsAPI(), runId);146 try {147 httpclient = HttpClients.createDefault();148 HttpGet httpGet = new HttpGet(reportsAPI);149 httpGet.setHeader(org.apache.http.HttpHeaders.AUTHORIZATION, "Bearer " + apiKey.getPlainText().trim());150 httpGet.setHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, "application/json; " + StandardCharsets.UTF_8);151 httpGet.setHeader(HttpHeaders.ACCEPT, "application/xml; " + StandardCharsets.UTF_8);152 CloseableHttpResponse response = httpclient.execute(httpGet);153 responseObj = getDataFromResponse(response, true);154 File reportsFile = new File(reportsFilePath);155 FileUtils.writeStringToFile(reportsFile, responseObj.toString());156 listener.getLogger().println("Reports saved to:"+reportsFile.getAbsolutePath());157 }catch(Exception e){158 listener.getLogger().println("Report generation failed..");159 listener.getLogger().println(ExceptionUtils.getStackTrace(e));160 }161 finally {162 if (httpclient != null) httpclient.close();163 }164 }165 private String getReportsAPI() {166 return String.format("%s%s", apiEndPoint,properties.getProperty("testsigma.reports.junit.restapi"));167 }168}...

Full Screen

Full Screen

Source:TrelloService.java Github

copy

Full Screen

...36 HashMap<String, String> payload = new HashMap<>();37 payload.put("name", mapping.getFields().get("name").toString());38 payload.put("desc", mapping.getFields().get("description").toString());39 payload.put("idList", mapping.getFields().get("issueTypeId").toString());40 HttpResponse<JsonNode> response = httpClient.post("https://api.trello.com/1/cards?key=" + applicationConfig.getPassword() + "&token=" + applicationConfig.getToken(), getHeaders(), payload, new TypeReference<JsonNode>() {41 });42 if (response.getStatusCode() != HttpStatus.SC_OK) {43 log.error(response.getResponseText());44 throw new TestsigmaException("Problem while creating Trello issue with ::" + mapping.getFields());45 }46 mapping.setExternalId(String.valueOf(response.getResponseEntity().get("id")).replace("\"", ""));47 return mapping;48 }49 public TestCaseResultExternalMapping link(TestCaseResultExternalMapping mapping) throws TestsigmaException {50 HashMap<String, String> payload = new HashMap<>();51 payload.put("text", "Linked to testsigma results [" + config.getServerUrl() + "/ui/td/test_case_results/" + mapping.getTestCaseResultId() + "] :: " + mapping.getTestCaseResult().getTestCase().getName());52 HttpResponse<JsonNode> response = httpClient.post("https://api.trello.com/1/cards/" + mapping.getExternalId() + "/actions/comments?key=" + applicationConfig.getPassword() + "&token=" + applicationConfig.getToken(), getHeaders(), payload, new TypeReference<JsonNode>() {53 });54 if (response.getStatusCode() != HttpStatus.SC_OK) {55 log.error(response.getResponseText());56 throw new TestsigmaException("Problem while Linking Trello issue with ::" + mapping.getFields());57 }58 return mapping;59 }60 public TestCaseResultExternalMapping unlink(TestCaseResultExternalMapping mapping) throws TestsigmaException {61 HashMap<String, String> payload = new HashMap<>();62 payload.put("text", "Unlinked from testsigma results [" + config.getServerUrl() + "/ui/td/test_case_results/" + mapping.getTestCaseResultId() + "] :: " + mapping.getTestCaseResult().getTestCase().getName());63 HttpResponse<JsonNode> response = httpClient.post("https://api.trello.com/1/cards/" + mapping.getExternalId() + "/actions/comments?key=" + applicationConfig.getPassword() + "&token=" + applicationConfig.getToken(), getHeaders(), payload, new TypeReference<JsonNode>() {64 });65 if (response.getStatusCode() != HttpStatus.SC_OK) {66 log.error(response.getResponseText());67 throw new TestsigmaException("Problem while UnLinking Trello issue with ::" + mapping.getFields());68 }69 return mapping;70 }71 //card72 public JsonNode getIssue(String cardId) throws TestsigmaException {73 HttpResponse<JsonNode> response = httpClient.get("https://api.trello.com/1/cards/" + cardId + "?key=" + applicationConfig.getPassword() + "&token=" + applicationConfig.getToken(), getHeaders(), new TypeReference<JsonNode>() {74 });75 return response.getResponseEntity();76 }77 //cards...

Full Screen

Full Screen

Source:BugZillaService.java Github

copy

Full Screen

...40 payload.put("version", mapping.getFields().get("version").toString());41 payload.put("product", mapping.getFields().get("project").toString());42 payload.put("op_sys", "All");43 payload.put("rep_platform", "All");44 HttpResponse<JsonNode> response = httpClient.post(integrations.getUrl() + "/rest/bug?api_key=" + integrations.getToken(), getHeaders(), payload, new TypeReference<JsonNode>() {45 });46 if (response.getStatusCode() != HttpStatus.SC_OK) {47 log.error(response.getResponseText());48 throw new TestsigmaException("Problem while creating BugZilla issue with ::" + mapping.getFields());49 }50 mapping.setExternalId(String.valueOf(response.getResponseEntity().get("id")));51 return mapping;52 }53 public TestCaseResultExternalMapping link(TestCaseResultExternalMapping mapping) throws TestsigmaException {54 HashMap<String, String> payload = new HashMap<>();55 payload.put("comment", "Linked to testsigma results [" + config.getServerUrl() + "/ui/td/test_case_results/" + mapping.getTestCaseResultId() + "] :: " + mapping.getTestCaseResult().getTestCase().getName());56 HttpResponse<String> response = httpClient.post(integrations.getUrl() + "/rest/bug/" + mapping.getExternalId() + "/comment?api_key=" + this.integrations.getToken(), getHeaders(), payload, new TypeReference<String>() {57 });58 if (response.getStatusCode() != HttpStatus.SC_CREATED) {59 log.error(response.getResponseText());60 throw new TestsigmaException("Problem while Linking BugZilla issue with ::" + mapping.getExternalId());61 }62 return mapping;63 }64 public TestCaseResultExternalMapping unlink(TestCaseResultExternalMapping mapping) throws TestsigmaException {65 HashMap<String, String> payload = new HashMap<>();66 payload.put("comment", "Unlinked from testsigma results [" + config.getServerUrl() + "/ui/td/test_case_results/" + mapping.getTestCaseResultId() + "] :: " + mapping.getTestCaseResult().getTestCase().getName());67 HttpResponse<String> response = httpClient.post(integrations.getUrl() + "/rest/bug/" + mapping.getExternalId() + "/comment?api_key=" + this.integrations.getToken(), getHeaders(), payload, new TypeReference<String>() {68 });69 if (response.getStatusCode() != HttpStatus.SC_CREATED) {70 log.error(response.getResponseText());71 throw new TestsigmaException("Problem while unLinking BugZilla issue with ::" + mapping.getExternalId());72 }73 return mapping;74 }75 public JsonNode getIssuesList(String project, String issueType, String version) throws TestsigmaException {76 HttpResponse<JsonNode> response = httpClient.get(integrations.getUrl() + "/rest/bug?project=" + project + "&component=" + issueType + "&version=" + version, getHeaders(), new TypeReference<JsonNode>() {77 });78 return response.getResponseEntity();79 }80 public JsonNode getIssue(Long issueId) throws TestsigmaException {81 HttpResponse<JsonNode> response = httpClient.get(integrations.getUrl() + "/rest/bug/" + issueId, getHeaders(), new TypeReference<JsonNode>() {...

Full Screen

Full Screen

post

Using AI Code Generation

copy

Full Screen

1import com.testsigma.util.HttpClient;2import java.util.HashMap;3import java.util.Map;4public class Test {5 public static void main(String[] args) throws Exception {6 HttpClient client = new HttpClient();7 Map<String, String> headers = new HashMap<String, String>();8 headers.put("Content-Type", "application/json");9 String json = "{\"key\":\"value\"}";10 System.out.println(response);11 }12}13import com.testsigma.util.HttpClient;14import java.util.HashMap;15import java.util.Map;16public class Test {17 public static void main(String[] args) throws Exception {18 HttpClient client = new HttpClient();19 Map<String, String> headers = new HashMap<String, String>();20 headers.put("Content-Type", "application/json");21 String json = "{\"key\":\"value\"}";22 System.out.println(response);23 }24}25import com.testsigma.util.HttpClient;26import java.util.HashMap;27import java.util.Map;28public class Test {29 public static void main(String[] args) throws Exception {30 HttpClient client = new HttpClient();31 Map<String, String> headers = new HashMap<String, String>();32 headers.put("Content-Type", "application/json");33 String json = "{\"key\":\"value\"}";34 System.out.println(response);35 }36}37import com.testsigma.util.HttpClient;38import java.util.HashMap;39import java.util.Map;40public class Test {41 public static void main(String[] args) throws Exception {42 HttpClient client = new HttpClient();43 Map<String, String> headers = new HashMap<String, String>();44 headers.put("Content-Type", "application/json");45 String json = "{\"key\":\"value\"}";46 System.out.println(response);47 }48}

Full Screen

Full Screen

post

Using AI Code Generation

copy

Full Screen

1package com.testsigma.util;2import java.io.*;3import java.net.*;4import java.util.*;5import org.apache.commons.httpclient.*;6import org.apache.commons.httpclient.methods.*;7import org.apache.commons.httpclient.params.*;8import org.apache.commons.httpclient.protocol.*;9import org.apache.commons.httpclient.util.*;10import org.apache.commons.httpclient.cookie.*;11import org.apache.commons.httpclient.auth.*;12import org.apache.commons.httpclient.methods.multipart.*;13import org.apache.commons.httpclient.methods.multipart.StringPart;14import org.apache.commons.httpclient.methods.multipart.FilePart;15import org.apache.commons.httpclient.methods.multipart.PartSource;16import org.apache.commons.httpclient.methods.multipart.InputStreamPartSource;17import org.apache.commons.httpclient.methods.multipart.StringPartSource;18import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;19import org.apache.commons.httpclient.methods.multipart.FilePartSource;20import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;21import org.apache.commons.httpclient.methods.multipart.Part;22import org.apache.commons.httpclient.methods.multipart.PartBase;23import org.apache.commons.httpclient.methods.multipart.StringPartSource;24import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;25import org.apache.commons.httpclient.methods.multipart.FilePartSource;26import org.apache.commons.httpclient.methods.multipart.InputStreamPartSource;27import org.apache.commons.httpclient.methods.multipart.PartSource;28import org.apache.commons.httpclient.methods.multipart.StringPart;29import org.apache.commons.httpclient.methods.multipart.FilePart;30import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;31import org.apache.commons.httpclient.methods.multipart.Part;32import org.apache.commons.httpclient.methods.multipart.PartBase;33import org.apache.commons.httpclient.methods.multipart.StringPartSource;34import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;35import org.apache.commons.httpclient.methods.multipart.FilePartSource;36import org.apache.commons.httpclient.methods.multipart.InputStreamPartSource;37import org.apache.commons.httpclient.methods.multipart.PartSource;38import org.apache.commons.httpclient.methods.multipart.StringPart;39import org.apache.commons.httpclient.methods.multipart.FilePart;40import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;41import org.apache.commons.httpclient.methods.multipart.Part;42import org.apache.commons.httpclient.methods.multipart.PartBase;43import org.apache.commons.httpclient.methods.multipart.StringPartSource;44import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource;45import org.apache.commons.httpclient.methods.multipart.FilePartSource;46import org.apache.commons.httpclient.methods.multipart.InputStreamPartSource;47import org.apache.commons.httpclient.methods.multipart.PartSource;48import org.apache

Full Screen

Full Screen

post

Using AI Code Generation

copy

Full Screen

1package com.testsigma.util;2import java.io.IOException;3import org.apache.http.HttpResponse;4import org.apache.http.ParseException;5import org.apache.http.client.ClientProtocolException;6import org.apache.http.entity.StringEntity;7import org.apache.http.util.EntityUtils;8import com.testsigma.util.HttpClient;9public class PostMethod {10 public static void main(String[] args) throws ClientProtocolException,11 IOException {12 HttpClient httpclient = new HttpClient();13 HttpResponse response = httpclient.post(url);14 System.out.println("Response Code : "15 + response.getStatusLine().getStatusCode());16 String responseString = EntityUtils.toString(response.getEntity());17 System.out.println("Response String : " + responseString);18 }19}20package com.testsigma.util;21import java.io.IOException;22import java.net.URI;23import java.net.URISyntaxException;24import java.util.ArrayList;25import java.util.List;26import org.apache.http.Header;27import org.apache.http.HttpEntity;28import org.apache.http.HttpResponse;29import org.apache.http.NameValuePair;30import org.apache.http.StatusLine;31import org.apache.http.client.ClientProtocolException;32import org.apache.http.client.HttpClient;33import org.apache.http.client.entity.UrlEncodedFormEntity;34import org.apache.http.client.methods.HttpGet;35import org.apache.http.client.methods.HttpPost;36import org.apache.http.client.utils.URIBuilder;37import org.apache.http.entity.StringEntity;38import org.apache.http.impl.client.DefaultHttpClient;39import org.apache.http.message.BasicNameValuePair;40import org.apache.http.util.EntityUtils;41public class HttpClient {42 HttpClient httpClient = new DefaultHttpClient();43 HttpResponse response;44 public HttpResponse get(String url) throws ClientProtocolException,45 IOException {46 HttpGet getRequest = new HttpGet(url);47 response = httpClient.execute(getRequest);48 return response;49 }

Full Screen

Full Screen

post

Using AI Code Generation

copy

Full Screen

1import com.testsigma.util.HttpClient;2public class 2 {3 public static void main(String[] args) throws Exception {4 String request = "{\"name\":\"abc\",\"age\":23}";5 String response = HttpClient.post(url, request);6 System.out.println(response);7 }8}9import com.testsigma.util.HttpClient;10public class 3 {11 public static void main(String[] args) throws Exception {12 String request = "{\"name\":\"abc\",\"age\":23}";13 String response = HttpClient.put(url, request);14 System.out.println(response);15 }16}17import com.testsigma.util.HttpClient;18public class 4 {19 public static void main(String[] args) throws Exception {20 String response = HttpClient.delete(url);21 System.out.println(response);22 }23}24import com.testsigma.util.HttpClient;25public class 5 {26 public static void main(String[] args) throws Exception {27 String response = HttpClient.get(url);28 System.out.println(response);29 }30}31import com.testsigma.util.HttpClient;32public class 6 {33 public static void main(String[] args) throws Exception {

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.

Run Testsigma automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful