1

May you have already tryed this solution, that use base64

May base64 encoded request work for you, but sometimes add some blank spaces in your params or params values is not a option, that is my case.

The API that I am working on requires an e-mail param, but the API dosent recognize an e-mail with some blank spaces on the end as a valid e-mail

So I was wondering if has another way to do this

1 Answer 1

4

Looks like there was a way to do this

I have develop this other option. The solution use hexadecimal to encode the entire body, you can use this with multiple params and files as you need

The use is very simple and clear, see the example bellow

String fileName = 'file.png';
String fileContent = ''; //base64 encoded

Blob formData = HttpHexFormBuilder.build()
        .writeParam( 'id', '123' )
        .writeParam( 'message', 'my second paramter' )
        .writeFile( 'file', fileName, fileContent )
        .getFormAsBlob();

HttpRequest request = new HttpRequest();
request.setEndpoint(endpoint);
request.setHeader( 'Connection', 'keep-alive' );
request.setHeader( 'Content-Length', String.valueOf(formData.size()) );
request.setHeader( 'Content-Type', HttpHexFormBuilder.GetContentType() );
request.setBodyAsBlob(formData);

Http http = new Http();        
HttpResponse respose = http.send(request);

You have to use the HttpHexFormBuilder class to build the blob that you will use as body of your HtttpRequest

The entire code of the HttpHexFormBuilder is avaliable here, with the same sample

Let me know if this was helpfull for you

1
  • Exactly what I was about to say. P.S. though, hex encoding uses 25% more memory than base64 encoding. The tradeoff, though, is you don't need to figure out padding.
    – sfdcfox
    Commented Feb 3, 2021 at 15:58

You must log in to answer this question.

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