Mox_z
Mox_z

Reputation: 520

StringContent is sent as empty object to node

I have a C# desktop that needs to use HttpClient to make a post request to my node API. The problem is that the JSON that the node app receives is different than what I am intending. I have tried using the example below and I have read from the StringContent and seen that it contains the values that I expect but it is showing up on the node app as {};

User user = new User
{
    Username = username,
    Password = password
};
StringContent content = new StringContent(JsonConvert.SerializeObject(user)); 
HttpResponseMessage response = await client.PostAsJsonAsync("auth", content);

I have also tried similar code but instead of StringContent I used only string like this:


User user = new User
{
    Username = username,
    Password = password
};
StringContent content = JsonConvert.SerializeObject(user); 
HttpResponseMessage response = await client.PostAsJsonAsync("auth", content);

But this gives me an error about Unexpected Token " in JSON at position 0;

Please help me understand how I can send a properly serialized user object. I would prefer doing this without implementing ISerializable if possible. here is my User data class:

namespace Cloud_Calendar
{
    class User
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
}

I think that it might be useful to see what my node is doing so here it is:

let failCount = 0;
app.all('/failAuth', (req, res) => {
  console.log("failed" + (++failCount));
  console.log(req.body);
  res.send('failure to authenticate user');
});

app.post('/main', (req, res) => {   //this will be removed & replaced with routes for each action
res.send('success');
});


app.post('/auth', passport.authenticate('local', { failureRedirect: '/failAuth' }), (req, res) => {
//TODO: if success parse req.body to search for req.body.action variable and redirect based on value
console.log(`req.body.action: ${req.body.action}`); //this indicates desired action
  try{
    res.redirect(308, '/main');
  }catch(err){
    console.error(err);
  }
});

req.body is logged as empty... is this because of the redirect?

Upvotes: 3

Views: 1700

Answers (1)

Nkosi
Nkosi

Reputation: 246998

With PostAsJsonAsync you need to pass the object as is. Internally it will serialize the object, create a StringContent and post that via the HttpClient

So either use PostAsJsonAsync as it was intended

User user = new User
{
    Username = username,
    Password = password
};
HttpResponseMessage response = await client.PostAsJsonAsync("auth", user);

Or perform the serialization yourself (basically what PostAsJsonAsync does internally)

User user = new User
{
    Username = username,
    Password = password
};
string json = JsonConvert.SerializeObject(user);
StringContent content = new StringContent(josn, Encoding.UTF8, "application/json"); 
HttpResponseMessage response = await client.PostAsync("auth", content);

Upvotes: 3

Related Questions