I’m trying to send a delete request for an article, but firstly it is needed in pre-requests to register a new user (1) and then create the article (2).
I need to use a token obtained in registration to create an aritcle, but in this pre-request incorrect token is used (which was previously stored).
So, I’d like to ask, how to use correct token in the 2nd pre-request that is obtained from 1st pre-request.
Many thanks.
(1) - is written in collection folder in Pre-Req Scripts as variable:
postman.setEnvironmentVariable('RegPreReq', () => {
const url = "https://conduit.mate.academy/api/users";
const username = pm.variables.replaceIn("{{$randomWord}}_{{$randomWord}}00");
const email = pm.variables.replaceIn("{{$randomWord}}_{{$randomWord}}[email protected]");
const registrationRequest = {
url: url,
method: 'POST',
header: {
'Content-Type': 'application/json',
},
body: {
mode: 'raw',
raw: JSON.stringify({
"user": {
"username": username,
"email": email,
"password": "!Conduit#"
}
})
}
};
pm.sendRequest(registrationRequest, (error,response) => {
console.log(error ? error : response.json());
pm.environment.set("TOKEN_NEW", response.json().user.token);
pm.environment.set("LOGIN_EMAIL", response.json().user.email);
});
(2) is written as variable in the request folder in Pre-Req Scripts
postman.setEnvironmentVariable('ArticlePreReq', () => {
const randomWords = pm.variables.replaceIn("{{$randomWord}} {{$randomWord}} {{$randomWord}}");
const CreateArticleRequest = {
url: 'https://conduit.mate.academy/api/articles',
method: 'POST',
header: {
'Content-Type': 'application/json',
'Authorization': 'Token '+ environment.TOKEN_NEW,
},
body: {
mode: 'raw',
raw: JSON.stringify({
"article": {
"title": randomWords,
"description": randomWords,
"body": randomWords,
"tagList": [
"Test_tag"
]
}
})
}
};
pm.sendRequest(CreateArticleRequest, (error,response) => {
console.log(error ? error : response.json());
pm.environment.set("SLUG", response.json().article.slug);
});
});
And in the end i make eval in the pre-req of DELETE request:
eval(environment.RegPreReq)();
eval(environment.ArticlePreReq)();