10-04-2019 03:39 AM
hi
we have a requirement to receive a file in snaplogic where we have to provide API URL to an external team. that team will do REST post along with file attached in POST request. we are supposed receive the file and store it. we tried a number of ways but it’s not working.
we sent a text file through Postman on API POST request as an attachment, but that pipeline failed with error msg as below.
“Document input views only accept JSON-encoded data”
let us know if there is a way to consume a file from such REST POST request with files attached to it
10-04-2019 07:48 AM
vaidyarm,
You can do this with a triggered task, but need to make sure there is only a binary view open, otherwise it will fail with the error:
Document input views only accept JSON-encoded data
SnapLogic has two types of input views: document and binary. The document input views are used for structured or semi-structured data like Json, XML, CSV, etc. Document input views are represented by a Snap with an open circle like this.
For your use case, the request will be multipart-form-data and so the pipeline should have a binary input view. Specifically you should use a Multipart Reader that will be able to extract the binary data from the POST request. The binary input view is an open diamond.
Here’s an example of a Pipeline that takes the POSTed file and stores it in SLFS using the Multipart Reader and the File Writer. You can try it out by importing this pipeline, creating a triggered task, and then using Postman/curl to send a file to the task endpoint. You should see the file you sent written to a file named ‘attachment.txt’ in the same project as the pipeline.
post-file-example_2019_10_04.slp (4.3 KB)
10-07-2019 04:36 AM
that really helped !!! i am able to transfer all kind of posted files now wherever I need to send it.
but there is one thing that i have to pass in API requested along with the attachment, the name of the file in the query field. then i map this pipeline parameter to the filename in file writer
is there a way where i can simply provide attachment and auth details to the REST POST API and pipeline should extract the name from it in form of some variable which we can map as the file name in file writer snap ?
10-07-2019 03:40 PM
Not really at the moment – although there is now an enhancement ticket to improve the Multipart Read so that it could do things like this. I’ll try and keep you updated about when it goes into development.
For now, another option would be to write the file to a “temporary” location and then have a separate pipeline that moves the pipeline to the final location. Then the API would require two POST calls, one to upload the file and the other that would take the file path and other things needed to write it to the final location that would “commit” the file.
10-10-2019 02:20 AM
let us know such updates.
however, as you said we can write file to temp location like sldb, even for that we would require some parameter name to map as the filename.
Ex: the pipeline you shared is writing file as attachment.txt, not the name of the actual sent attachment, how this can be handled if the name of file need to be the same in post request?
Also, I tried with making task as ultra and pass parameter, ultra is not capturing the parameter in triggers