How to add files in a POST using request-promise of npm? - javascript

I'm tryng to send a Curl request using "request-promise" of npm. The Curl that I want to send is as follows:
`curl \
-H "Content-Type: multipart/form-data" \
-F "original=#./${parent_path}" \
-F "modified=#./${version_path}" \
-o "${out_path}" \
${URI}`
My code in node is:
BIMFile.findOne({ _id: responseDB.parent_id })
.then(parent => {
parent_path = parsePath(parent.path);
version_path = parsePath(responseDB.path);
console.log("PARENT!", parent_path, version_path);
const URI =
`${protocol}://${host_img_diff}:${port_img_diff}/diff`
out_path = version_path + '.tmp.jpg';
request.post({
url: URI,
formData: {
file: fs.createReadStream(parent_path),
file: fs.createReadStream(version_path)
}
}).then((apiResponse) => {
console.log('apiUPDATEResponse', apiResponse);
})
The result is:
Unhandled rejection StatusCodeError: 400 - "<!DOCTYPE HTML PUBLIC
\"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad
Request</title>\n<h1>Bad Request</h1>\n<p>The browser (or proxy) sent a re
quest that this server could not understand.</p>\n"
at new StatusCodeError (/backend/node_modules/request-promise-
core/lib/errors.js:32:15)
at Request.plumbing.callback (/backend/node_modules/request-promise-
core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/backend/node_modules/request-
promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/backend/node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (/backend/node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous>
(/backend/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
The server returns the following message:
xxx.xx.x.xx - - [05/Apr/2019 10:00:15] "POST /diff HTTP/1.1" 400 -
As you can see the server couldn't understand the post request. Does anyone know how to add files correctly?

Use send files using formData as you're doing, but one of the issues you have is that you're setting both files in the same property, and only the last one is set.
console.log({ file: 1, file: 2 });
So if file can receive multiple files, you need to use an array
const formData = {
file: [
fs.createReadStream(parent_path),
fs.createReadStream(version_path)
]
}
If you need additional meta-data, request module provides a way too:
Pass optional meta-data with an 'options' object with style:
{value: DATA, options: OPTIONS}
Use case: for some types of
streams, you'll need to provide "file"-related information manually.
See the form-data README for more information about options:
https://github.com/form-data/form-data

Related

Firebase Functions and Autodesk Forge integration

I have a problem with a fetch request inside Firebase Functions for the Autodesk Forge Token.
Here is the error that is showing on functions registrations:
FetchError: request to https://developer.api.autodesk.com/authentication/v1/authenticate failed,
reason: getaddrinfo EAI_AGAIN developer.api.autodesk.com:443
at ClientRequest.<anonymous> (/srv/node_modules/node-fetch/lib/index.js:1455:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at TLSSocket.socketErrorListener (_http_client.js:401:9)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:66:8)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
I have already tried inserting the Forge API inside my react js project, and figure it out it would be a CORS problems.
const snapshot = change.after;
console.log(snapshot)
const api = "https://developer.api.autodesk.com/authentication/v1/authenticate"
const search = () =>
fetch(`${api}`, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: JSON.stringify(`client_id=${process.env.REACT_APP_FORGE_CLIENT_ID}&client_secret=${process.env.REACT_APP_FORGE_CLIENT_SECRET}&grant_type=client_credentials&scope=data:read`)
}).then(res => res.json())
search().then((res) => {
const data = res
return snapshot.ref.parent.child('token').set(data);
})
})
Since Firebase Functions runs in the GCD backend CORS does not really come into play ...
You must be on the free plan - getaddrinfo EAI_AGAIN indicates a DNS lookup timeout and is due to the limitations of the free tier where outbound networking is limited to within Google services. Upgrade your plan to Flame or Blaze.

How to create an Azure AppendBlob from node.js

I have installed the npm azure-storage package.
On Azure I have created a Storage Account and a container.
I then try to create an Append Blob:
const azure = require('azure-storage');
const service = azure.createBlobService("[ACCOUNT]", "[KEY]");
service.createAppendBlobFromText("[CONTAINER]",
"some-blob-name",
"some-text",
{},
(err, result) => {
console.log('err ->',err);
console.log('result ->',result);
});
The result of calling this is:
err -> { Error
at Function.StorageServiceClient._normalizeError (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:1191:23)
at BlobService.StorageServiceClient._processResponse (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:738:50)
at Request.processResponseCallback [as _callback] (/[REMOVED]/node_modules/azure-storage/lib/common/services/storageserviceclient.js:311:37)
at Request.self.callback (/[REMOVED]/node_modules/request/request.js:186:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request.<anonymous> (/[REMOVED]/node_modules/request/request.js:1163:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage.<anonymous> (/[REMOVED]/node_modules/request/request.js:1085:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'StorageError',
message: 'Append blobs are not supported.\nRequestId:ed1777f4-601c-00cf-19a0-bb77ba000000\nTime:2018-03-14T14:25:50.8138962Z',
code: 'BlobTypeNotSupported',
statusCode: 400,
requestId: 'ed1777f4-601c-00cf-19a0-bb77ba000000' }
result -> null
I have not been able to find anything, when searching for the error.
Am I missing something here?
Please check the redundancy kind of the storage account in which you're trying to create this blob.
Blob type support varies by the storage account redundancy kind.
For example, ZRS Classic redundancy kind of storage account only supports Block Blob while Premium LRS redundancy kind of storage account only supports Page Blob.

Get a file without name in url

I would like to get the file from this url : https://donnees.roulez-eco.fr/opendata/instantane
As you can see there's no file name in the URL.
My problem is with this simple code :
var file = fs.createWriteStream('./myFile.zip')
http.get(url, function(response) {
response.pipe(file);
}
This code works well if you got an url like https://someurl.com/filename.zip.
The file is downloaded and can be use but this response.pipe(file) line throw me this error:
events.js:163
throw er; // Unhandled 'error' event
^
Error: Parse Error
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:559:20)
What should / can i do to fix this?

Instagram API: JSON.parse() cannot read property of undefined Node Js

I am using the Instagram API to search for a specific tag and then display the first result's image. Here I am using the JSON.parse() function parse the JSON that comes back from making a http get request to https://api.instagram.com/v1/tags/{tag}/media/recent?access_token={access token}. Here is what the request is returning me (- everything that doesn't matter). Note: I blanked out parts of the URL's, assume that they are correct.
{
"data":[
{
"images":{
"low_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":320,
"height":320
},
"thumbnail":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":150,
"height":150
},
"standard_resolution":{
"url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------",
"width":640,
"height":640
}
}
}
]
}
Here is the code I am using to make the request and parse the JSON:
request.get("https://api.instagram.com/v1/tags/" + tag + "/media/recent?access_token=" + accessToken, function(error, response, body)
{
console.log(JSON.parse(body).data.images.standard_resolution.url)
});
});
When ever I run this code it gives me this error:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data.images.standard_resolution.url)
^
TypeError: Cannot read property 'images' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
I thought that maybe it could have been because console.log() was running before JSON.parse() but JSON.parse blocks the code from running anything else. I also check if it had a callback or a promise that I could use but I didn't see anything.
Now I have no idea what is going on. Does anyone know what could be causing this error and how I could fix it?
EDIT:
Okay, so I found out that data is a data array, so I need to change my code to be data[0] the problem now is I get the following error when I run the code:
/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52
console.log(JSON.parse(body).data[0].images.standard_resolution.url)
^
TypeError: Cannot read property '0' of undefined
at Request._callback (/Users/pjtnt11/Documents/NodeJs/Instagram/index.js:52:67)
at Request.self.callback (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:187:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:1044:10)
at emitOne (events.js:96:13)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (/Users/pjtnt11/Documents/NodeJs/Instagram/node_modules/request/request.js:965:12)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
data is an Array, so you need to access the first element...
JSON.parse(body).data[0].images
see following working snippet...
var someObject = '{ \
"data":[ \
{ \
"images":{ \
"low_resolution":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s320x320/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":320, \
"height":320 \
}, \
"thumbnail":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s150x150/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":150, \
"height":150 \
}, \
"standard_resolution":{ \
"url":"https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/--------_--------6248_1327489376_n.jpg?ig_cache_key=--------", \
"width":640, \
"height":640 \
} \
} \
} \
] \
}';
console.log(JSON.parse(someObject).data[0].images.low_resolution.url);

