Sending multiple files in Postman - NodeJS - json

I have a JSON request in the following structure-
{
"user_id" : "1",
"user_details" : {
"name" : "my_name"
"passport_image" : "passport_image.jpg"
},
"user_documents" : [
{"file" : "doc_1.jpg"},
{"file" : "doc_2.jpg"}
]
}
How can I send files via postman that are part of a JSON request?
This is what I tried -
But then passport_image.jpg would be its own field and not part of user_details object, correct?
And what about the array of file objects under user_documents? How can I send it too in the request?
I would appreciate help as I'm quite new to using form-data requests rather than raw JSON ones.

You can set the keys as array. It can be set like this. user_documents[0] and the respective file for it.
This will work!

The above answer is wrong, the way to do it is to select the key value = to the value you used in your code, for instance see below:
const storage = multer.diskStorage({
destination: function(req,file,cb) {
cb(null,'uploads/')
},
filename: function(res,file,cb) {
console.log('file= ',file)
const uniqueSuffix=Date.now()+'-'+Math.round(Math.random()*1e9)
cb(null, file.fieldname+'-'+uniqueSuffix+'.jpg')
}
})
const upload = multer({storage:storage})
app.post('/uploads', upload.array('files',12), (req,res)=>
{
req.files.map((file)=>{
console.log('received request: ', file)
})
res.status(200).send('received files')
})
Then in postman you just select several files, in this case less than 12 after clicking on the select files button. Just one entrance for multiple files.

Actually I find solution on Youtube. You can choose multiple files by keeping Ctrl and choose multiple files.

Related

Postman: POST request of nested JSON via form-data not working (while via raw-data ok)

