How to upload image using post request and pass it as a parameter?

Hi Akash,

What you need is POST On Session or POST

The thing that wasn’t obvious to me at first, is the last line in the documentation for both, is the reference to **kwargs being in the documentation for GET here you will find the documentation for files file-like-objects for multipart encoding upload.

I’ve never tried this myself, but hopefully this points you in the right direction.

Dave.

*** Settings ***
Library Collections
Library RequestsLibrary

Library UploadImage.py
*** Variables ***
${base_url} https://petstore.swagger.io/v2
${petId} 1
${file} Get Image
${additionalMetadata} Set Variable {“additionalMetadata”: “cat”}

*** Test Cases ***
Upload_Image
create session mysession ${base_url}
${data} Create Dictionary “file”: (${file} ,image.png, content-type=multipart/form-data boundary) ${additionalMetadata}
${Header} create dictionary Content-Type=multipart/form-data boundary accept=application/json

${response}         post on session          mysession    /pet/${petId}/uploadImage   data=${data}    headers=${Header}

log to console  ${response.status_code}

#its still not getting uploaded

Asuming that the file to upload is located in the same directory as the test suite this should work:

    Upload_Image
        create session     mysession     ${base_url}
        ${file}=    GET FILE FOR STREAMING UPLOAD    ${CURDIR}/test.png
        ${files}=    CREATE DICTIONARY    file    ${file}
        ${Header}     create dictionary     accept=application/json
        ${resp}    POST ON SESSION    mysession    pet/1/uploadImage    headers=${Header}    files=${files}