简体   繁体   中英

How to Send API header request through Selenium and get response back

I have to automate API through selenium+TestNG (Java). I know its not a good habit to automate APIs using selenium code but still I have got this to do.

Scenario - there is a login API and have to send the email and password and get response back (response code 200). Also can we print the response information also?

If you are working on java you can use JAVA socket libraries for the same:

Here are the sample code for POST API:

    URL obj = new URL(url);
    con = (HttpsURLConnection) obj.openConnection();
    // add request header
    con.setRequestMethod("POST");
    con.setRequestProperty("User-Agent", USER_AGENT);

    String urlParameters = "j_username=" + user + "&j_password=" + pass
            + "";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.flush();
    wr.close();

    responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + urlParameters);
    System.out.println("Response Code : " + responseCode);

For GET API:

obj = new URL(url);
    obj.openConnection();
    con = (HttpsURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");
    CookieHandler.setDefault(new CookieManager());

    // add request header
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.8l");
    con.setRequestProperty("Accept", "*/*");
    con.setRequestProperty(
            "Cookie",
            "BIGipServerPRODCAN-Default=423078080.2087.0000; __utma=44365112.1659763098.1418886605.1427784911.1441869730.4; __utmc=44365112; __utmz=44365112.1427784911.3.2.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); JSESSIONID="
                    + cookieValue);

    responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    File file = new File(prop.getFilePath() + "//XMLs//" + fileName
            + ".xml");
    file.delete();
    FileWriter fstream = new FileWriter(prop.getFilePath() + "//XMLs//"
            + fileName + ".xml", true);

    BufferedWriter out = new BufferedWriter(fstream);
    while ((inputLine = in.readLine()) != null) {

        out.write(inputLine.toString());
        out.newLine();

        response.append(inputLine);
    }

    in.close();
    out.close();`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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