3 weeks ago
Hello all,
I'm successfully using Postman to submit a file and payload to a SnapLogic triggered task using the Multipart Reader snap, however when I try the same using Python, I'm getting an error like this one:
{
"failure" : "Pipeline execution failed while waiting for output",
"reason" : "Snap errors: {ruuid=653b762b58a6244b73fecf7a_1388887b-924e-4356-b1e6-b05868cf03bf, reason='content-type' was not found while evaluating the sub-expression '$['content-type']', label=Binary Router, resolution=Check the spelling of the property or, if the property is optional, use the get() method (e.g. $.get('content-type')), failure=$['content-type'] is undefined. Perhaps you meant: 'Content-Type', 'Content-Length', 'Connection', 'CONTENT_TYPE'}",
"resolution" : "Fix the reported errors"
}
The Python sample was generated by Postman and didn't include any 'Content-Type' defined:
import requests
url = "https://MyURL.com?bearer_token=abc123"
payload = {'fileName': 'fileName.jpg'}
files=[
('file',('file.png',open('file.png','rb'),'image/png'))
]
headers = {}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
I added the 'content-type' in the headers like below, but it doesn't change anything, I'm getting the same error.
import requests
url = "https://MyURL?bearer_token=abc123"
payload = {'fileName': 'fileName.jpg'}
files=[
('file',('file.png',open('file.png','rb'),'image/png'))
]
headers = {
'Content-Type' : 'image/png'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
I'm using the Multipart Reader snap the default values.
Does anyone know how to resolve this issue?
Thanks in advance for any help!
Kind regards,
JF
Solved! Go to Solution.
2 weeks ago
import requests
url = "https://MyURL.com?bearer_token=abc123"
files=[
('file',('fileName.png',open('file.png','rb'),'image/png'))
]
headers = {}
response = requests.request("POST", url, headers=headers, files=files)
print(response.text)
Try this. The first argument in the file line should be the target filename you want to use. Let me know if this helps.
Regards,
Bojan
3 weeks ago
Hi @jfpelletier,
The problem is that in the Python script, you're not just sending the file as multi part form data, you're also sending a content, a payload saying {'fileName': 'fileName.jpg'}. You basically send 2 different payloads JSON and Binary (the very image itself). When the multipart reader snap collects your request, it pulls the JSON first:
And that document doesn't have a content-type attached to it, so your next snap, the binary router, is failing.
Basically, your request from Postman is not sending an additional payload to change the name of the target file, that's why it is passing successfully. Just remove the data=payload part of the request in the script and you'll get the same result in the Postman request.
I hope this helps. If not, let us know.
Regards,
Bojan
3 weeks ago
Hello @bojanvelevski,
Thanks for your reply! There is one part of your reply that I don't really understand, that is if I remove the "data=payload" part of the request, then how can I also supply a JSON payload at the same time as my file?
The payload includes the name we want to give to the returned files in response (the API takes in one file, does some manipulation, then returns another file). Other parameters will eventually be passed via this payload.
Kind regards,
JF
3 weeks ago
How do you send the name via Postman?
3 weeks ago
I send the name as text value in a form-data (and the file as a file value in the same form-data)