Post the CSV file to API

[

I need to send the CSV file as in the attached image approach. I used the below steps to achieve this,
createMimeData (empty MimeData)
addMimeHeader (Content-Type=multipart/form-data, Accept = application/json, Authorization=Bearer token)
getString (used to get the file data in string format)
stringToStream (used to convert the string to inputStream format)
addBodyPart (map the MimeData, inputStream to content, isEnvStream is yes, and mimeHeader, create the ImportRequest (JSON format), content-type = application/json).
getEnvStream (used to get the envSteam)
http (URL is API URL, method is post, in the data map the envStream to mimestream and bearer key to auth token)

it returns 400 bad request,

I verified to add streamToString and it return the data as,
Date: Wed, 20 Mar 2024 15:57:06 -0500
Message-ID:
MIME-Version: 1.0
Content-Type: multipart/form-data
Accept: application/json
Authorization: Bearer 1221313122

------=_Part_32_1212321–
Date: Wed, 20 Mar 2024 15:57:06 -0500 (CDT)
Message-ID:
MIME-Version: 1.0
content-type: text/plain
content-transfer-encoding: 7bit
SAP Account ID, Company Name, Street Address, Street Address 2, City, State, Zip, Country, Industry, Owner: SAP Account ID, Company Name
1, Test Company
------=_Part_32_1212321–

Can you please help with this?
Thanks,

try with application/octet-stream while sending request in postman

or

(without postman)
create a client service in webMethods IS, get your excel file from webMethods server location as a string and do a post to target http or https API url. …On target API service use input as contentStream, convert stream to string and proceed further.

client service
image

I tried the same approach as below,
API_Call
getFile, read the csv file and get it string
stringToStream, map body string and get the stream format
map, create the args document and maps stream to stream and importRequest string is created and append the JSON string.
HTTP call, the API.

The request was created as like in attached. but the file is not attached in the body.

The response should be created like in files; the file should be attached.

Can you please suggest on this.?

Try sending with content type as
application/octet-stream

I tried, but the file content is not created as an attachment in the API instead it creates RAW content.

Can you also ensure the content is ‘encoded’ before sending it in http call?

If you could pass the file name, you can also set another http header but first try by encoding it (base64binary).

Content-Disposition: attachment; filename=“test.txt”

Using the MIME services is the right path.

Just make sure you understand how to add the parts and use the right headers for each. You can review the content of the Postman log to see how Postman structures the entire HTTP request. Your form will have 2 body parts, one for the “importRequest” (JSON) and “files” (the CSV, but you’ll need to know what content type the server expects/supports).

createMimeData - specify a subType value of form-data; this will cause the top-level HTTP header to be what it needs to be.

Add the importRequest JSON as the first field

documentToJSONString
stringToStream
addBodyPart

  • mimeData as created by createMimeData; content set to the stream of the JSON;
  • isEnvStream set to no
  • mimeHeader - add Content-Disposition field with value ‘form-data; name=“importRequest”’
  • contenttype - set to application/json

Add the CSV content as the second field

getFile - set loadAs to stream; there is no need to read the file as a string and then wrap that with a stream – just load the file as a stream right away (which will not load it all into memory)
addBodyPart

  • mimeData as created by createMimeData above
  • content set to the stream of the file
  • isEnvStream set to no
  • mimeHeader - add Content-Disposition field to ‘form-data; name=“files” filename=“somefilename.csv”’; you can easily do something to set the filename dynamically, perhaps matching the actual filename
  • contenttype set to ‘application/octet-stream; name=“somefilename.csv”’; name must match same name as above; again, you can easily do something to set the filename dynamically, perhaps matching the actual filename; setting the filename twice is redundant but one is for the form, the other for the body part content; the server being posted to may need something other than application/octet-stream, such as text/csv so set as needed.
  • encoding set to binary; you can use other encodings if desired; which you use depends upon the target server and what it supports

getEnvelopeStream - mimeData as created by createMimeData above; createMultipart set to yes

http - set data/mimeStream to the stream output by getEnvelopeStream

Because that is how form-data is managed. It has nothing to do with security.

Thank you all the for the support.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.