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}
1 Like

Hello @Heiko,

I have the same issue, I want to automate a post api that take in the body an uploaded file with the following format (csv, xlsx, xml) , I am getting HTTPError: 400 Client Error: Bad Request for url each time I run the robot script, bellow my code

*** Settings ***
Library   RequestsLibrary
Library   OperatingSystem
Library   Collections

*** Variables ***
${API_URL}   https://xxxxxxxxxxxxxxxxxxxxx.net
${FILE_PATH}   path-to-my-file/products.xlsx

*** Test Cases ***
Upload XLSX File Test
    [Documentation]    Upload XLSX file with parameters and verify 201 response
    ${file}=   Get File For Streaming Upload   ${FILE_PATH}
    ${files}=   Create Dictionary   file=${file}   operator_format=false   Content-Type=multipart/form-data
    ${headers}=   Create Dictionary   Authorization=xxxxxxxxxxxxxxxxxxxxxxxxxxx   Accept=application/json    Content-Type=multipart/form-data    
    Create Session   mySession   ${API_URL}   verify=false
    ${response}=   POST On Session   mySession   /api/products/imports   headers=${headers}   data=${files}
    Should Be Equal As Strings    ${response.status_code}    201

Can you please help? thanks,

Please help to automate this post api. I am also getting error 400

Hi Ann,

You should probably start your own thread with your specific issue, as it doesn’t appear to be the same as the OP

I guess you want to use RequestsLibrary to test the interface behind the form in your screenshot?

One thing that is completely different between your form and the OP’s form is the File feild (the one I guess you are struggling with?

  • OP’s
    image

  • Yours
    image

Notice theirs says (formdata) and yours says string($binary), so any solution from this thread probably won’t work for you.

Open dev tools in your browser, then submit the form, from the network tab of devtoools, find the post request that happened when you submitted the form and screen shot all the request headers (Headers sub tab) and everything on the Request sub tab, this is the information you’ll need to make requests library work.

I’m guessing that your file will be encoded as something like uuencode or base64 to make it a “binary string”, but the Request sub tab should make that clear.

The request headers will help you identify any request headers you might need to add to the RequestsLibrary call (sometimes you don’t need to add any, sometimes there are several)

Hope that helps,

Dave.

Hi Dave,

I posted as another topic, I have copied the request header, can u help to give the basic syntax the post request with file upload