Scenario:
You want to test your Rest API signup method. You need to generate multiple email ids and then call RestAPI to signup. You can use this python script and make changes as required.
>> This script uses a single email domain yahoo.com. You can change it.
>> It will generate a random number of email ids based on randrange provided.
Solution:
import random import string import requests import json #get first 22 lowercase characters letters = string.ascii_lowercase[:22] def generate_email_name(letters, length): return ''.join(random.choice(letters) for i in range(length)) def get_rand_emails(no_of_emails, length): return [generate_email_name(letters, length) + '@' + "xyz.com" for i in range(no_of_emails)] #generate and print random email. It will generate random number of email names based on randrange. The user name length will be 10. randemail = get_rand_emails(random.randrange(1,5), 10) print(randemail) for i in range(len(randemail)): print(randemail[i]) myHeaders = {"accept":"application/json","Content-Type":"application/json"} data={'username':randemail[i],'password':'fdse!434321s0'} response = requests.post('https://testsite.com/signup', data=json.dumps(data) , headers=myHeaders) print(response.request.url) print(response.request.body) print(response.request.headers) print(response.status_code) print(response.text)
NOTE: You may observe the below error if ‘data’ is not passed as json.dumps().
{“errors”:null,”warnings”:null,”info”:null,”message”:”Bad Request Format: Error parsing undefined value. Path \u0027\u0027, line 1, position 1.”,”statusCode”:400,”isSuccessStatusCode”:false}