I've recently started using azure function apps. I am passing some json data from Postman into my app when it's running so that I can test it and I'm getting an error of an unexpected character '{'.
{
"testId": "001",
"shopItems": {
"itemName": "something"
}
}
Here is what is happening.
Firstly, I have my very basic function, which can be called via it's route in postman. I pass the above request in the body as JSON to this function which then calls a service to work with it.
[FunctionName("UpdateCart")]
public async Task<IActionResult> UpdateCart(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "v1/cart/update")] HttpRequest req,
ILogger log)
{
await _myService.UpdateCartAsync(req.Body);
//Return ok
return new OkResult();
}
The body property of 'HttpRequest' is a 'Stream' and therefore my service and it's interface expect a stream to be passed in.
Interface
namespace MyProject.Services.Interface
{
public interface IMyService
{
Task UpdateCartAsync(Stream contents);
}
}
Service
Since we know it's a stream, I use "ReadToEndAsync" which then produces a string from the result. I pass that string to the "JsonConvert.Deserializer" to map that to my model and that's where it fails with the unexpected character '{' which is the bracer before "itemName".
public async Task UpdateCartAsync(Stream content)
{
var result = await new StreamReader(content).ReadToEndAsync();
Basket cart = JsonConvert.DeserializeObject<Cart>(result); //fails here
var db = _connectionMultiplexer.GetDatabase();
await db.StringSetAsync(cart.CartId, cart.CartContents);
}
Model
public class Cart
{
public string CartId { get; set; }
public string CartContents { get; set; }
}
I'm guessing here that the Steam string isn't actually accurate or that I perhaps need to serialize first, I'm not sure. Is there another way to pass JSON from the body to a functions app?