Reputation: 1786
I am using the request module to make a request to an API which sends me a Base64 encoded response which is a file.
app.get("/report", async(request, response) => {
const newRequest = new mdl.Request
const newDatasources = new mdl.Datasources
const newVariables = new mdl.VariablesType
const VariablesArray = []
const myArray = []
newRequest.Uri = "http://127.0.0.1:8080/listing.docx"
newRequest.Async = false
newRequest.OutputFormat = "html"
newVariables.Name = "Var1"
newVariables.Type = "text"
newVariables.Value = "Hello World!"
VariablesArray.push(newVariables)
newDatasources.Name = "JSON"
newDatasources.Type = "JSON"
newDatasources.Data = "ewogICAgImxpc3RQcmljZSI6ICIkODc5LDAwMCIsCiAAgICAgInB1cmNoQ29udHJhY3REYXRlIjogIiIsCiAgICAgICAgImVuZGluZ0RhdGUiOiAiIgogICAgfQp9"
newDatasources.Variables = VariablesArray
myArray.push(newDatasources)
newRequest.Datasources = myArray
const req_data = JSON.stringify(newRequest)
const options = {
hostname: 'report.sample.com',
port: 80,
path: '/v1/reports',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': req_data.length
}
}
const req = http.request(options, (error, resp, body) => {
console.log(`statusCode: ${res.statusCode}`)
resp.on('data', (d) => {
process.stdout.write(d)
})
})
resp.on('error', (error) => {
console.error(error)
})
req.write(req_data)
req.end()
response.send("done")
});
This prints out the received data on my terminal, but how can I go about storing it in a variable and return it as a response to a request? just some background to what i am doing. I am building a report request which is then send to a api via the request which returns me the data in form of the encoded string
Upvotes: 2
Views: 827
Reputation: 300
It's difficult to tell exactly what you're using to run this.
Use
res.status(200).send({success: true, data: data})
if you're using express.
Upvotes: 1