webMethods

webMethods

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
Expand all | Collapse all

Post the CSV file to API

  • 1.  Post the CSV file to API

    Posted Wed March 20, 2024 05:00 PM

    [

    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,


    #Integration-Server-and-ESB
    #webMethods


  • 2.  RE: Post the CSV file to API

    Posted Thu March 21, 2024 12:09 PM

    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


    #webMethods
    #Integration-Server-and-ESB


  • 3.  RE: Post the CSV file to API

    Posted Fri March 22, 2024 05:54 PM

    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.?


    #Integration-Server-and-ESB
    #webMethods


  • 4.  RE: Post the CSV file to API

    Posted Sat March 23, 2024 01:00 AM

    Try sending with content type as
    application/octet-stream


    #webMethods
    #Integration-Server-and-ESB


  • 5.  RE: Post the CSV file to API

    Posted Sat March 23, 2024 04:29 PM

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


    #webMethods
    #Integration-Server-and-ESB


  • 6.  RE: Post the CSV file to API

    Posted Sun March 24, 2024 07:53 AM

    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”


    #webMethods
    #Integration-Server-and-ESB


  • 7.  RE: Post the CSV file to API

    Posted Mon March 25, 2024 04:56 PM

    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


    #Integration-Server-and-ESB
    #webMethods


  • 8.  RE: Post the CSV file to API

    Posted Tue March 26, 2024 10:43 AM

    any specific reason why we need to use " MIME services" other than security ?


    #webMethods
    #Integration-Server-and-ESB


  • 9.  RE: Post the CSV file to API

    Posted Tue March 26, 2024 02:31 PM

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


    #Integration-Server-and-ESB
    #webMethods


  • 10.  RE: Post the CSV file to API

    Posted Wed March 27, 2024 06:31 PM

    Thank you all the for the support.


    #webMethods
    #Integration-Server-and-ESB