No file getting uploaded from postman even after supplying file via firebase - postman

const upload = async (request, response, next) => {
const { files } = request;
if (!files) {
response.status(400).send({ message: "No file uploaded." });
return;
}
}
Am I missing something here?

Looks like it could be as simple as using capitalized File instead of file for the key name of the file upload in Body. If that doesn't work, try inspecting the whole request to see if your middleware is putting the files on a different key in the request object.

Related

Data file content for Postman Runner to test a POST request of type multipart/form-data

What is the data file that should be provided in Postman runner to execute multiple calls to a POST endpoint that expects multiplart/form-data?
I have an Azure function which looks like this (simplified for this post):
[FunctionName("UploadImage")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
var form = await req.ReadFormAsync();
if (!form.Files.Any()) return new BadRequestObjectResult("form.Files is empty");
var uploadedFile = form.Files.First();
form.TryGetValue("fileName", out var fileNames);
return new OkObjectResult($"Uploaded image length: {uploadedFile.Length} file name {fileNames.First()}");
}
I use Postman to send it a request successfully like this:
Postman request body params
I want to call this function 100 times concurrently to test its performance using the "Run Collection" selection of Postman. It asks to provide a csv datafile. So I tried a file that has only 2 lines like this:
FileName, Photo
Attachment1, Regina.jpg
But it does not work with that data file. I get a response message that "form.Files is empty"
How to correctly provide a data file for this Postman runner to test calling the endpoint (n) times async?

How do I make putObject request to presignedUrl using s3 AWS

I am working with AWS S3 Bucket, and trying to upload image from react native project managed by expo. I have express on the backend. I have created a s3 file on backend that handles getting the presigned url, and this works, and returns the url to the front end inside this thunk function from reduxjs toolkit. I used axios to send request to my server, this works. I have used axios and fetch to try the final put to the presigned url but when it reached the s3 bucket there is nothing in the file just an empty file with 200 bytes everytime. When I use the same presigned url from postman and upload and image in binary section then send the post request the image uploads to the bucket no problems. When I send binary or base64 to bucket from RN app it just uploads those values in text form. I attempted react-native-image-picker but was having problems with that too. Any ideas would be helpful thanks. I have included a snippet from redux slice. If you need more info let me know.
redux slice projects.js
// create a project
// fancy funtion here ......
export const createProject = createAsyncThunk(
"projects/createProject",
async (postData) => {
// sending image to s3 bucket and getting a url to store in d
const response = await axios.get("/s3")
// post image directly to s3 bucket
const s3Url = await fetch(response.data.data, {
method: "PUT",
body: postData.image
});
console.log(s3Url)
console.log(response.data.data)
// make another request to my server to store extra data
try {
const response = await axios.post('/works', postData)
return response.data.data;
} catch (err) {
console.log("Create projects failed: ", err)
}
}
)

hot to syntax postman header for hashed result

I need to hash my credentials to get access to api like this sha2(app-secret:SALT:timestamp)
I set this in the header as authtoken which should have this hashed value auth-token: api.hashify.net/hash/sha256/{{appSecret}}:{{SALT}}:{{timestamp}}
Can you please advise if the usage is correct I mean if adding thisapi.hashify.net/hash/sha256/ in front of the keys will get me what I need
it will be send just as a string it won't call to that endpoint , so instead call that endpoint in pre- request script, and store response value to a variable and use it in auth header :
// Example with a plain string URL
pm.sendRequest(`https://api.hashify.net/hash/sha256/${pm.environment.get('appSecret')}:${pm.environment.get('SALT')}:${pm.environment.get('timestamp')}`, (error, response) => {
if (error) {
console.log(error);
} else {
console.log(response.json()); // or response.text() if parse error
}
});
inside else statement write logic to get the value and use pm.environment.set("auth-token",value)
now in header auth-token:{{auth-token}}

Log all response cookies in Postman

I know I'm able to log a single response cookie using Postman, but is it possible to log ALL the response cookies in one go?
I'm trying to troubleshoot a test and would like to know what cookies are coming back, and I can't seem to get this solution to work:
const jar = pm.cookies.jar();
jar.clear(env, function (error) {
jar.getAll(env, function (error, cookies) {
console.log("cookies: " + cookies);
});
});
In the console "cookies" just remains undefined, and that's after adding the above code to the 'Tests' tab...
Thanks.
I resolved this issue by replacing the following line
const env = "integration-api.yyyy.xxx.xxxx.net";
with the full endpoint URL:
const env = "integration-api.yyyy.xxx.xxxx.net/api/v1/etc/etc";
Full code now looks like this:
const env = "integration-api.yyyy.xxx.xxxx.net/api/v1/etc/etc";
const jar = pm.cookies.jar();
jar.getAll(env, function (error, cookies) {
console.log("cookies: " + cookies);
});
Thanks to #DannyDainton for the pointers.

FileInterceptor and Body Issue in NestJS (upload a file and data in a request)

I have the following controller :
createCollection(
#UploadedFile() file,
#Body() createCollectionDto: CreateCollectionDto,
#GetUser() user: User,
): Promise<Collection> {
this.logger.verbose(
`User "${
user.username
}" creating a new collection. Data: ${JSON.stringify(
createCollectionDto,
)} of "${user.username}"`,
);
if (file) {
return this.collectionService.createCollection(
createCollectionDto,
user,
file,
);
} else {
throw new InternalServerErrorException('File needed');
}
}
I need to upload a file and In the same query give some data.
Because I want to upload a file, I set up Postman like this:
First Question : How can I send the file along with the data showed in picture n°1 ?
I searched another tool for API requests and I found Postwoman
Here is the config I used :
But the response is always the same: It doesn't detects the data. (i.e. { name: foo, color: bar} )
Second Question : How can I solve this issue ? Is it possible to put data along a file ? If it is possible how can I achieve that in NestJS ?
Thank you very much for reading my question :) Any help would be appreciated.
You're pretty close. To upload a file and extra properties in a single request in Postman, you need to choose form-data and not x-www-form-urlencoded.
And here is a simple NestJS controller that shows that you can get all of the data:
import { Body, Controller, Post, UploadedFile, UseInterceptors } from '#nestjs/common';
import { FileInterceptor } from '#nestjs/platform-express';
#Controller('upload-stack-overflow')
export class UploadStackOverflowController {
#Post('upload')
#UseInterceptors(FileInterceptor('file'))
uploadSingleFileWithPost(#UploadedFile() file, #Body() body) {
console.log(file);
console.log(body.firstName);
console.log(body.favoriteColor);
}
}