1

Can you help me recreating this curl command into Rest callout...?

    curl --request POST \
  --url https://api.imgur.com/3/image \
  --header 'authorization: Client-ID {{clientId}}' \
  --header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  --form image=R0lGODlhAQABAIAAAAAAAP/

What I have written so far...

String apiUrl = 'https://api.imgur.com/3/image'; 
        String apiKey = 'f714837c34e1426'; 
        String boundary = '----WebKitFormBoundary7MA4YWxkTrZu0gW';
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('POST');    
        req.setEndpoint(url);
        Blob bodyValue = file;
        String authorizationHeader = 'Client-ID ' + apiKey;
        req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
        req.setHeader('Authorization', authorizationHeader);
        req.setBodyAsBlob(bodyValue);
        HTTPResponse res = http.send(req);
        system.debug(res.getBody());
        return res.getBody();

I am bit confused on How to use the boundary and add the form data as

image=R0lGODlhAQABAIAAAAAAAP

P.S. I did refer other posts that are similar to this...But they were bit confusing and didn't get me anywhere.

Thanks....

1 Answer 1

1

I've covered multipart/formdata support from Apex with respect to the Einstien Predictive Vision service in a blog post.

The short answer is it's a reasonably complicated as you need to manually build up the formdata body in code while making careful consideration to ensure the intermediate base 64 encoding doesn't mess up the binary encoding.

You definitely shouldn't be passing the binary data from the file directly off as the request body. It will be missing the required boundary markers.

Instead have a look at the uploadFile method by Grant Wickman in Post multipart without Base64 Encoding the body. It shouldn't need much modification to produce what you need. Mostly just adding the Authorization header and getting the response body.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .