-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
62 lines (56 loc) · 1.5 KB
/
test.js
File metadata and controls
62 lines (56 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const fetch = require('node-fetch')
let fs = require("fs")
const stats = fs.statSync("./download.jpg")
const fileSizeInBytes = stats.size
// You can pass any of the 3 objects below as body
let readStream = fs.createReadStream("./download.jpg")
//var stringContent = fs.readFileSync('foo.txt', 'utf8');
//var bufferContent = fs.readFileSync('foo.txt');
fetch("http://localhost:5001/soiling-sensor/us-east4/imageTest", {
method: "POST",
headers: {
"Content-length": fileSizeInBytes,
},
body: readStream, // Here, stringContent or bufferContent would also work
})
.then(function (res) {
return res.json()
})
.then(function (json) {
console.log(json)
})
const logResponse = (res, url) => {
console.log("Request was made to " + url + ":")
if (res.status < 200 || res.status > 299) {
console.error(res)
} else {
console.log(res)
}
}
const sendPostRequest = async (url, data) => {
let toReturn = {}
try {
const config = {
headers: {
"Content-Type": "image/jpeg",
},
method: "POST",
body: JSON.stringify(data),
}
console.log(config)
const response = await fetch(url, config)
console.log(response)
const status = response.status
const message = await response.json()
console.log(message)
toReturn = { status, message }
} catch (err) {
toReturn = {
status: 500,
message: err,
}
}
logResponse(toReturn, url)
return
}
// sendPostRequest("http://localhost:5001/soiling-sensor/us-east4/imageTest")