[ISSUE] Robotframework file that searches for GIF

Hello team,

I am trying to reproduce a request I do on Postman with a .robot file. It simply searches for a GIF.

This is what I have so far:

*** Settings ***
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com/v1/gifs/search
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger
${NUMBER_OF_GIFS}      5

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${headers}=      Create Dictionary   api_key=${API_KEY}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}   limit=${NUMBER_OF_GIFS}
    ${response}=     Get Request        giphy   /   headers=${headers}   params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Should Contain   ${json}['data']     ${NUMBER_OF_GIFS} items
    Log              ${json}['data']

But I keep getting status 404.

Could you please assist?

Edit as response because I couldn’t find the edit option.

Adding Documentation to Settings, I replace ‘your_giphy_api_key_here’ with my API key before running it.

*** Settings ***
    Documentation     A test test for searching GIFs.
    Library           RequestsLibrary

    *** Variables ***
    ${BASE_URL}       https://api.giphy.com/v1/gifs/search
    ${API_KEY}          your_giphy_api_key_here
    ${SEARCH_TERM}    cheeseburger
    ${NUMBER_OF_GIFS}      5

    *** Test Cases ***
    Search for GIFs
        [Documentation]  Search for GIFs using the GIPHY API
        Create Session   giphy   ${BASE_URL}
        ${headers}=      Create Dictionary   api_key=${API_KEY}
        ${params}=       Create Dictionary   q=${SEARCH_TERM}
        ${response}=     GET On Session        giphy   /   headers=${headers}   params=${params}
        Should Be Equal As Strings   ${response.status_code}   200
        ${json}=         Set Variable       ${response.json()}
        Log              ${json}['data']

After removing ${NUMBER_OF_GIFS} and limit from the request, I get the error

MissingSchema: Invalid URL 'api.giphy.com/v1/gifs/search/': No schema supplied. Perhaps you meant http://api.giphy.com/v1/gifs/search/?

When adding ‘?’ to the end of ${BASE_URL}, I get the following

HTTPError: 401 Client Error: Unauthorized for url: https://api.giphy.com/v1/gifs/search?/&q=cheeseburger

I am confused.

Hi Milan.

I had a quick look at the giphy search api endpoint documentation and I think your issue might be the forward slash (/) before the question mark (?)

As a first step I’ll suggest you try it like this:

*** Settings ***
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger
${NUMBER_OF_GIFS}      5

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${headers}=      Create Dictionary   api_key=${API_KEY}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}   limit=${NUMBER_OF_GIFS}
    ${response}=     Get Request        giphy   /v1/gifs/search   headers=${headers}   params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Should Contain   ${json}['data']     ${NUMBER_OF_GIFS} items
    Log              ${json}['data']

Note the changes to the ${BASE_URL} and GET On Session lines

Also from the docs It looks like you can just put the api_key in ${params} and you then wouldn’t need the ${headers} at all which might make things simpler

*** Settings ***
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger
${NUMBER_OF_GIFS}      5

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}   limit=${NUMBER_OF_GIFS}   api_key=${API_KEY}
    ${response}=     Get Request        giphy   /v1/gifs/search   params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Should Contain   ${json}['data']     ${NUMBER_OF_GIFS} items
    Log              ${json}['data']

You’ll see in the Documentation for Create Session it says

url Base url of the server

Though you might also be able to do this:

*** Settings ***
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com/v1/gifs
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger
${NUMBER_OF_GIFS}      5

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}   limit=${NUMBER_OF_GIFS}   api_key=${API_KEY}
    ${response}=     Get Request        giphy   /search   params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Should Contain   ${json}['data']     ${NUMBER_OF_GIFS} items
    Log              ${json}['data']

Hopefully this helps,

Dave.

1 Like

Dave,

Thanks to your reply I managed to come with a script that actually works

*** Settings ***
Documentation     A test suite for searching GIFs.
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}    api_key=${API_KEY}
    ${response}=     GET On Session        giphy   /v1/gifs/search    params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Log              ${json}['data']

I have one question though, why I am getting error

Dictionary ‘${json}’ has no key ‘‘data’’.

When I log json, it does have a key ‘data’

Could you please assist?

Hi Milan.

A couple of possible reasons

  • maybe it wants like this:
 Log              ${json['data']}

Or like this (Dictionary variable syntax)

 Log              ${json}[data]
  • The JSON might not have a data key in it’s root, you could try just logging the JSON first to see what the structure looks like
 Log              ${json}

Dave.

1 Like

This is the final code for the curious.

*** Settings ***
Documentation     A test suite for searching GIFs.
Library           RequestsLibrary

*** Variables ***
${BASE_URL}       https://api.giphy.com
${API_KEY}        your_giphy_api_key_here
${SEARCH_TERM}    cheeseburger

*** Test Cases ***
Search for GIFs
    [Documentation]  Search for GIFs using the GIPHY API
    Create Session   giphy   ${BASE_URL}
    ${params}=       Create Dictionary   q=${SEARCH_TERM}    api_key=${API_KEY}
    ${response}=     GET On Session        giphy   /v1/gifs/search    params=${params}
    Should Be Equal As Strings   ${response.status_code}   200
    ${json}=         Set Variable       ${response.json()}
    Log              ${json['data']}

Thanks for being a hero Dave :bowing_man:

3 Likes