How I use same session or form-data in RequestsLibrary

Guy’s, I’ve got a problem with a POST in robot.
I’m sending a GET in a URL , this URL I get a Token and put it inside a variable to use the variable in my POST sending inside form-data.
I need to get a token before sending the POST using form-data.
but the response of the POST , always is error because the token.
This token needs to be kept in the same session to work.

How do I do? It’s correct the sending form-data or doesn’t is it the same session?

My code:

Create Session                          StartAPI                                ${URL_LOGINSHOW}                              verify=true
${response}                Get                      ${URL_LOGINSHOW}
${response_body}           Set Variable             ${response.content}    # Store the response body in a variable
${response_str}            Convert To String        ${response_body}  
${value}                   Get Lines Containing String            ${response_str}        dynamic-csrf-token
${csrf_token}              Get Substring            ${value}      70                     250
Log                        ${csrf_token}    # Print the extracted value
Set Global Variable        ${csrf_token} **//here I got the token**

Create Session StartAPI ${URL_SUBMITREGISTRATION} verify=true
${HEADERS}= Create Dictionary
#… Content-Type=${CONTENT_TYPE}
#… User-Agent=RobotFramework
#… Accept=/
… Authorization=Basic ${auth_data}
${payload} Create Dictionary
… dwfrm_profile_customer_firstname Newton
… dwfrm_profile_customer_lastname Newton
… dwfrm_profile_customer_cpf 096.344.033-11
… dwfrm_profile_customer_birthdate 13/04/1992
… dwfrm_profile_customer_email
… dwfrm_profile_customer_phone (81) 99291-8155
… dwfrm_profile_login_password Teste@123
… dwfrm_profile_login_passwordconfirm Teste@123
… csrf_token ${csrf_token} //here I use the token
${RESPONSE}= POST On Session StartAPI ${URL_SUBMITREGISTRATION} data={${payload}} headers=${HEADERS} expected_status=Anything
Log ${RESPONSE.json()}

Hi Newton,

Sime suggestions to try:

  • before the POST, add a Log of the ${payload} variable (just to check it’s what you expected it to be
  • it could be your formatting when you pasted in the forum, but you do have 2+ spaces between csrf_token and ${csrf_token}, etc?
  • I’ve seen the ... syntax turn each line into a list item, so i’m concerned that Create Dictionary is getting a list of lists, perhaps try changing to the = style like you did for the headers (i.e. dwfrm_profile_customer_firstname=Newton, etc)
  • I’m guessing that " //here I use the token" is not in your robot file? if it is try changing it to #here I use the token
  • I’ve not seen this data={${payload}} syntax before perhaps it should be data=${payload} ?

Hopefully one or a couple of these will help,