How to use bearer authorization for robot framework requestlibrary

Viewed 1548

I m trying to use bearer authorization using requestlibary of robot framework. Note: ${tokenval} getting value from another keyword. ${Body} value declared as a variable.

*** Test Cases ***
    Createnew
    ${tokenval}=     Get Token Client Credentials
    ${token1}=       Catenate  bearer  ${SPACE}  ${tokenval}
    ${headers}       content-type=application/json   Authorization=${token1}
    Create Session   apitest    https://testxxapi.com    ${headers}
    ${response}=     POST On Session   apitest   /deals/new   ${Body}
    ${resDict}=      convert to dictionary   ${response.json()}
    log              ${resDict}

But I m getting HTTPError: 403 Client Error. Same request with bearer authorization working in postman.

2 Answers

There's one issue with your code, and one potential you'd better change for the piece of mind.

The Catenate keyword combines its arguments in a single string using (by default) "space" as delimiter. Thus after this call:

${token1}=       Catenate  bearer  ${SPACE}  ${tokenval}

, the value you get is bearer the_token - 3 spaces b/n the two words. Drop the second argument, or just use

${token1}=       Set Variable    Bearer ${tokenval}

The other, potential issue is the casing of "Bearer" - according to the specs it should be treated case insensitive, but some Oauth 2.0 providers had bugs years ago where they expected it to be with a capital letter.

*** Settings ***
Documentation     API Testing in Robot Framework
Library           RequestsLibrary
Library           Collections
Library           OperatingSystem

*** Variables ***
${dev_b_token}    Bearer eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICI3R
${dev_b_server}    https://test-api-dev-b.apps.robot-b.osd.corp
${params}         kod=BI1K

*** Test Cases ***
Do a GET Request with Bearer token
&{headers}    Create Dictionary    Authorization=${dev_b_token}
Create Session    dev_b_session    ${dev_b_server}
Log    ${headers}
${response}=    GET On session    dev_b_session    /api/v1/test/info    headers=${headers}    params=${params}
Log    ${response}
Status Should Be    200    ${response}    #Check Status as 200
Related