Uploading files with auth

Hi!
I am learning on how to upload files to my database.

 ${UserAndPassword}    Create Dictionary    username=Name    password=Password
    ${response}    POST    url=site.com/authorize    json=${UserAndPassword}    #I want to get auth token here
    ${accessToken}  Set Variable  ${response.json()}[accessToken]
    ${headers}    Create Dictionary    accessToken=${accessToken}   #Adding auth token to headers
    ${file}=    Get File For Streaming Upload    ${CURDIR}/MyFileInZip.zip
    ${response2}    POST    url=http://192.168.25.24:9090/api/documents    data=${file}   headers=&{headers}

I got error 403 An Authentication object was not found in the SecurityContext
What am I doing wrong? (And how to make code on forum look pretty?)

Hi @Vitaly,

First start with Create Session, then use Update Session and POST On Session, It’ll make your life a lot easier.

Also if you are pushing the file as raw data like that, you probably also need to include a mime type in the header.

Does the DB’s API support raw file data or do you need to encode it (base64?)?

  • Do you need to embed the file content within a JSON request?
  • if it does it might need a document id in the header?

There should be documentation about uploading a file, it’s one of the trickier things to do with a NoSQL database, make sure you have everthing the documentation asks for, or better yet find a working example to compare with.

Dave.

2 Likes

I am not sure if you should use &{headers} (the dictionary object). Maybe you should use ${headers} (the dictionary content). Disclaimer: I did not checked documentation.

About formatting, in the message editor you have style buttons, like, Bold, Italic, … The one to mark code is </>. Control-e is also the shortcut.

I use for inline code style, text enclosed in a single ` (back tick). For blocks of code like below, you can write in the first line and the last, three back ticks, ```.

${response2} POST url=http://192.168.25.24:9090/api/documents data=${file} headers=${headers} 

Note: Dave beat me by 5 minutes :slight_smile:

3 Likes

It should be binary (according to mine requests in Postman)
Why do I need Sessions? I am just curious when I need to use them.

You don’t need sessions, they make your life easier as you can carry headers across posts rather than having to set them every time.

Also when the server sets a header like a auth token or cookie it will remember and pass it back for you like a browser does.

If you are doing a single get don’t bother with a session, if you are doing several posts with the first one being an AUTH post, then you probably want a session.

Dave.

Yes, now everything is working. The problem was that I need Bearer auth.

We prefer
:beer: :beers: :beer: :beers: :beer: :beers: :beer: :beers:
Auth :slight_smile:

3 Likes