I have a backend controller method
[Route("{ropeId}/segments")]
[HttpPut]
public async Task<IActionResult> Put([NotNull] [FromBody] IEnumerable<Segment> segments)
{
Debug.WriteLine("In here!");
}
I can hit this fine using a scratch pad in Rider:
PUT https://localhost:6001/api/ropes/3/segments
Accept: */*
Cache-Control: no-cache
Content-Type: application/json
[{"SegmentNo":1,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":16,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1}, {"SegmentNo":50,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":69,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":101,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1}, {"SegmentNo":106,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1},
{"SegmentNo":128,"RopeId":3,"LastReceived":"2019-12-28T14:34:35.9799039+01:00","BendDamage":0.018,"Temperature":30.0,"LoadLevel":1}]
The Segment Model:
public sealed class Segment
{
public Segment(short segmentNo,
byte ropeId,
DateTime lastReceived,
double bendDamage,
double temperature,
byte loadLevel)
{
SegmentNo = segmentNo;
RopeId = ropeId;
LastReceived = lastReceived;
BendDamage = bendDamage;
Temperature = temperature;
LoadLevel = loadLevel;
}
public short SegmentNo { get; }
public byte RopeId { get; }
public DateTime LastReceived { get; }
public double BendDamage { get; }
public double Temperature { get; }
public byte LoadLevel { get; }
}
I can't hit it with a httpClient PutAsync call.
I've tried:
var httpContent = new StringContent(updatedSegments);
var response = await httpClient.PutAsync(@"https://localhost:6001/api/ropes/3/segments", httpContent);
But I get a SerializeError.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Route matched with {action = "Put", controller = "Ropes"}. Executing action WEBAPI.Controllers.RopesController.Put (WEBAPI)
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.
What is wrong with my PutAsync call?
Like I said, this works in Postman / PUT scratch.
If I do
var s = await httpContent.ReadAsStringAsync();
s.Print();
I get the JSON above, so I think the issue is I need to set a header somehow on the httpContent to tell it that it is application/json?
Have you tried to specify the content type on the constructor for the httpContent object? I worked with httpClient call and declaring the type worked for me like so:
var httpContent= new StringContent(updatedSegments.ToString(Newtonsoft.Json.Formatting.None), Encoding.UTF8, "application/json");
Let me know if it worked for you as well