I have the following endpoint in my asp.net core 2.0 app:
[HttpPost("postself")]
public IActionResult post([FromBody]JObject email)
{
try
{
var service = new EmailService();
var emailHtml = service.GenerateEmail(email.ToString(), false);
return Json(new { Email = emailHtml });
}
catch
{
return Json(new { Email = "error" });
}
}
When calling the API like so:
curl -X POST \
http://myapp/api/v1/emails/postself \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 85c9bbe7-2112-0746-5f41-8dc01b52ab59'
The endpoint gets hit and returns at return Jason(new { Email = "error" });
so that works. It returns error
because the JObject
is null
but still this is expected behavior.
However, when calling the API like so (with JSON payload):
curl -X POST \
http://myapp/api/v1/emails/postself \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: a80c85de-8939-01d8-4a5d-c2108bf1491d' \
-d '{
"body": [
{
"image": {
"url": "mailing_logo_slice.png",
"alt": "This is my logo"
}
}
]
}'
...my app returns 400 Bad request
.
Also, the requests work in development, but only returns 400
in production.
Any ideas?
=========
Updated code:
[HttpPost("postself")]
public IActionResult PostSameDomain([FromBody]string email)
{
try
{
var service = new EmailGenerationService();
var emailHtml = service.GenerateEmailFromJson(email, false);
return Json(new { Email = emailHtml });
}
catch
{
return Json(new { Email = "error" });
}
}
[HttpPost("test")]
public IActionResult PostSameDomain([FromBody]EmailViewModel email)
{
try
{
var service = new EmailGenerationService();
var emailHtml = service.GenerateEmailFromJson(email.Raw, false);
return Json(new { Email = emailHtml });
}
catch
{
return Json(new { Email = "error" });
}
}