How to accept POST data in Node.js - python

I am using NodeJS to send notifications to my clients using NowJS but the data that I need to send in the notifications has to come from my database.
Since I am using Django backend, I can make HTTP requests to my Node.js server and send the required data. But I need to be able to accept this data using Node.js. How can I do this?

How can I do this?
require("http").createServer(function (req, res) {
doStuff();
}).listen(PORT);

I'm a fan of using Connect/Express, so here's a trivial example you could use:
var express = require('express');
var app = express.createServer();
var nowjs = require("now");
var everyone = nowjs.initialize(app);
app.use(express.bodyParser()); //If you want nice JSON parsing
app.post('/sendNotification', function(req, res){
console.log(req.params) //Look at the POST params
console.log(req.body) //Look at the POST body
everyone.now.receiveMessage(req.body.dataYouCareAbout);
res.send('Notification Sent!');
});
You could then use that "sendNotification" endpoint to accept POST data and send it (or some piece of it, or anything really) down to NowJS.

Use formidable. Here's a short example from their docs:
var formidable = require('formidable'),
http = require('http'),
util = require('util');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}

Related

How to fetch data analyzed in python to node.js and pass it to angular?

I am new to angular and i want to display JSON data from python to angular with the help of node.js and I used child process to connect python and node.js but I dont know how to pass it to angular service
node.js file
const express = require('express')
const { spawn } = require('child_process')
const app = express()
const port = 8000
app.get('/', (req, res) => {
let dataToSend
let largeDataSet = []
// spawn new child process to call the python script
const python = spawn('python', ['test.py'])
// collect data from script
python.stdout.on('data', function (data) {
console.log('Pipe data from python script ...')
//dataToSend = data;
largeDataSet.push(data)
})
// in close event we are sure that stream is from child process is closed
python.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`)
// send data to browser
res.send(largeDataSet.join(''))
})
})
app.listen(port, () => {
console.log(`App listening on port ${port}!`)
})
Technically you just have to send a Http GET request from your service.
I suggest that you should read and follow this offical http client guide to set it up correctly.
Here is a simple service snippet. This should be enough.
#Injectable({
providedIn: 'root',
})
export class MyService {
constructor(private http: HttpClient) {}
getData(): Observable<any> {
const url = '';
return this.http.get(url);
}
}

Swift HTTP session not sending actual Request

So I have some Swift code that send a request to my local host
//
// ContentView.swift
// Shared
//
// Created by Ulto4 on 10/23/21.
//
import SwiftUI
struct ContentView: View {
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button(action : {
self.fu()
}, label: {
Image(systemName: "pencil").resizable().aspectRatio(contentMode:.fit)
})
}
}
func fu(){
let url = URL(string: "http://127.0.0.1:5000/232")
guard let requestUrl = url else { fatalError() }
var request = URLRequest(url: requestUrl)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error took place \(error)")
return
}
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
However, on my Flask app there are no get requests coming in and the function isn't running. There also isn't anything printing to the console.
I am fairly new to swift so I don't really know how to fix this.
Is there any other way to send requests in swift, if not, How would I fix this?
You are creating the URLSessionDataTask, but you never start it. Call task.resume(), e.g.
func performRequest() {
guard let url = URL(string: "http://127.0.0.1:5000/232") else {
fatalError()
}
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let error = error {
print("Error took place \(error)")
return
}
if let response = response as? HTTPURLResponse {
print("Response HTTP Status code: \(response.statusCode)")
}
}
task.resume() // you must call this to start the task
}
That having been said, a few caveats:
You are doing http rather than https. Make sure to temporarily enable insecure network requests with app transport settings, e.g.
You didn’t say if this was for macOS or iOS.
If running on physical iOS device, it will not find your macOS web server at 127.0.0.1 (i.e., it will not find a web server running on your iPhone). You will want to specify the IP number for your web server on your LAN.
If macOS, make sure to enable outbound network requests in the target’s “capabilities”:
You asked:
Is there any other way to send requests in swift?
It is probably beyond the scope of your question, but longer term, when using SwiftUI, you might consider using Combine, e.g., dataTaskPublisher. When running a simple “what was the status code” routine, the difference is immaterial, but when you get into more complicated scenarios where you have to parse and process the responses, Combine is more consistent with SwiftUI’s declarative patterns.
Let us consider a more complicated example where you need to parse JSON responses. For illustrative purposes, below I am testing with httpbin.org, which echos whatever parameters you send. And I illustrate the use of dataTaskPublisher and how it can be used with functional chaining patterns to get out of the mess of hairy imperative code:
struct SampleObject: Decodable {
let value: String
}
struct HttpBinResponse<T: Decodable>: Decodable {
let args: T
}
class RequestService: ObservableObject {
var request: AnyCancellable?
let decoder = JSONDecoder()
#Published var status: String = "Not started yet"
func startRequest() {
request = createRequest().sink { completion in
print("completed")
} receiveValue: { [weak self] object in
self?.status = "Received " + object.value
}
}
func createRequest() -> AnyPublisher<SampleObject, Error>{
var components = URLComponents(string: "https://httpbin.org/get")
components?.queryItems = [URLQueryItem(name: "value", value: "foo")]
guard let url = components?.url else {
fatalError("Unable to build URL")
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: HttpBinResponse<SampleObject>.self, decoder: decoder)
.map(\.args)
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
struct ContentView: View {
#ObservedObject var requestService = RequestService()
var body: some View {
VStack{
Text("Hello, world!")
.padding()
Button {
requestService.startRequest()
} label: {
Image(systemName: "pencil").resizable().aspectRatio(contentMode:.fit)
}
Text(requestService.status)
}
}
}
But, like I said, it is beyond the scope of this question. You might want to make sure you get comfortable with SwiftUI and basic URLSession programming patterns (e.g., making sure you resume any tasks you create). Once you have that mastered, you can come back to Combine to write elegant networking code.
FWIW, like workingdog said, you could also use the new async-await rendition of data(for:delegate:). But when in the declarative world of SwiftUI, I would suggest Combine.

node request vs python session

I have a working node.js script which written using the node.js request module.
I'm trying to convert this script to python with the session module.
I'm new to python and I followed the python docs as it mentioned. but I'm struggling to get my code works.
the problem I'm having is sending the cookie values in the subsequent requests with the session module.
as per the docs it is saving cookies and send them automatically in any requests after that. but
here is my working node.js script
const request = require('request');
const fs = require('fs');
const getOptions = {
jar:true,
followAllRedirects:true,
method:'GET',
url:'https://dummyurl.com'
};
request.get(getOptions,(err,response,html)=>{
if(err){
console.log('error in request');
console.log(err);
}
else {
const postOptions = {
jar:true,
followAllRedirects: true,
method:'POST',
url:'https://dummyurl.com',
form:{
'data':{
'page':2
}
}
};
request.post(postOptions,(err,response,html)=>{
if(err){
console.log('post err');
console.log(err);
}
else {
fs.writeFileSync('pyres.html',html,'utf8');
}
})
}
});
this is my python conversion of above script
s = requests.Session()
url= 'https://dummyurl.com'
response = s.get(url)
print(response.cookies)
data_url = 'https://dummyurl.com/'
postData = {
"data":{
"page":2
}
}
resultResponse = s.post(data_url,data=postData)
print(resultResponse.content)
Can anyone points me out any mistake in this code?
actually the problem was in data format.
in nodejs I post it like this
{'data':{'page':2} }
but in python it should be converted like this
{
'data[page]': '2'
}
not sure why it was not worked in normal json format in python

Request.files object is empty on flask server when uploaded using ng-file-upload

I am using ng-file-upload to upload a xls or xlsx file, below is my html code
<div ngf-drop ngf-select ngf-change="checkFile($invalidFiles);" ngf-model-invalid="invalidFiles" ng-model="file" class="center" ngf-drag-over-class="'dragover'" ngf-pattern="'.xls,.xlsx'">
<button class="some-class" ng-click="upload(file)"></button>
Angular controller:
$scope.upload = function (file) {
if (file) {
var upload = Upload.upload({
url: API.url('upload'),
data: {
file: file
},
headers:{ // tried passing different headers as well
"Content-Type":"multipart/form-data"
}
});
upload.then(function (resp) {
//Some Operation
}, null, function (evt) {
//Some Operation
});
}
};
Flask server:
def upload(self):
params = self.get_params(request)
file = request.files['file'] ###This object is empty.
result = self._upload_file(file)
return "Dummy"
I followed this documentation for server side.
I think the problem is from client side, may be I am sending some bad request as flask server is throwing 400. So I have tried by sending 'multipart/form-data' headers but no luck and also by sending Form data explicitly.
I have also looked for few solutions like sol-1.
I didn't find anything which could solve my problem in the ng-file-upload doc as well.
If required I can send the request headers as well.
I might be missing very tiny part, can someone please point me out that.
There is default value for headers which is already in post request. Adding "Content-Type":"multipart/form-data" is just overriding that. Try it without that.

Parsing postman collection with python to create requests

I have a postman collection which has multiple folders containing around 100 requests with all methods like GET, POST, DELETE, PUT.
I need to parse the postman.json collection file in python and create requests and write to a txt file.
These request need to be passed to another tool. Can you help me with it. Any pointers would be helpful.
I am stuck in parsing collection JSON file, which is very difficult.
HOW TO GUIDE
Read the doc about JSON encoder/decoder
To parse a JSON string json_str:
import json
obj = json.loads(json_str)
To parse a JSON file:
import json
import io
with io.open("path/to/file.json", mode-"rb") as fd:
obj = json.load(fd)
Read the doc about Requests
There is a requests.request() function, where you can pass the method (GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD) in parameter. Usage:
import requests
response = requests.request('GET', 'http://httpbin.org/get')
Write into a binary file
You can write the result of your request into a binary file. You can also use a text file but some response may be binary, for instance if you download an image.
with io.open("path/to/file.dat", mode="wb") as fd:
fd.write(response.content)
The code below will help you understand how to parse the Collection JSON file so that you can recursively accumulate all the requests in that collection. I have used the Postman Collection SDK along with a helper utility called Lodash.
You can either use this snippet below to get the request information you want to be consumed by using Python requests. Or to keep things simple, just use Javascript if you can.
var fs = require('fs'), // needed to read JSON file from disk
sdk = require('postman-collection'),
Collection = sdk.Collection,
Request = sdk.Request,
Item = sdk.Item,
ItemGroup = sdk.ItemGroup,
_ = require('lodash'),
myCollection,
requests = [],
dfs = function (item, requests) { // fn -> Depth first search
// Check if this is a request
if (Item.isItem(item)) {
if (item.request && Request.isRequest(item.request)) {
requests.push(item.request);
}
}
// Check if this is a nested folder
else if (ItemGroup.isItemGroup(item)) {
// Check if this is an empty folder
if (item.items && (item.items.count() === 0)) {
return requests;
}
// Do a depth first search for requests on the nested folder
item.each(function (item) {
requests.push(dfs(item, []));
})
}
return requests;
};
// Load a collection to memory from a JSON file on disk
myCollection = new Collection(JSON.parse(
fs.readFileSync('<path_to_your_collection_json_file>').toString()));
myCollection.items.each(function (item) {
// Check if this is a request at the top level
if (Item.isItem(item)) {
if (item.request && Request.isRequest(item.request)) {
requests.push(item.request);
}
}
// Check if this is a folder at the top level
else if (ItemGroup.isItemGroup(item)) {
item.items.each(function (item) {
requests.push(dfs(item, []));
})
}
});
// Flatten. After flattening requests will an array of PostmanRequest objects
requests = _.flattenDeep(requests)
// Serialize each PostmanRequest to it's JSON representation
requests = _.map(requests, (r) => { return r.toJSON(); })
_.each(requests, (request) => {
console.log(request.url); // The request URL
console.log(request.method); // The HTTP Verb of your request Eg. GET, POST
_.each(request.header, (header) => {
console.log(header.key, header.value); // Eg. key -> 'Content-Type', value -> 'application/json'
});
// You can also access the request body and the auth, certificate and proxy used by the request
// Your PostmanRequest description is also available
});

Categories

Resources