Postman is seemingly ignoring my POST in the pre-request - postman

I am trying to set up a DELETE call, but to do this I first need to create the data to delete so I am trying to call a POST in the pre-request. I have run the POST as a normal request and it works fine, but inside the pre-request it just seems to be getting ignored. I know this because I can alter the URL to something that should not work and it makes no difference and raises no errors.
This is what I have in my pre-request: -
pm.sendRequest({
url: "http://someurl/test",
method: 'POST',
header: {
'Authorization': 'Basic Tmlfefe89899eI='
},
body: {
"ClientId": 594,
"Name": null,
"Disabled": false
}, function (err, res) {
console.log(res);
}
});
Is there anything special I have to do to use a POST as a pre-request?
Any help would be greatly appreciated.

Seems strange that nothing is happening, might be returning a 400 but you're only going to see that in the Postman Console.
You can open this by hitting this icon, you'll find it in the bottom left of the app:
This is an example view from the Console, the icon next to the timing on the first request will show that a pm.sendRequest() function was used:
I would suggest just changing your request a little bit to something like the one below and it should be fine:
pm.sendRequest({
url: 'http://someurl/test',
method: 'POST',
header: {
'Authorization': 'Basic Tmlfefe89899eI=',
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify({ ClientId: 594, Name: null, Disabled: false})
}
}, function (err, res) {
console.log(res);
});

There is a trick to get request information for the pre-request script.
Save the request in a separate collocation (temporary) and export that collection. And then check that json collection using IDE or notepad, you'll get all the information there, use that as it is for your request.
Using given information in the question, here is how your pre-request script looks like,
pm.sendRequest({
url: "http://someurl/test",
method: "POST",
header: [{
"key": "Authorization",
"value": "Basic Tmlfefe89899eI=",
"type": "text",
},
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}],
body: {
mode: 'raw',
"raw": ""raw": "{\n \"ClientId\": 594,\n \"Name\": null,\n \"Disabled\": false\n}"
}
}, function(err, res) {
console.log(res);
});
Also check Postman console, you'll get all the information there, including request, response and errors if any.

Related

Getting InvalidInput : Amazon SP-API: DELETE /listings/2021-08-01/items/{sellerId}/{sku}

I am calling amazon SPI-API delete listing endpoint in Celigo integration data flow.
Here is my http method :
http Method : Delete
URI:
https://sellingpartnerapi-na.amazon.com/listings/2021-08-
01/items/ANRRIZ***sellerAccount/{{record.SKU}}?marketplaceIds=ATVPDKIKX**marketplaceId
header : {
"accept": "application/json",
"x-amz-access-token": "********",
"Host": "sellingpartnerapi-na.amazon.com",
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
"Content-Length": 298,
"X-Amz-Date": "20221017T165222Z",
"Authorization": "AWS4-HMAC-SHA256 Credential=AKIA4QUUOLH***/20221017/us-east-
1/execute-api/aws4_request, SignedHeaders=accept;content-length;content-type;host;x-amz-
access-token;x-amz-date,
Signature=fd9a396bbca14ce1fcbf52baad46b2076cd85dea1c9ca097b6***",
"accept-encoding": "gzip, deflate"
}
And I am just receiving
{
"errors": [
{
"code": "InvalidInput",
"message": "Invalid Input",
"details": ""
}
]
}
without any details.
Did anyone ran into this? Appreciate your guidance here. Thank you in Advance.
Just incase if someone is looking for an answer - It was because, Celigo was sending request body by default and there was no control from the front end to remove the request body object. Celigo said, they will release an update to remove request body when not needed. Celigo worked with me to remove the requestbody for the delete request and it worked.

How to send a POST request (registration) in Postman using pre-request script

