You need to sign in to do that
Don't have an account?
azrkhn
apex (https request) post method callout with multiple blobs in form data
Hello,
I am calling a remote webservice api from apex.
This is how the request should look like as per the api documentation.
Below is the code that I have accomplished so far, but the problem is that this code returns a 500 - Internal Server Error when I run it.
Here is my code:
So I could not figure out what is wrong as apparently I have meet the webservice callout requirments as mentioned in the api documentation.
Any help in this regard will be appreciated.
I am calling a remote webservice api from apex.
This is how the request should look like as per the api documentation.
URL: 'https://example.com/merge' Method: 'POST' Content-Type: 'multipart/form-data' Parameters: {file_1: 'BINARY_PDF_DATA', file_2: 'BINARY_PDF_DATA', file_3: ..., user: 'auth_username', password: 'auth_password'} Result: 'BINARY_PDF_DATA'
Below is the code that I have accomplished so far, but the problem is that this code returns a 500 - Internal Server Error when I run it.
Here is my code:
string username = 'auth_username'; string password = 'auth_password'; boolean isTest = true; Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('https://example.com/merge'); req.setMethod('POST'); req.setHeader('Content-Type','multipart/form-data; boundary=----RandomString'); string body = '------RandomString\r\n'; body += 'Content-Disposition: form-data;'; body += 'name=\"user\"\r\n\r\n'+username+'\r\n'; body += '------RandomString\r\n'; body += 'Content-Disposition: form-data;'; body += 'name=\"password\"\r\n\r\n'+password+'\r\n'; body += '------RandomString\r\n'; body += 'Content-Disposition: form-data;'; body += 'name=\"test\"\r\n\r\n'+isTest+'\r\n'; // build the blob form data integer k = 1; for(blob blb : blobs){ body += '------RandomString\r\n'; body += 'Content-Disposition: form-data; name="file_'+k+'"; filename="file_'+k+'.pdf"\r\n\r\n'; body+= EncodingUtil.base64Encode(blb) +'\r\n'; k++; } body += '------RandomString--'; req.setBody(body); // Send the request, and return a response HttpResponse res = h.send(req); system.debug('Response is: '); system.debug(res);
So I could not figure out what is wrong as apparently I have meet the webservice callout requirments as mentioned in the api documentation.
Any help in this regard will be appreciated.
The related Apex controller builds the blobs, converts them to a list of base64 strings and then we use them directly in the Javascript code below.
Here is the Ajax code that I used in the Visualforce page to do the webservice call and get the binary data back.