Forum Discussion

sonya_m's avatar
sonya_m
SmartBear Alumni (Retired)
4 years ago
Solved

[TechCorner Challenge #10] Sending HTTP requests and parsing JSON in TestComplete

Hi everyone!

 

Let’s start September with a new challenge in the TestComplete Community! We are going to learn something great today. And, by completing the tasks, you will not only improve your TestComplete skills but also get into the TechCorner Leaderboard.

 

TestComplete allows sending HTTP requests using the aqHttp object. Let's see if you can solve our task based on this functionality!

 

Task: Create a script in TestComplete that will get an image link from this API endpoint (https://dog.ceo/api/breeds/image/random) and save the image on the machine. Post your script and the image you got to the comments!

 

Note: the provided endpoint does not require any authorization.

 

Difficulty:

 

Steps to follow:

1. Send a GET request to https://dog.ceo/api/breeds/image/random. Check the status of the request - if it is successful, the response will return a JSON that contains a link to a random picture of a dog.

2. Parse the returned JSON to extract the link to the image. JavaScript and Python provide support for JSON out of the box; for other languages, you might want to parse JSON as a string or use regular expressions.

3. Send a GET request to the URL obtained from the previous response - this will return an image.

4. Save the response as an image to a JPG file by calling the SaveToFile method like this: response.SaveToFile("C:\\image.jpg")

 

Best of luck!😊

  • Task: Create a script in TestComplete that will get an image link from this API endpoint (https://dog.ceo/api/breeds/image/random) and save the image on the machine. Post your script and the image you got to the comments!

     

    This is a solution created for [TechCorner Challenge #10]

     

    Hi Sonya,

     

    I'd do it like this.

     

     

    function getThisDog(https) {
      var aqHttpRequest = aqHttp.CreateGetRequest(https);
      
      aqHttpRequest.SetHeader("Accept", "application/vnd.api+json; version=1");
      aqHttpRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
      aqHttpRequest.SetHeader("Accept-Language", "pl");
      aqHttpRequest.SetHeader("Accept-Charset", "utf-8, iso-8859-13;q=0.8");
      aqHttpRequest.SetHeader("Content-Language", "pl");
    
      var aqHttpRes = aqHttpRequest.Send();  
      Log.Message(aqHttpRes.Text);
      return aqHttpRes;
    }
    
    function parseThisDog() {
      let jsonResponse = getThisDog("https://dog.ceo/api/breeds/image/random");
      
      if(jsonResponse.StatusCode === 200) {
        let doggyJson = JSON.parse(jsonResponse.Text);
    
        let dogImage = getThisDog(doggyJson.message);
        let randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
        dogImage.SaveToFile("C:\\TEST-TRASH\\" + randomString + "dog.jpg");
    
      } else {
        Log.Error("Something went wrong while trying to connect")
      }
      
    }

     

     

    Screen of img in attach.

2 Replies

  • Wamboo's avatar
    Wamboo
    Community Hero

    Task: Create a script in TestComplete that will get an image link from this API endpoint (https://dog.ceo/api/breeds/image/random) and save the image on the machine. Post your script and the image you got to the comments!

     

    This is a solution created for [TechCorner Challenge #10]

     

    Hi Sonya,

     

    I'd do it like this.

     

     

    function getThisDog(https) {
      var aqHttpRequest = aqHttp.CreateGetRequest(https);
      
      aqHttpRequest.SetHeader("Accept", "application/vnd.api+json; version=1");
      aqHttpRequest.SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
      aqHttpRequest.SetHeader("Accept-Language", "pl");
      aqHttpRequest.SetHeader("Accept-Charset", "utf-8, iso-8859-13;q=0.8");
      aqHttpRequest.SetHeader("Content-Language", "pl");
    
      var aqHttpRes = aqHttpRequest.Send();  
      Log.Message(aqHttpRes.Text);
      return aqHttpRes;
    }
    
    function parseThisDog() {
      let jsonResponse = getThisDog("https://dog.ceo/api/breeds/image/random");
      
      if(jsonResponse.StatusCode === 200) {
        let doggyJson = JSON.parse(jsonResponse.Text);
    
        let dogImage = getThisDog(doggyJson.message);
        let randomString = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
        dogImage.SaveToFile("C:\\TEST-TRASH\\" + randomString + "dog.jpg");
    
      } else {
        Log.Error("Something went wrong while trying to connect")
      }
      
    }

     

     

    Screen of img in attach.

    • sonya_m's avatar
      sonya_m
      SmartBear Alumni (Retired)

      Wamboo Great script! 👍

      Also, let me post the dog you got inline here, it looks hilarious😄