I'm trying to create a new user using a pre-request script to be able to use a PUT request to edit user profile settings independently from other requests.
So I'm setting a token from the response to my env variable to use it in the header for the PUT request.
My whole pre-request script is not working - new user is not created and new token is not set. What am I missing?
const createUser = pm.environment.get('url') + 'users'
pm.sendRequest({
url: createUser,
method: 'POST',
header: {
'Content-type': 'application/json',
'X-Foo': 'bar'
},
body: {
mode: 'application/json',
raw: JSON.stringify({"user":{
"username":"{{$randomUserName}}",
"email":"{{$randomEmail}}",
"password": "Pa$$w0rd!"}
})
}
})
pm.sendRequest(function (err, response) {
pm.environment.set("tokenConduit", response.json().token);
});
I think this is incorrect:
body: {
mode: 'application/json',
raw: JSON.stringify({"user":{
"username":"{{$randomUserName}}",
"email":"{{$randomEmail}}",
"password": "Pa$$w0rd!"}
})
}
application/json goes into Headers like you have it, but the body is in raw format. See the example in Postman docs. You stringify a json, so it's just a bunch of charaters, mode "application/json" doesn't exist.
Another thing is you're sending 2 requests, but I think you want to send only one:
const request = {
url: createUser,
method: 'POST',
header: {
'Content-type': 'application/json',
'X-Foo': 'bar'
},
body: {
mode: 'application/json',
raw: JSON.stringify({"user":{
"username":"{{$randomUserName}}",
"email":"{{$randomEmail}}",
"password": "Pa$$w0rd!"}
})
}
};
pm.sendRequest(request, function (err, response) {
pm.environment.set("tokenConduit", response.json().token);
});
So you should have only one pm.sendRequest() in your code.
const createUser =
pm.sendRequest({
url: "https://reqres.in/api/users?page=2",
method: 'POST',
header: {
'Content-type': 'application/json',
'X-Foo': 'bar'
},
body: {
mode: 'application/json',
raw: pm.variables.replaceIn(JSON.stringify({"user":{
"username":"{{$randomUserName}}",
"email":"{{$randomEmail}}",
"password": "Pa$$w0rd!"}
}))
}
})
pm.sendRequest(function (err, response) {
pm.environment.set("tokenConduit", response.json().token);
});
you have to use pm.variables.replacein to use variables inside script section,
Goto console to see what was actually send:

The authorization on the header is not taken into account in my pre-request script

I am trying to establish a pre-request script in postman but I get error "Full authentication is required" because when I send the authorization in the header it is not being taken into account (As I see in the console).
This is strange because when I use that same header in a separate request it works fine and generates the desired token.
This is my pre-request code:
var username = pm.environment.get("username");
var password = pm.environment.get("password");
const echoPostRequest = {
url: myURL(I deleted due the security reasons),
method: 'POST',
timeout: 0,
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic xxxxxxxxxxxxx"
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "password", disabled: false},
{key: "username", value: username, disabled: false},
{key: "password", value: password, disabled: false}
]
}
}
pm.sendRequest(echoPostRequest, function (err, response) {
console.log(response.json())
//console.log('Saving the token ')
//var responseJson = response.json();
// pm.environment.set('access_token', responseJson.access_token)
});
thank you very much for helping me with this!

I have an app with react as frontend and django rest framework as backend, i use axios to send my response, but the data is empty

I have an app with react and Django rest framework. I use Django allauth for login and registration. when I want to log in, everything is ok, and the response is 201 but the data is empty and I don't get token. I send this request with the postman and I get the token. what should i do?
React Request:
axios({
method: 'post',
url: 'http://localhost:8000/rest-auth/login/',
data: {
username: 'admin',
email: '',
password: 'admin123456'
},
headers: { 'content-type': 'application/json' }
})
.then(response => {
console.log(response.data.key);
})
.catch(error => {
console.log(error);
});
the response is:
{data: "", status: 200, statusText: "OK", headers: {…}, config: {…}, …}
postman request: http://localhost:8000/rest-auth/login/
{
"username": "mahtab",
"email": "",
"password": "mahtab23"
}
postman response:
{
"key": "f75b9f54848a94ac04f455321118aff5d5a7e6f8"
}

Send Email in SharePoint Hosted App without using VS workflow

I have created a SharePoint Hosted app which uses a visual studio workflow to send email within same domain.
Since I am in process of migrating custom aspx forms in SP2010 to SharePoint Online, each form is redeveloped as a SP Hosted app and I don't want to include a workflow every time to send email.
Is there any other workaround to handle email in SP Hosted app?
Thanks!
You can use javascript REST API to send email from hosted app.
See the code below:
var urlTemplate = SPAppWebUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': [to] },
'Body': emailBody,
'Subject': subject
}
}
),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
//console.log('success')
alert('email send successfull..');
},
error: function (err) {
//console.log(JSON.stringify(err));
alert(JSON.stringify(err));
}
});