What are the required headers and body to upload a new item? - autodesk-data-management

I'm calling the /data/v1/projects/:ProjectId:/storage endpoint to create a new storage entry for Data Management API, but I'm getting invalid/missing parameters.
What's the required header & body for this call to work?

Call endpoint /data/v1/projects/:ProjectId:/storage with
Header:
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json'
Body:
{
data: {
type: 'object',
attributes: {
name: theFileNameHere
},
relationships: {
target: {
data: {
type: 'folders',
id: folderIdHere
}
}
}
}
}
More details here

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

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());
});

Postman. Getting "JSONError: No data" after sending Cognito token pre-request

let tokenUrl = 'https://my.url/oauth2/token';
let scope = 'pets/read pets/updage petId/read'
let getTokenRequest = {
method: 'POST',
url: tokenUrl,
header: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic Base64Encode(client_id:client_secret)'}, // encoded manually beforehand
body: {
mode: 'formdata',
formdata: [
{ key: 'grant_type', value: 'client_credentials' },
{ key: 'scope', value: scope }
]
}
};
pm.sendRequest(getTokenRequest, (err, response) => {
let jsonResponse = response.json(),
newAccessToken = jsonResponse.access_token;
pm.environment.set('access_token', newAccessToken);
pm.variables.set('access_token', newAccessToken);
});
Geeks, help, please!
I have API with Cognito authorization (Client Credentials type). It work's fine in Postman with manually 'Request new Access token'. But I want to retrieve token with pre-request script. I relied on AWS documentation about token endpoint. I have
JSONError: No data, empty input at 1:1
in the console. Do you have any suggestions?
I've encountered the same problem and resolved it by setting grant_type and scope as query string in the url:
let tokenUrl = pm.variables.get("cognito-url")+ "/oauth2/token?grant_type=client_credentials&scope=' + pm.variables.get("cognito-scope");
let auth_code = btoa(pm.variables.get("cognito-client-id") + ":" + pm.variables.get("cognito-client-secret"))
let getTokenRequest = {
method: 'POST',
url: tokenUrl,
header: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + auth_code}
};
pm.sendRequest(getTokenRequest, (err, response) => {
let jsonResponse = response.json(),
newAccessToken = jsonResponse.access_token;
pm.environment.set('access_token', newAccessToken);
pm.variables.set('access_token', newAccessToken);
});

Cross origin resource sharing in AWS lambda proxy integration

Been browsing through SO for the past hours to find a fix for my issue, but no progress yet, I'm getting
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Now typically, this could be fixed with adding appropriate headers to the code, and it would work, however it's not the case for me, since I've tried to configure cors through API Gateway on AWS.
Screenshot:
Some research on google mentioned, that if the function is using lambda proxy integration, we would have to modify the lambda itself, and add the headers by our own, e.g
headers: {
'Access-Control-Allow-Origin': '*',
},
However this doesn't make much difference, is there anything I'm missing?
My actual code for the lambda (forgot to add):
const rp = require('request-promise')
const sendEmail = require('./sendEmail')
module.exports.run = async (event, context, callback) => {
const body = JSON.parse(event.body)
const { name, email, budget, message, attachment } = body
if (!name) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Name is required' }),
})
}
if (!email) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Email address is required' }),
})
}
if (!message) {
return callback(null, {
statusCode: 400,
body: JSON.stringify({ message: 'Message is required' }),
})
}
return Promise.all([
sendEmail({
to: 'Example <user#example.com>',
subject: 'New enquiry received!',
data:
`Name: ${name}\n` +
`Email: ${email}\n` +
`Budget: ${budget || 'n/a'}\n` +
`Attachment: ${attachment || 'n/a'}\n` +
`\n${message}`,
}),
sendEmail({
to: `${name} <${email}>`,
subject: 'Your message was delivered at ',
data:
'Thanks for reaching out!\n' +
'Somebody at our office will get back to you as soon as possible.\n' +
'\n' +
'While you wait, check out our Handbook (/) and get acquainted with how we do things around here.\n' +
'We have a lot of content there so feel free to explore as you please.\n' +
'\n' +
'Speak soon,\n' +
'\n',
}),
rp({
method: 'POST',
uri: `https://hooks.slack.com/services/${process.env.SLACK_PATH}`,
json: true,
body: {
text: `<!channel> New enquiry received`,
attachments: [
{
fallback: 'Information:',
pretext: 'Information:',
color: '#FF5050',
fields: [
{ title: 'Name', value: name, short: false },
{ title: 'Email', value: email, short: false },
{ title: 'Budget', value: budget || 'n/a', short: false },
{ title: 'Attachment', value: attachment || 'n/a', short: false },
{ title: 'Message', value: message || 'n/a', short: false },
],
},
],
},
}),
])
.then(() => {
return callback(null, {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify({ message: 'Great success' }),
})
})
.catch(err => {
return callback(null, {
statusCode: 500,
body: JSON.stringify({
message: 'Oh no :( Message not delivered',
error: err
}),
})
})
}
No 'Access-Control-Allow-Origin' header is present on the requested resource.
This is saying that the resource you requested, your Lambda via API Gateway, is not returning an Access-Control-Allow-Origin header in its response; the browser is expecting the CORS headers in the response from the API (possibly because of an OPTIONS request), but the response doesn’t have them.
To solve your issue, add a Access-Control-Allow-Origin: * header to the response your Lambda returns. Using the first item you're returning:
if (!name) {
return callback(null, {
statusCode: 400,
headers: {
'Access-Control-Allow-Origin': '*',
// any other required headers
},
body: JSON.stringify({ message: 'Name is required' }),
})
}
Worth noting that you'll have to add those headers to every response.

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

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);
});