I want to POST the following JSON-object via Postman:
{
"title": "test_title",
"date": "2021-12-31",
"attachments": [
{
"name": "test_attachment"
}
]
}
This works perfectly fine, when using Postman's raw input form for the request-body: I get a "201 Created"-response back.
However, when using the form-data to POST the data, I get the error "Invalid data. Expected a dictionary, but got str." (see also screenshot below) What am I doing wrong here? I tried all kind of other versions to enter the attachment-key:value pair but nothing worked so far
I managed to make it work! (note: I added some additional fields compared to the screenshot in question. See below for details:
You did nothing wrong.
If you want to make a request with json object, then you go with raw type (json) in postman.
If you want to upload file, then you use form-data
One more thing, status 201 means the request is succeed, your object has been created.
var express = require('express')
const multer = require('multer')
const upload = multer()
var app = express()
app.use(express.json());
app.post('/test',upload.none(), function (req, res, next) {
res.send(req.body)
})
app.listen(80, function () {
console.log('web server listening on port 80')
})
Above is a sample endpoint which works with both form-data and json , just do a post to http://localhost:80/test with both form data and raw json
you can see both will get parsed correclty
APIs are just abstraction , its like a function that takes in many attribute, how you parse it depends on the implementation ( how the api function is written) .
so answer is "Talk to the developer" on how the API is implemented and what it is supporting
I'm having issue in placing json into form format the way Daniel did in Postman. Need help in figuring out what is it required to place the cascaded json objects into form data format. Please see here that I'm trying to accomplish.
JSON Format (to be filled into Postman form-data section:
{
"primary_object": {
"child_object_1": [{"id": 12345678, "value": "abc"},{"id": 87654321, "value": "xyz"}],
"child_object_2": [
"first_val",
"second_val"
]
}
}

JSON is loaded with missing key/value pairs

I am using Axios to load a static JSON from the React application server, and upon reading the files, there are many key/value pairs missing.
The JSON file in question is asset-manifest.json, which includes all the assets (media, css, js) files in the app, and is generated in the build process by webpack. I use this file as reference to pre-load the images for the website.
The JSON file looks like this (ellipsis added by me, ofc):
{
"files": {
"main.js": "/static/js/main.04cbf801.chunk.js",
"main.js.map": "/static/js/main.04cbf801.chunk.js.map",
"runtime-main.js": "/static/js/runtime-main.ed6fc4d3.js",
"runtime-main.js.map": "/static/js/runtime-main.ed6fc4d3.js.map",
"static/css/2.f5a05697.chunk.css": "/static/css/2.f5a05697.chunk.css",
"static/js/2.448c4959.chunk.js": "/static/js/2.448c4959.chunk.js",
"static/js/2.448c4959.chunk.js.map": "/static/js/2.448c4959.chunk.js.map",
"static/css/3.d7d661be.chunk.css": "/static/css/3.d7d661be.chunk.css",
"static/js/3.1605bce2.chunk.js": "/static/js/3.1605bce2.chunk.js",
"static/js/3.1605bce2.chunk.js.map": "/static/js/3.1605bce2.chunk.js.map",
"index.html": "/index.html",
"precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js": "/precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js",
"service-worker.js": "/service-worker.js",
"static/css/2.f5a05697.chunk.css.map": "/static/css/2.f5a05697.chunk.css.map",
"static/css/3.d7d661be.chunk.css.map": "/static/css/3.d7d661be.chunk.css.map",
"static/media/ANKLES.6d795be0.svg": "/static/media/ANKLES.6d795be0.svg",
"static/media/ARMS.8418bd6e.svg": "/static/media/ARMS.8418bd6e.svg",
(...)
},
(...)
}
And there is nothing extraordinary in the key/value pairs that are missing. In total, these are about 15 missing entries, from a total of more than 400. For example, these are some of the key/value pairs missing:
"static/media/testa.png": "/static/media/testa.0b92a7e4.png",
"static/media/Osso01.png": "/static/media/Osso01.9949a55e.png",
"static/media/CabeloHomem01.png": "/static/media/CabeloHomem01.258b6c19.png"
This is the code I am using to load the JSON:
axios.get("asset-manifest.json")
.then(manifest => {
// entries in manifest are already missing at this point
// do stuff with manifest
})
I tried using fetch instead of Axios, for the same result.
BTW, if I copy the JSON code and parse it in the console/terminal, all entries are parsed as expected.
As a workaround, I can embed the related images in the code using base 64, but that's a last resort that I'd prefer to avoid.
Any ideas?
Thanks for reading.
is working fine with fetch first check your response in the postman or any other that you like
let payload = {
token: 'p1ztA-3ZEYrX2wJAa_juSg',
data: {
'static/media/testa.png': '/static/media/testa.0b92a7e4.png',
'static/media/Osso01.png': '/static/media/Osso01.9949a55e.png',
'static/media/CabeloHomem01.png':
'/static/media/CabeloHomem01.258b6c19.png',
'main.js': '/static/js/main.04cbf801.chunk.js',
'main.js.map': '/static/js/main.04cbf801.chunk.js.map',
'runtime-main.js': '/static/js/runtime-main.ed6fc4d3.js',
'runtime-main.js.map': '/static/js/runtime-main.ed6fc4d3.js.map',
'static/css/2.f5a05697.chunk.css': '/static/css/2.f5a05697.chunk.css',
'static/js/2.448c4959.chunk.js': '/static/js/2.448c4959.chunk.js',
'static/js/2.448c4959.chunk.js.map': '/static/js/2.448c4959.chunk.js.map',
'static/css/3.d7d661be.chunk.css': '/static/css/3.d7d661be.chunk.css',
'static/js/3.1605bce2.chunk.js': '/static/js/3.1605bce2.chunk.js',
'static/js/3.1605bce2.chunk.js.map': '/static/js/3.1605bce2.chunk.js.map',
'index.html': '/index.html',
'precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js':
'/precache-manifest.093a3239b1d438e30c3887d109b2e7b2.js',
'service-worker.js': '/service-worker.js',
'static/css/2.f5a05697.chunk.css.map':
'/static/css/2.f5a05697.chunk.css.map',
'static/css/3.d7d661be.chunk.css.map':
'/static/css/3.d7d661be.chunk.css.map',
'static/media/ANKLES.6d795be0.svg': '/static/media/ANKLES.6d795be0.svg',
'static/media/ARMS.8418bd6e.svg': '/static/media/ARMS.8418bd6e.svg',
},
};
fetch('https://app.fakejson.com/q', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then(response => response.json())
.then(json => console.log(json));

response with status 200: ok

I am trying to figure out how to plot data from a local '.JSON' file using angular2-highcharts example.
I followed the example in 'https://www.npmjs.com/package/angular2-highcharts' to first understand how to plot .JSON data and it worked. I took the data available for the example and created a local .JSON file (copied the content from 'https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK' in notepad and saved it with UTF-8 encoding as a .JSON file), and replaced the file path for the JSON request to this. When I do this though, I get an error - response with status 200.
constructor(jsonp : Jsonp) {
//jsonp.get('https://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=JSONP_CALLBACK').subscribe(res => {
jsonp.get('./data.json').subscribe(res => {
this.options = {
title : { text : 'AAPL Stock Price' },
series : [{
name : 'AAPL',
data : res.json(),
tooltip: {
valueDecimals: 2
}
}]
};
});
}
options: Object;
};
Since I am not super familiar with json data/ Javascript or angular2 I am not sure if I am missing something very basic here. Any help is appreciated.
as far as I know, Response Status 200 specifies that request was successful. i.e. your request was successfully handled. perhaps you want to try checking response data.
check your callback for response data.
Using http instead of json helped. I made use of the suggestion in this answer https://stackoverflow.com/a/36305814/4567096.

Collection+JSON with AngularJS

I'm working on a project where various tables of data will be displayed with AngularJS. The data will be in the Collection+JSON format, as shown below. I found this library https://github.com/Medycation/angular-collection-json, I'm not sure how to make it work. Below is an example of the data.
angular.module('app', ['cj']);
var $injector = angular.injector();
var cj = $injector.get('cj');
cj("cjapi1.php").then(function(cjProvider){
console.log(collection.items());
});
I tried the above. In the console it says I need to register cjProvider as a provider. Any help with how to set this up properly would be appreciated. Thanks.
{
“collection”:
{
“version”: “0.1”,
“href” : “https://example.com/companies”
“items” : [
{
“href” : “https://example.com/companies/123”,
“data” : [
{
“orgInfo”: {
{“name”: “companyName”, “value”: “Example Company 1”}
}
},
{
“href” : “https://example.com/companies/1234”,
“data” : [
{
“orgInfo”: {
{“name”: “companyName”, “value”: “Example Company 2”}
}
},
]
}
Please configure your cjProvider while configuring your module. Check the below code template for the reference to configure cjProvider.
angular.module('app', ['cj']).configure(function(cjProvider){
// Alter urls before they get requested
// cj('http://example.com/foo') requests http://example.com/foo/improved
cjProvider.setUrlTransform(function(original){
return original + '/improved';
});
// Disable strict version checking (collections without version "1.0")
cjProvider.setStrictVersion(false);
});
Please make sure that you have configured your transformUrl just shown above.
Your base url must be configured in cjProvider and while hitting any url ang getting data you should transform your request like here you are requesting cjapi1.php. so your baseurl must be append before that like your_base_url + 'cjapi1.php' this will be done for all requesting api. So cjProvider will take care that and will return api path and in .then(responce) you will get your responce which is collection.
cj("cjapi1.php").then(function(collection){
console.log(collection.items());
});
Are you trying to configure or get the contents of the collection from php call?
Looks like a typo to me but try this to get collection:
cj("cjapi1.php").then(function(collection){
console.log(collection.items());
});
...and this for configuration of your provider:
angular.module('agile', ['cj']).configure(function(cjProvider){
// Alter urls before they get requested
// cj('http://example.com/foo') requests http://example.com/foo/improved
cjProvider.setUrlTransform(function(original){
return original + '/improved';
});
// Disable strict version checking (collections without version "1.0")
cjProvider.setStrictVersion(false);
});

working with JSON data, trying to parse nested data

I just working with JSON data and am playing around with jQuery and Ajax requests. Pretty basic stuff, but here's my problem.
I have a basic data set which I was using for time tracking. I know how to parse the simple JSON data like this:
{
"end" : "1/18/2011",
"start" : "1/18/2011",
"task" : "Code Review",
},
It's the more complicated stuff I'm trying to parse like this where I'm trying to pull the "time" data out.
{
"end" : "1/17/2011",
"start" : "1/17/2011",
"task" : "Exclusive Brands",
"time" : {
"analysis" : 4,
"documentation" : 3,
"meetings" : 2
}
This is the code for the script I've been using to parse the simple data:
$(function() {
$('.load').click(function(){
$.getJSON("data.js",function(data){
$.each(data.timesheet, function(i,data){
var div_data ="<div class='box'>"+data.start+" "+data.task+"</div>";
$(div_data).appendTo("#time-tracking");
});
}
);
return false;
});
});
My question is what's the format to parse the time data, or what's the best way to parse the information nested inside the time element?
Any help will be greatly appreciated.
A JSON string will be parsed into an object. When parsed, the time is the key of one object. You could retrieve the value of this object through the dot operator (.).
data = JSON.parse('{"end":"1/17/2011", "start":"1/17/2011", "task":"Exclusive Brands", "time": {"analysis":4, "documentation":3, "meetings":2 } }')
// => obj
data.time.analysis
// => 4
In your case similarly you could use the data.time.meetings to access your data from remote server.
Unless I am terribly mistaken, since jquery already converted data into a javascript for you, you should be able to access time as if it was a javascript object like so:
var analysis = data.time.analysis;
var documentation = data.time.documentation;
var meetings = data.time.meetings;
etc...