Hi,
I've written an API in c# that updates a SQL table. The API passes JSON to the code. If I run the API through Postman the code works correctly and updated the SQL table, but if I run through a CURL command I get the "Object reference not set to an instance of an object." error message.
In trying to work out what is going on I see that the values in the model don't appear to be populating when ran via CURL.
It's probably something simple, I'd appreciate if someone could point me in the right direction.
What I have tried:
My model...
public class Balance
{
public string customer { get; set; }
public string amount { get; set; }
public string type { get; set; }
}
public class CreateUpdate : Balance
{
}
and the PUT section of the API that works if I run through Postman...
public string Put([FromBody]CreateUpdate value)
{
_conPU = new SqlConnection(ConfigurationManager.ConnectionStrings["TestConnection"].ConnectionString);
var query = "insert into L_UPD (ACUID, AMNT, DATE ,COMMENTS) " +
"values (@ACUID, CASE WHEN @TYPE = 'debit' THEN @POINTS * -1 ELSE @POINTS END, getdate() , CASE WHEN @TYPE = 'debit' THEN 'REDEEM : ONLINE' ELSE 'EARN : ONLINE' END)";
SqlCommand insertcommand = new SqlCommand(query, _conPU);
insertcommand.Parameters.AddWithValue("@ACUID", value.customer);
insertcommand.Parameters.AddWithValue("@POINTS", value.amount);
insertcommand.Parameters.AddWithValue("@TYPE", value.type);
_conPU.Open();
int result = insertcommand.ExecuteNonQuery();
if (result > 0)
{
return "approved";
}
else
{
return "update failed";
}
}
The CURL request is
curl -X PUT -H "Content-Type: application/json" -d '{"customer":"435673","amount":5,"type":"debit"}' http:
EDIT :
Through postman if I try just the json, eg
{"customer":"435673","amount":5,"type":"debit"}
the code works, but in postman if I use curl
curl -X PUT -H "Content-Type: application/json" -d '{"customer":"435673","amount":5,"type":"debit"}'http:
it doesn't work.