Hello,
I am having an issue with uploading an image using Post and Form Data.
Robot Framework Code:
*** Settings ***
Resource …/…/…/resource/main.resource
*** Keywords ***
POST api/v3/image/
[Arguments] ${patient} ${bodypart}
${filepath} Set Variable ./resource/data/media/test.jpg
${image}= Evaluate {'File': ("test.jpg", open('${filepath}', 'r+b'), 'image/jpg')}
&{headers} Create Dictionary
... authorization=Token ${valid_token}
${data} Create Dictionary
... patient=${patient}
... body_part=${bodypart}
... photo=${image}
${response} POST
... headers=${headers}
... url=${image_endpoint}
... data=${data}
RETURN ${response}
Request: POST api/v3/image/ with token and the value
${response} POST api/v3/image/ 157907 55
Set Test Variable ${response}
I am trying to upload an image that is already included in my solution using POST keyword and Form-Data
The error shows that body={“photo”:[“The submitted data was not a file. Check the encoding type on the form.”]}
Why is the above code not submitting a file?
Any help is much appreciated.
Here is the Postman request:
Hi Sean,
I suspect it’s this line causing the issue:
${image}= Evaluate {'File': ("test.jpg", open('${filepath}', 'r+b'), 'image/jpg')}
I’ve never seen that approach used before, doesn’t mean it’s wrong, I don’t know for sure, but it looks like you are getting a pointer object to the file rather than the actual file data, can you show us the request body from Postman to confirm what the file part of the request body should contain?
I suspect you’ll need something like Get Binary File (because it’s an image use the Binary keyword not Get File
, but after getting the raw file, you might also need to do something like base64 encode it as usually you can’t just put raw binary data into JSON.
Unfortunately with requests like this, there is no “standard” so it really depends on how your developers implemented it, so there will not be any built-in single keyword to do this for you, you’ll need to piece it together.
That’s my guess, hope it helps,
Dave.
Thanks Dave, really appreciate the help! I did find another solution that worked:
*** Keywords ***
POST api/v3/image/
[Arguments] ${patient} ${bodypart}
${filepath} Set Variable ./resource/data/media/test.jpg
${image}= Evaluate {'photo': open('${filepath}', 'rb')}
&{headers} Create Dictionary
... authorization=Token ${valid_token}
${data} Create Dictionary
... patient=${patient}
... body_part=${bodypart}
${response} POST
... headers=${headers}
... url=${image_endpoint}
... data=${data}
... files=${image}
... expected_status=any
RETURN ${response}
Request: POST api/v3/image/ with token and the value ${value} for the field ${field}
&{data} Create Dictionary
... patient=157907
... bodypart=55
Set To Dictionary ${data} ${field} ${value}
${response} POST api/v3/image/ &{data}
Set Test Variable ${response}
1 Like