Home > Net >  Problem with ssl comuniction with post request
Problem with ssl comuniction with post request

Time:10-02

Hey, I built ssl communication with self certificate. The connection is working, but when I trying to deliver data on the client side with post request in postman or my android client, the body of the request in the server side is empty. Here is my nodejs server code below,thankes for help.

const express = require('express')
const https = require('https')
const path = require('path')
const fs = require('fs')


const app = express()
app.use(express.json())    


app.post('/',(req,res,next)=>
{
    console.log("we got new connection")
    var data =req.body

    res.send(data)
})

const ssl_server = https.createServer(
    {
    key:fs.readFileSync(path.join(__dirname,'cert','key.pem')),
    cert:fs.readFileSync(path.join(__dirname,'cert','cert.pem'))
    
},app)

ssl_server.listen(3443,console.log("SSl server is online!"))

CodePudding user response:

You're reading the response body. You should look at the request.

In any case, there won't probably be much reasonable data to read unless you parse the POST payload somehow; I'd recommend the Express library rather than raw Node.js HTTP bits if you don't want to do that yourself.

CodePudding user response:

here is my answer for my question,like AKS said, you need to parse the post request, i add bodyparser to my code and it worked.

const express = require('express')
const https = require('https')
const path = require('path')
const fs = require('fs')
const bodyParser = require('body-parser');


const app = express()
app.use(bodyParser.urlencoded({ extended: true }))    // <==== parse request body as JSON

app.post('/',(req,res,next)=>
{
    console.log("we got new connection")
    var data =req.body

    res.send(data)
})

const ssl_server = https.createServer(
    {
    key:fs.readFileSync(path.join(__dirname,'cert','key.pem')),
    cert:fs.readFileSync(path.join(__dirname,'cert','cert.pem'))
    
},app)

ssl_server.listen(3443,console.log("SSl server is online!"))
  • Related