Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnityWebRequest.downloadHandler returning null, while getting response from Node Server

I am creating a Registration Scene on Unity. On the backend I have NodeJS server with MongoDB. Registration is successful and data is saved on Mongo too.

This is my NodeJS api for register

api.post('/register', (req,res) => {
Account.register(new Account({username: req.body.username}), req.body.password, function(err, account){
  console.log("acc: "+account);
  if(err){
    if (err.name == "UserExistsError") {
      console.log("User Exists");
      return res.status(409).send(err);
    }else {
      console.log("User Error 500");
      return res.status(500).send(err);
    }
  }else {
    let newUser = new User();
    newUser.accountid = account._id;
    newUser.name = req.body.fullname;
    newUser.gender = req.body.gender;
    newUser.role = req.body.role;
    newUser.country = req.body.country;
    newUser.coins = req.body.coins;
    newUser.save(err => {
      if(err){
        console.log(err);
        return res.send(err);
      }else{
        console.log('user saved');
        res.json({ message: 'User saved' });
      }
    });
    passport.authenticate(
      'local', {
        session: false
      })(req,res, () => {
         res.json({ registermsg: 'Successfully created new account'});
      });
  }
});
});

And this is my POST co-routine in Unity C#

IEnumerator Post(string b) {

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b);

    using (UnityWebRequest www = new UnityWebRequest(BASE_URL, UnityWebRequest.kHttpVerbPOST)) {
        UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
        www.uploadHandler = uH;
        www.SetRequestHeader("Content-Type", "application/json");
        yield return www.Send();

        if (www.isError) {
            Debug.Log(www.error);
        } else {
            lbltext.text = "User Registered";
            Debug.Log(www.ToString());
            Debug.Log(www.downloadHandler.text);
        }
    }
}

I am trying to Debug.Log(www.downloadHandler.text); but I get NullReferenceException.

I'd like to ask, is the way I am using to return response in my api correct? If yes, how can I use that response in Unity side.

like image 555
Luzan Baral Avatar asked Apr 08 '17 10:04

Luzan Baral


1 Answers

UnityWebRequest.Post, UnityWebRequest.Get and other UnityWebRequest functions that creates new instance of UnityWebRequest, will automatically have DownloadHandlerBuffer attached to it.

Now, if you create new instance of UnityWebRequest with the UnityWebRequest constructor, DownloadHandler is not attached to that new instance. You have to manually do that with DownloadHandlerBuffer.

IEnumerator Post(string b)
{

    byte[] bytes = System.Text.Encoding.ASCII.GetBytes(b);

    using (UnityWebRequest www = new UnityWebRequest(BASE_URL, UnityWebRequest.kHttpVerbPOST))
    {
        UploadHandlerRaw uH = new UploadHandlerRaw(bytes);
        DownloadHandlerBuffer dH = new DownloadHandlerBuffer();

        www.uploadHandler = uH;
        www.downloadHandler = dH;
        www.SetRequestHeader("Content-Type", "application/json");
        yield return www.Send();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            lbltext.text = "User Registered";
            Debug.Log(www.ToString());
            Debug.Log(www.downloadHandler.text);
        }
    }
}
like image 75
Programmer Avatar answered Nov 15 '22 16:11

Programmer