New to JSON in nodejs getting some errors

getJSON('https://api.twitch.tv/kraken/streams/Jonathan_x64',
function(channel) {
if (channel["stream"] == null) {
//do something
} else {
////do something else
}
});
that is my current code but when i run it i get the following error
if (channel["stream"] == null) {
^
TypeError: Cannot read property 'stream' of undefined
at E:\my ultemet bot\index.js:10:16
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
at Request.self.callback (E:\my ultemet bot\node_modules\request\request.js:200:22)
at emitTwo (events.js:106:13)
at Request.emit (events.js:191:7)
at Request.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:1067:10)
at emitOne (events.js:101:20)
at Request.emit (events.js:188:7)
at IncomingMessage.<anonymous> (E:\my ultemet bot\node_modules\request\request.js:988:12)
at emitNone (events.js:91:20)
As far as I know there isn't a built-in top level getJSON() function in Node so you must be using a custom function.
From the stack trace you've shared:
at Request._callback (E:\my ultemet bot\node_modules\get-JSON\lib\node.js:11:5)
^^^^^^^^^^^^^^^^^^^^^
... we learn that you are using an NPM third-party module. Once there, it's trivial to find the documentation:
var getJSON = require('get-json')
getJSON('http://api.listenparadise.org', function(error, response){
error
// undefined
response.result
// ["Beth Orton — Stolen Car",
// "Jack White — Temporary Ground",
// "I Am Kloot — Loch",
// "Portishead — Glory Box"]
response.ok
// => true
})
It isn't real code but it's clear that the first callback argument is error, but you have this:
function(channel){}
Since (as the error message states) it's undefined, that means that the call is successful—it's just you aren't reading it correctly.
I've been peeking the module source code and it's actually not very impressive. It's basically a tiny wrapper for request that doesn't add much value.

Categories