0

I am calling a rest api with multipart/form-data content type. I need to pass a pdf and additional parameters to generate a pdf. The appends the additional params data to the pdf and generate a new pdf. The additional param is an object like structure which I am able to generate using apex

QrInvoice= {  
"creditorInformation":  
    {  
    "iban": "CH3908704016075473007",  
    "creditor":  
        { 
            "addressType": "STRUCTURED",  
            "name": "Robert Schneider AG",  
            "streetName": "Rue du Lac",  
            "houseNumber": "1268/2/22", "postalCode": "2501",  
            "city": "Biel",  
            "addressLine1": "Rue du Lac 1268/2/22",  
            "addressLine2": "2501 Biel",  
            "country": "CH"  
        }  
    },  
"ultimateCreditor": {  
     "addressType": "STRUCTURED",  
     "name": "Robert Schneider Services Switzerland AG",  
     "streetName": "Rue du Lac",  
     "houseNumber": "1268/3/1",  
     "postalCode": "2501",  
     "city": "Biel",  
     "addressLine1": "Rue du Lac 1268/3/1",   
     "addressLine2": "2501 Biel",  
     "country": "CH"  
 },  
"paymentAmountInformation": {  
    "amount": 199.95,  
    "currency": "CHF"  
},  
"ultimateDebtor": {   
    "addressType": "STRUCTURED",   
    "name": "Pia-Maria Rutschmann-Schnyder",  
    "streetName": "Grosse Marktgasse",  
    "houseNumber": "28",  
    "postalCode": "9400",  
    "city": "Rorschach",  
    "addressLine1": "Rue du Lac 1268/3/1",   
    "addressLine2": "2501 Biel",  
    "country": "CH"  
},  
"paymentReference": {   
    "referenceType": "SCOR",   
    "reference": "RF18539007547034",  
    "additionalInformation": {  
    "unstructuredMessage": "Bill No. 3139 for garden work and disposal of cuttings",   
    "billInformation": "//S1/10/10201409/11/190512/20/1400.000-53/30/106017086/31/180508/32/7.7/40/2:10;0:30" }  
},  
"alternativeSchemes": {  
    "alternativeSchemeParameters": [  
    "Name AV1: UV;UltraPay005;12345"  
    ]  
}  
}

I am still getting bad request 400 error. I believe there is something wrong while passing params. Appreciate your help. Thanks Here is my code

String file_name = 'testing.pdf';
       String boundary = '----------------------------741e90d31eff';
        String header = '--'+boundary+'nContent-Disposition: form-data; name="file"; QrInvoice='+addParam+';nContent-Type: application/octet-stream';

      String footer = '--'+boundary+'--';             
      String headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'rnrn'));
        
        while(headerEncoded.endsWith('='))
        {
            header+=' ';
            headerEncoded = EncodingUtil.base64Encode(Blob.valueOf(header+'nn'));
        }
        //base64 encoded body
        String bodyEncoded = EncodingUtil.base64Encode(retBlob);
        //base64 encoded footer
        String footerEncoded = EncodingUtil.base64Encode(Blob.valueOf(footer));
        
        Blob bodyBlob = null;
        //last encoded body bytes
        String last4Bytes = bodyEncoded.substring(bodyEncoded.length()-4,bodyEncoded.length());
        if(last4Bytes.endsWith('='))
        {
            Blob decoded4Bytes = EncodingUtil.base64Decode(last4Bytes);
            HttpRequest tmp = new HttpRequest();
            tmp.setBodyAsBlob(decoded4Bytes);
            String last4BytesFooter = tmp.getBody()+footer;   
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded.substring(0,bodyEncoded.length()-4)+EncodingUtil.base64Encode(Blob.valueOf(last4BytesFooter)));
        }
        else
        {
            bodyBlob = EncodingUtil.base64Decode(headerEncoded+bodyEncoded+footerEncoded);
        }
        
        if(bodyBlob.size()>3000000)
        {
            system.debug('--Excpetion--');
        }
  HttpRequest req1 = new HttpRequest();
  req1.setHeader('Content-Type','multipart/form-data; boundary='+boundary);
  req1.setMethod('POST');
  req1.setEndpoint('https://rest.qr-invoice.cloud/v2/payment-part-receipt/append?fontFamily=LIBERATION_SANS&fontsEmbedded=true&boundaryLines=true&boundaryLineScissors=true&api_key=<API KEY>');  
  req1.setBodyAsBlob(bodyBlob);
  req1.setTimeout(60000);
  req1.setHeader('Content-Length',String.valueof(req.getBodyAsBlob().size()));
  Http http = new Http();
  HTTPResponse res1 = http.send(req1);

Demo Curl request boy which is working

-X POST "https://rest.qr-invoice.cloud/v2/payment-part-receipt/append?fontFamily=LIBERATION_SANS&fontsEmbedded=true&boundaryLines=true&boundaryLineScissors=true&api_key=" -H "accept: application/pdf" -H "Accept-Language: de" -H "Content-Type: multipart/form-data" -F "QrInvoice={ "creditorInformation": { "iban": "CH3908704016075473007", "creditor": { "addressType": "STRUCTURED", "name": "Robert Schneider AG", "streetName": "Rue du Lac", "houseNumber": "1268/2/22", "postalCode": "2501", "city": "Biel", "addressLine1": "Rue du Lac 1268/2/22", "addressLine2": "2501 Biel", "country": "CH" } }, "paymentAmountInformation": { "amount": 199.95, "currency": "CHF" }, "ultimateDebtor": { "addressType": "STRUCTURED", "name": "Pia-Maria Rutschmann-Schnyder", "streetName": "Grosse Marktgasse", "houseNumber": "28", "postalCode": "9400", "city": "Rorschach", "addressLine1": "Rue du Lac 1268/3/1", "addressLine2": "2501 Biel", "country": "CH" }, "paymentReference": { "referenceType": "SCOR", "reference": "RF18539007547034", "additionalInformation": { "unstructuredMessage": "Bill No. 3139 for garden work and disposal of cuttings", "billInformation": "//S1/10/10201409/11/190512/20/1400.000-53/30/106017086/31/180508/32/7.7/40/2:10;0:30" } }, "alternativeSchemes": { "alternativeSchemeParameters": [ "Name AV1: UV;UltraPay005;12345" ] } }" -F "pdf=@Rechnung - _Alpenstrasse 61 3052 Zollikofen_5.5 - ZWG - 1 OG.pdf;type=application/pdf"```

2
  • please reset your API key since it is public now.
    – kurunve
    Nov 22 '21 at 12:28
  • this is demo key. Thanks by the way
    – Usman
    Nov 23 '21 at 10:18
0

The main problem is that you're using the wrong characters in your line breaks. rnrn should be \r\n\r\n and nn should be \n\n.

Since you're not actually posting binary data, you could skip all the base64 encoding and write the string directly, but that's not causing the 400 error, as far as I can tell.

1
  • I tried backslashed to r and n but no luck. The main question am I passing it correctly?
    – Usman
    Nov 23 '21 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.