cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI : Attachment with content Type multipart/form-data

navepras
Explorer

Hi

I have a requirement where I need to read the attachment from SF and post to another system which accepts multipart content-type,

can anyone help me on how to design the I-Flow when I get the attachment as Base64 format from SF and I need to send the File with Content type of multipart/form-data.

PFA which I posted through Postman, the same I need to replicate in CPI

file.png

Regards

Naveen

Accepted Solutions (0)

Answers (2)

Answers (2)

Hi navepras,

Please find below the blog with more details:

https://blogs.sap.com/2018/02/09/the-marriage-between-form-data-and-sap-cloud-platform-integration/

If this doesnt work you could directly use the script by engswee.yeoh as mentioned in below answer.

https://answers.sap.com/questions/12716088/sap-cpi---forwarding-raw-image-data-through-integr.html

I am pasting the code mentioned in the answer post.

import com.sap.gateway.ip.core.customdev.util.Message

import javax.activation.DataHandler
import javax.mail.internet.ContentType
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMultipart
import javax.mail.util.ByteArrayDataSource

Message processData(Message message) {
    byte[] bytes = message.getBody(byte[])
    //  Construct Multipart
    MimeBodyPart bodyPart = new MimeBodyPart()
    ByteArrayDataSource dataSource = new ByteArrayDataSource(bytes, 'image/jpeg')
    DataHandler byteDataHandler = new DataHandler(dataSource)
    bodyPart.setDataHandler(byteDataHandler)
    bodyPart.setFileName('img.jpg')
    bodyPart.setDisposition('form-data; name="files"')

    MimeMultipart multipart = new MimeMultipart()
    multipart.addBodyPart(bodyPart)

    // Set multipart into body
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
    multipart.writeTo(outputStream)
    message.setBody(outputStream)

    // Set Content type with boundary
    String boundary = (new ContentType(multipart.contentType)).getParameter('boundary');
    message.setHeader('Content-Type', "multipart/form-data; boundary=${boundary}")

    return message
}

you might need to modify as per your requirement.

Hope this helps !

thanks and regards,

Praveen T

navepras
Explorer
0 Kudos

Hi Praveen

Thanks for your help !!

I tried in that script method and i am getting 400 error when i post,

I have added the the encoded format manually and tried to give all the Headers and name in script,but it is not working ..

can you help me in identifying where i made mistake to post the attachment , there are some mandatory inputs like caseid, descriotion, filetype, candidatefile name which i have written in script.

I have attached the Message body and trace as well

body.jpg

response.jpg

groovy1.jpg

groovy2.jpg

trace.jpg

Regards

Naveen V