1

Yes, I've already looked at similar questions like this Express.js req.body undefined but it did not help me, and yes I've tried changing the code to a middleware.

My problem is that the passed XML is valid and everything is ok (I'm using Angular for Frontend), but my backend always delivers me an undefined variable req.body... I tried printing out console.log(req) and it always shows the body as an empty object.

I have the following code:

var testvar = req.body;
var req = http.request(options, function (res) {
    var chunks = [];
  
    res.on("data", function (chunk) {
      chunks.push(chunk);
    });
  
    res.on("end", function (chunk) {
      var body = Buffer.concat(chunks);
      console.log(body.toString());
    });
  
    res.on("error", function (error) {
      console.error(error);
    });
  });
  
  req.write(postData);
  
  req.end();
  console.log(testvar);

The code is implemented inside a app.post, which naturally contains req, res as usual. Options contain the IP, port etc, which is not relevant for solving this riddle.

Also: Once the post method is called, the error

TypeError: data should be a string, Buffer or Uint8Array

occurs in the backend.

Serg
  • 2,346
  • 3
  • 29
  • 38
Munchkin
  • 857
  • 5
  • 24
  • 51

4 Answers4

2

If native methods fail, try to install 'request' with node package manager.

Try to implement it in your function like this: var request = require('request');

You should now be able to access the body object via the request function as follows:

  request(options, function (error, request) {
    if (error) throw new Error(error);
    console.log(request.body);
    req.write(request.body);
    req.end();
    res.status(200).send(req.body);
  });
crispyzlata
  • 195
  • 9
  • This actually works! Also the bonus points that the response is given back is fulfilled as well, any clue why the native approach fails? – Munchkin Jul 16 '20 at 11:31
  • 2
    depricated https://github.com/request/request/issues/3142 – x00 Jul 17 '20 at 20:11
0

In angular try defining an interface for what you are receiving from the backend, so that when you create the observable, it knows what it is fetching from the backend. As you are saying that you have already parsed the information from the XML in the backend. this should work. Eg:

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { ErrorprocessorService } from './errorprocessor.service';
import { Observable, Subject } from 'rxjs';
import { map } from 'rxjs/operators';

interface JWTResponse {
  success: boolean;
  message: string;
  user: any;
}

@Injectable({
  providedIn: 'root'
})

export class AuthService {

  constructor(private http: HttpClient, private errorProcessor: ErrorprocessorService) { }

  checkJWTtoken() {
    this.http.get<JWTResponse>('http://localhost:3000/auth/checkToken')
      .subscribe(res => {
        console.log('JWT Token Valid: ', res);
        this.sendUsername(res.user.username);
      }, err => {
        console.log('JWT Token Invalid: ', err);
        this.destroyUserCedentials();
      });
  }
}
Akshay Sethia
  • 37
  • 2
  • 7
0

It seems you need to add an XML body parser.

You could try with body-parser-xml.

Daniele Ricci
  • 15,422
  • 1
  • 27
  • 55
0

You need xml-body-parser for this. The body-parser will only parse json, text etc but not the xml. So in your code, you might need to do something like,

var xmlparser = require('express-xml-bodyparser');

app.use(xmlparser());
gaurav soni
  • 206
  • 1
  • 10