Invalid JSON response from server

Options
arthurgps2
arthurgps2 Member Posts: 3

Hello. So in my Spring application I have this CloudinaryService class with this function that's supposed to take a Base64-enconded image and upload it to Cloudinary:

public String uploadFileFromBase64(String base64) {
		try {
			@SuppressWarnings("rawtypes")
			Map uploadedFile = cloudinary.uploader().upload(base64, // the error is being thrown in this line
					ObjectUtils.emptyMap());
			String publicId = (String) uploadedFile.get("public_id");
			return cloudinary.url().secure(true).generate(publicId);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

and I'm calling it like this:

String thumbnail = form.get("thumbnail");
String cardImageUrl = cloudinaryService.uploadFileFromBase64(thumbnail);

But for some reason, whenever it tries to upload the image, it throws this error:

Invalid JSON response from server A JSONObject text must begin with '{' at 1 [character 2 line 1]

I'm not really sure what's causing this because I used a similar method in another project and it worked fine.

Best Answer

  • arthurgps2
    arthurgps2 Member Posts: 3
    Answer ✓
    Options

    That was it, the credentials. I had them defined in the CloudinaryConfiguration class like this:

    @Value("cloudinary.cloud_name")
    private String cloudName;
    
    @Value("cloudinary.api_key")
    private String apiKey;
    
    @Value("cloudinary.api_secret")
    private String apiSecret;
    

    The @Value annotation wasn't taking the values set in application.properties because I wrote the value in the parenthesis wrong. The moment I corrected it...

    @Value("${cloudinary.cloud_name}")
    private String cloudName;
    
    @Value("${cloudinary.api_key}")
    private String apiKey;
    
    @Value("${cloudinary.api_secret}")
    private String apiSecret;
    

    it started working right back again. It had to do with my own config, after all. Thanks for the help!

Answers

  • Cloudinary Team
    Cloudinary Team Administrator, Cloudinary Staff Posts: 123 admin
    Options

    Hi Arthur,

    Thanks for contacting us.
    In general, the error you are receiving indicates that the upload call is receiving an error response due to a failure, but the error that was returned as an HTML page wasn't parsed successfully and you couldn't see the actual cause for the failure.

    When the check the logs for your cloud -

    The only error listed says "ResourceInvalid: Empty file"

    Would it be possible to confirm that you're passing a valid base64 and not an empty value?

    Thanks,
    John

    Helpful Links For You
    💬 Share questions, connect with other users in our Cloudinary Community forums and Discord server!
    🧑‍🎓 Join our Cloudinary Academy for free courses, workshops and other educational resources.
    📄 Read our documentation for in-depth details on Cloudinary product features and capabilities
    📰 Check out the Cloudinary blog for the latest company news and insights

  • arthurgps2
    arthurgps2 Member Posts: 3
    Options

    Okay, I just checked it using

    System.out.println(form.get("thumbnail"))
    

    where the "thumbnail" form is where the image is supposed to be. This is what I got:

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAWgAAAFoCAYAAAB65WHVAAAAAXNSR0IArs4c6QAAIABJREFUeF7svQeYHOd1JXqqqqtzTw7IIEiAYBJF ...
    

    So yeah, that's a base64 image right there

  • Cloudinary_John_H
    Cloudinary_John_H Cloudinary Staff Posts: 36
    Options

    Hi Arthur,

    Can you paste the full base64, so I can decode it and confirm that it loads successfully?

    Also, the last logs that mention your cloud name are from February 4 - have run the upload call since then?

    If you have, please double-check your credentials, the call may not be reaching us.

    Looking forward to your response,

    John

  • Cloudinary_John_H
    Cloudinary_John_H Cloudinary Staff Posts: 36
    Options

    Wonderful, glad you were able to figure it out!