sendRequest does not seem to use Headers values I specify in the Test - postman

I'm using sendRequest function to send a 2nd request as part of the Test for my 1st request. But it seems that sendRequest does not use the Headers I specify in the function. Any ideas why and how to fix it?
Here is part of my Test, that sends 2nd request:
var runHost = pm.environment.get("MyHost");
var runToken = pm.environment.get("Token");
pm.sendRequest({
url: runHost,
method: 'PUT',
headers: {
"Authorization": "Auth "+runToken,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: {
mode: 'raw',
raw: JSON.stringify(jsonData)
}
}, (err, res) => {
console.log(res);
});
Here is what I see as an actual Request Headers sent (what I see in Console):
PUT https://some_url
Request Headers:
Content-Type:"text/plain"
User-Agent:"PostmanRuntime/7.15.2"
Accept:"*/*"
Cache-Control:"no-cache"
Postman-Token:"5e3543c-1ww0-dfc4-bert-92ba9a455667"
Host:"my_host"
Accept-Encoding:"gzip, deflate"
Content-Length:1876
Connection:"keep-alive"
I expect Request Headers to have the following attributes and values:
Authorization:"Auth current_token_value"
Accept:"application/json"
Content-Type:"application/json"
...

The key for the object that contains the Request Headers should be header, like in the example below:
pm.sendRequest({
url: runHost,
method: 'PUT',
*header*: {
"Authorization": "Auth "+runToken,
"Accept": "application/json",
"Content-Type": "application/json"
},
body: {
mode: 'raw',
raw: JSON.stringify(jsonData)
}
}, (err, res) => {
console.log(res);
});

Related

Postman pre-request script oder - does not run the requests in the order I expect

Total postman noob. I have a script (well I don't I am trying to) to do the drudge tasks of authentication and authorization which takes 2 requests:
console.log("START");
var authenticationToken;
// Identity token
var authenticationTokenRequest = {
url: 'xxxx',
method: 'POST',
timeout: 0,
header: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic xxxxx="
},
body: {
mode: 'urlencoded',
urlencoded: [
{key: "grant_type", value: "password"},
{key:"username", value: "xxxxx"},
{key:"password", value: "xxxx"},
]}
};
pm.sendRequest(authenticationTokenRequest, function (err, res) {
**console.log("01 send first request body");**
var responseJson = res.json();
console.log(responseJson);
pm.environment.set('ACCESS_TOKEN', responseJson['access_token']);
authenticationToken = responseJson['access_token'];
});
**console.log("Authentication token local var: " + authenticationToken);
console.log("Authorization token env var: " + pm.environment.get('ACCESS_TOKEN'));**
var authorizationTokenRequest = {
url: "xxxx",
method: "POST",
header: {
"Content-Type": "application/json",
"Authorization": "Bearer " + authenticationToken,
"Accept": "application/xxx+json"
},
body:{
tenantId: "xxx",
deviceId: "xxx"
}
}
pm.sendRequest(authorizationTokenRequest, function (err, res) {
**console.log("02 second request call");**
var responseJson = res.json();
pm.environment.set('ACCESS_TOKEN', responseJson['access_token']);
});
//
When I run this and look at the console, the console messages of the local vars show undefined. The console message for the second request shows in console before the first request. The second request depends on a value from the first.
What am I doing wrong? Thanks

AWS post request works on postman but not react-native

This is my JS code for the API
export const getUser = async (user) => {
//Working
const json = await fetch( "*****/username/getUser", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
user:user
}),
})
.then((res) => {
return res.json();
})
.catch((err) => {
console.log("Error in getUser: " + err);
});
return json;
};
Here is an attempt to make the request, which unfortunately return the authentication error.
fetchUser.request("kirolosM")
.then((result)=>{
console.log(result);
}).catch((err)=>{console.log("Error ",err);})
The error
{
"message": "Missing Authentication Token"
}
I have tested the API using postman and it is working as expexted.
Probably useful to compare the request sent from your json code to the one you're sending from postman. It looks like you need to include you auth token in your headers in your request.
Something like
export const getUser = async (user) => {
//Working
const json = await fetch( "*****/username/getUser", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authentication": `Bearer ${token}`
},
...

Unable to get content-type or body from Cloud Tasks HTTP request

I am creating an HTTP task that is triggering an endpoint on a Cloud Run service. The handler is being called from the queue, but I am unable to get the payload.
I have tried to log the headers, but it doesn't seem to contain content-type and I suspect that is why the app.use(bodyParser.raw({ type: "application/octet-stream" })); is failing.
These are all the request headers I'm receiving according to my Express handler:
{
"host": "my_service.a.run.app",
"x-cloudtasks-queuename": "notifications",
"x-cloudtasks-taskname": "36568403927752792701",
"x-cloudtasks-taskretrycount": "0",
"x-cloudtasks-taskexecutioncount": "0",
"x-cloudtasks-tasketa": "1640337087",
"authorization": "Bearer some_token",
"content-length": "193",
"user-agent": "Google-Cloud-Tasks",
"x-cloud-trace-context": "496573f34310f292ade89f566e7c8f40/11132544205299294705;o=1",
"traceparent": "00-496573f34310f292ade89f566e7c8f40-9a7ebdf8d332a1f1-01",
"x-forwarded-for": "35.187.132.21",
"x-forwarded-proto": "https",
"forwarded": "for=\"35.187.132.21\";proto=https",
"accept-encoding": "gzip, deflate, br"
}
This is currently what the handler looks like:
app.post("/send-notification", (req, res) => {
console.log(`req.headers: ${JSON.stringify(req.headers)}`);
console.log(`req.body: ${JSON.stringify(req.body)}`);
});
For body it prints {} but there should be a payload. I create it like this:
const task = {
httpRequest: {
httpMethod: "POST" as const,
url: def.url,
oidcToken: {
serviceAccountEmail,
},
body: Buffer.from(JSON.stringify(payload)).toString("base64"),
},
scheduleTime: {
seconds: 3 + Date.now() / 1000,
},
};
I have run out of ideas for things to try. What am I missing?
According to that example in the documentation, it's possible to add header to the request performed to Cloud Task.
You can add the content type in the header
const task = {
httpRequest: {
httpMethod: "POST" as const,
url: def.url,
headers: {
"Content-type": "application/json"
},
oidcToken: {
serviceAccountEmail,
},
body: Buffer.from(JSON.stringify(payload)).toString("base64"),
},
scheduleTime: {
seconds: 3 + Date.now() / 1000,
},
}

Postman giving 400 for pm.sendRequest

When I am running below send request from Postman - Tests, getting 400 bad request.
Could anyone tell me where I am wrong in sending the request?
let loginRequest = {
url: new_url,
method: 'POST',
headers: {
"Content-Type": "application/json",
"Authorization":autho
},
body: {
mode: 'raw',
raw:JSON.stringify({
"name":child_group,
"description":"Create Via RestAPI"
})}
};
// send request
pm.sendRequest(loginRequest, function (err, res) {
console.log(err ? err : res.text());
});
Error:
{ "message" : "Missing Authorization header."}
#Manish Katepallewar , this worked for me:
let loginRequest=
{
url:new_url,
method:'post',
header:
{
'Content-Type':'application/json',
'Authorization':autho
},
body:
{
mode:'raw',
raw:JSON.stringify(
{
'name':child_group,
'description':'Create Via RestAPI'
})
}
};
pm.sendRequest(loginRequest,function(err,res)
{
console.log(err?err:res.text());
});

Post api is not working but it works on postman in flutter

I am working on one flutter project.On which i am facing one issues on POST api.It works properly in postman but gives error in post method in flutter.
please help me I have used possible solutions.
Code:
var body = jsonEncode({
"userid": "2",
"CUSTOMERID":"158",
"OFFSET":"0"
});
final response = await http.post(
"http://18.191.223.160/webservices/FoodSubCategoryList",
headers: {
"Accept": "application/json",
"Tokenvalue": "sOzz0Y6O",
"Content-Type": "application/json"
}, body: body
);
PostMan:-
Try this
http.Response response = await http.post("http://18.191.223.160/webservices/FoodSubCategoryList",
body: { "userid": "2",
"CUSTOMERID":"158",
"OFFSET":"0"},
headers: {
"Accept": "application/json",
"Tokenvalue": "sOzz0Y6O",
"Content-Type": "application/json"
});
var responseJson = json.decode(response.body);
instead of this
var body = jsonEncode({
"userid": "2",
"CUSTOMERID":"158",
"OFFSET":"0"
});
final response = await http.post(
"http://18.191.223.160/webservices/FoodSubCategoryList",
headers: {
"Accept": "application/json",
"Tokenvalue": "sOzz0Y6O",
"Content-Type": "application/json"
}, body: body
);
I hope it helps
update dependencies http: ^0.12.1