【问题标题】:Run multiple curl commands sequentially using java使用java顺序运行多个curl命令
【发布时间】:2021-03-20 08:33:48
【问题描述】:

我能够在终端中顺序运行命令及其工作正常。我想通过 java 实现同样的事情

token=$(curl -H "Content-Type: application/json" -X POST --data @"/Users/surya/KarateUIAutomation/target/surefire-reports/cloud_auth.json" https://xray.cloud.xpand-it.com/api/v2/authenticate| tr -d '"')

在第二个 curl 命令中,第一个令牌作为参数传递

curl -H "Content-Type: application/json" -X POST -H "Authorization: Bearer $token"  --data @"/Users/surya/KarateUIAutomation/target/surefire-reports/testcase.firstUITest.json" https://xray.cloud.xpand-it.com/api/v2/import/execution/cucumber

我已经编写了下面的 java 代码,但不知道如何传递上述 2 个命令并按顺序运行

String[] command = {" "};
           
           
            ProcessBuilder process = new ProcessBuilder(command); 
            Process p;
            try
            {
                p = process.start();
                 BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
                    StringBuilder builder = new StringBuilder();
                    String line = null;
                    while ( (line = reader.readLine()) != null) {
                            builder.append(line);
                            builder.append(System.getProperty("line.separator"));
                    }
                    String result = builder.toString();
                    System.out.print(result);

            }
            catch (IOException e)
            {   System.out.print("error");
                e.printStackTrace();
            }
    }

【问题讨论】:

  • Curl 是一个命令行 http 客户端。您可以使用 java http 客户端执行相同的操作。您必须将 curl 命令转换为 http 概念,然后弄清楚 java http 客户端如何在其 api 中使用这些概念。
  • @M.P.Korstanje 你可以检查下面的 Sérgio 答案并建议如何做

标签: java curl automation jira jira-xray


【解决方案1】:

我同意 M.P. Korstanje,从 java 启动 curl 命令行,在从纯 java HTTP 库调用 URL 之前是一个糟糕的解决方案。

HttpClient.getToken 替换你的第一个 curl 命令 (https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java#L58)

你可以用这个java代码来使用它:

String token = new HttpClient().getToken("{ " +
                "\"client_id\": \"" + xray_client_id + "\", " +
                "\"client_secret\": \"" + xray_client_secret + "\"" +
                " }";);

并且可以更新 HttpClient.publishToXray 以推送黄瓜结果 (https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java#L63)

你可以用这个java代码来使用它:

String json = "Your json"; //See https://docs.getxray.app/display/XRAYCLOUD/Import+Execution+Results+-+REST
new HttpClient().publishToXray(token, json);

【讨论】:

  • 我已经浏览了这篇文章,但是如果我使用那个特定的库,它没有选项可以传递客户端 ID 和客户端密码值来获取令牌。所以不太清楚
  • @After public void jiraReport() throws IOException { //JiraReporting.importExecutionToJira(); HttpClient httpClient = new HttpClient();字符串响应 = httpClient.getToken("empty json"); System.out.println("响应----"+响应); } }
  • @SuryaneelVarshney 我添加了一些用法示例。
【解决方案2】:

您可以在以下代码中看到一个具体示例,取自 repo,它为 REST API 实现了一个 Java 客户端库。 也许您可以使用它(请参阅文档here),从而避免最终实现它。

package net.azae.xray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpClient {
    private final static Logger logger = LoggerFactory.getLogger(HttpClient.class);

    // TODO : get base URL from conf
    String baseUrl = "https://xray.cloud.xpand-it.com";

    public static String jsonPost(String toURL, String data, String token) {
        URL url;
        String response = "";
        try {
            url = new URL(toURL);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            if (token != null)
                conn.setRequestProperty("Authorization", "Bearer " + token);

            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(data);
            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null) {
                    response += line;
                }
            } else {
                response = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static String jsonPost(String toURL, String data) {
        return jsonPost(toURL, data, null);
    }

    String getToken(String json) {
        String response = jsonPost(baseUrl + "/api/v1/authenticate", json);
        return response.replace("\"", "");
    }

    void publishToXray(String token, String inputJson) {
        logger.debug("Publish input: " + inputJson);
        String response = jsonPost(baseUrl + "/api/v1/import/execution", inputJson, token);
        logger.debug("Publish response: " + response);
    }
}

https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java

【讨论】:

  • 调用哪个方法,需要传递哪些参数,才能导入执行结果。 public static void main(String[] args) { jsonPost("toURL", "data", "token"); }
猜你喜欢
  • 2011-09-15
  • 1970-01-01
  • 2018-03-03
  • 2018-09-18
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
相关资源
最近更新 更多