Search code examples
javaspringspring-bootjpeghttpresponse

Saving an image returned by an API in JSON


I am using a Meme generator API. My goal is to generate memes with the API , be able to view and save them as JPG images.

When I try to use the Java code provided by the creator, I get an error message.

Here's the provided code that fails:

  HttpResponse<JsonNode> response = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Condescending-Wonka&top=Yolo&bottom=HA")
      .header("X-RapidAPI-Key", "TOP_SECRET")
      .asJson();

The error message:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'APIController' defined in file [C:\yaml\out\production\classes\com\example\demo\controllers\APIController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'APIService' defined in file [C:\yaml\out\production\classes\com\example\demo\services\APIService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.demo.services.APIService]: Constructor threw exception; nested exception is com.mashape.unirest.http.exceptions.UnirestException: java.lang.RuntimeException: java.lang.RuntimeException: org.json.JSONException: A JSONArray text must start with '[' at 1 [character 2 line 1]

It says the field

response

cannot be parsed as a JSONArray, so I tried this code snippet instead:

  HttpResponse<String> meme = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Impossibru-Guy-Original&top=Random+meme&bottom=Bottom+text")
      .header("X-RapidAPI-Key", "TOP_SECRET")
      .asString();

In this case the code runs, but as I call the endpoint, I get loads of

ufffd

snippets in the String, which basically means that I am trying to read a code which has no representation in Unicode. I've seen a solution here how I could deal with this problem but I'm not pretty sure I'm going on a right way.

According to website, on which the API is provided, I should get something like this as a response:

enter image description here

Could you give me any pieces of advice how to approach this problem?

Thanks for your help in advance.


Solution

  • Eventually I could sort out the problem with some help. Here it is:

    HttpResponse httpResponse = Unirest.get("https://ronreiter-meme-generator.p.rapidapi.com/meme?font=Impact&font_size=50&meme=Condescending-Wonka&top=Top+text&bottom=Bottom+text")
            .header("X-RapidAPI-Key", "YOUR_SECRET_API_KEY")
            .asBinary();
    InputStream inputStream = httpResponse.getRawBody();
    BufferedImage imBuff = ImageIO.read(inputStream);
    String filePath = "C:/x/x.jpg";
    File file = new File(filePath);
    ImageIO.write(imBuff, "jpg", file);
    

    So here are the points to do:

    1. Retrieve the response as binary data
    2. Transform it to an InputStream
    3. Create a BufferedImage from it
    4. Create a File with the specified filePath
    5. Write the BufferedImage into the FIle