cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CPI - Forwarding Raw Image Data through Integration Flow

Hi Experts,

I have a use case where I am calling a CPI endpoint, passing in the URL of a hosted jpeg image (like this one: http://braiden.net/images/img.jpg) and then I make a GET request to that URL using a request-reply to get the raw image data into the message body.

I have successfully built this part out. Now that I have this raw image data in the message body, I would like to make a request-reply to the SAP Optical Character Recognition service (documented here).

This API requires that the request is formatted like this:

•Content-Type: multipart/form-data; boundary=CPI

•APIKey: [some-valid-api-key]

•Content-Length: [length-of-content]

  • Must contain a multipart form body, with a single field called "files" containing the image data

Because of the requirements of the request for this API, I have added a content modifier to remove all unnecessary headers (that may have been brought in from the first request) and add the required headers. (It seems that the "Content-Length" header is automatically added by CPI, and any attempts to change/delete this header do not work.)

Inside this content modifier, I also added the following in the body as an expression:

--CPI
Content-Disposition: form-data; name="files"; filename="img.jpg"
Content-Type: image/jpeg

${body}
--CPI--

This wraps the raw image data with the necessary boundary information required for the multipart/form-data content type. (This method of wrapping raw data was borrowed from this blog post.)

Here is a picture of my iflow:

Click here to download this iflow as a zip file.

One thing I have noticed is that when sending the request from Postman vs. making the request from CPI is that the value for the Content-Length header is drastically different, and as I have already mentioned, it seems there is no way to manually set this header to a different value.

I have also noticed that when viewing the raw HTTP request made by CPI, the image data is slightly different from the image data when sending it using a REST Client. There are a few characters missing every so often in the request that CPI makes.

Here are two examples of the requests, one being sent from a REST client, the other being sent from CPI. The request from the REST client works when sent to the OCR API, but the request from CPI does not (returns a 500 error).

REST Client Request

CPI Request

Again, the differences are the Content-Length header value and some missing characters from the CPI request.

Here are some questions:

1. Is using a request-reply to get the raw image data the proper way to store the image in the message body, or is there a better method?

2. How can I properly send this image to the OCR API? I have only been getting 500 errors from the OCR API server so far.

3. Should I be using a MIME Multipart encoder or script to modify encoding? If so, how?

TL;DR: I want to send an image to an optical character recognition API using CPI. Any ideas on what I could be doing wrong, and why my request is not working?

Any help would be greatly appreciated!

View Entire Topic
JuanDK78
Participant
0 Kudos

Hej Everyone,

If you are having issues in CPI when using /api/v2/image/ocr returning error: "message": "Wrong request: No file sent in the request, please set the field 'files'"

If turns out that CPI HTTP adapter is sending the payload with "Transfer-Encoding: chunked"

If I trace the message and use the created Content-Type and body by Eng Swee's code, the boundaries are correct and the payload is correct, It works from POSTMAN when Posting RAW

I have not figured out if is possibkle to disable the chuked encoding, like the SOAP adapter, or how to generated chucked content.