简体   繁体   English

使用 java 按顺序运行多个 curl 命令

[英]Run multiple curl commands sequentially using java

i am able to run both the commands in terminal sequentialy and its working fine.same thing i want to achieve through java我能够在终端中连续运行命令及其工作正常。我想通过 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 '"')

In 2nd curl command the 1st token to be passed as parameter在第二个 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

I have written the below java code but not sure how to pass the above 2 commands and run sequentially我已经编写了下面的 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();
            }
    }

You can see a concrete example in the following code, taken from this repo which implements a Java client library for REST API.您可以在以下代码中看到一个具体示例,该代码取自此repo ,它为 REST API 实现了 Java 客户端库。 Perhaps you can use it (see docs here ) and thus avoid having to implement it in your end.也许您可以使用它(请参阅此处的文档),从而避免最终实现它。

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 https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java

I'm agree with MP Korstanje, launching curl command line from java it's a poor solution in front of calling URLs from pure java HTTP library. I'm agree with MP Korstanje, launching curl command line from java it's a poor solution in front of calling URLs from pure java HTTP library.

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

You can use it with this java code:您可以将其与此 java 代码一起使用:

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

and HttpClient.publishToXray could be update to push cucumber result ( https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java#L63 )并且 HttpClient.publishToXray 可以更新以推送 cucumber 结果( https://gitlab.com/azae/outils/junit-xray/-/blob/master/src/main/java/net/azae/xray/HttpClient.java# L63 )

And you can use it with this java code:您可以将其与此 java 代码一起使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM