gpt4 book ai didi

C# HttpClient - PutAsync

转载 作者:行者123 更新时间:2023-12-04 02:46:25 33 4
gpt4 key购买 nike

我有一个后端 Controller 方法

[Route("{ropeId}/segments")]
[HttpPut]
public async Task<IActionResult> Put([NotNull] [FromBody] IEnumerable<Segment> segments)
{
Debug.WriteLine("In here!");
}

我可以在 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}]

分割模型:

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; }
}

我无法通过 httpClient PutAsync 调用访问它。

我试过:

var httpContent = new StringContent(updatedSegments);
var response = await httpClient.PutAsync(@"https://localhost:6001/api/ropes/3/segments", httpContent);

但是我得到一个 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'.

我的 PutAsync 调用有什么问题?

就像我说的,这适用于 Postman/PUT scratch。

如果我这样做

var s = await httpContent.ReadAsStringAsync();
s.Print();

我得到上面的 JSON,所以我认为问题是我需要以某种方式在 httpContent 上设置 header 以告诉它它是 application/json?

最佳答案

您是否尝试过在 httpContent 对象的构造函数中指定内容类型?我使用 httpClient 调用并声明类型对我有用,如下所示:

var httpContent= new StringContent(updatedSegments.ToString(Newtonsoft.Json.Formatting.None), Encoding.UTF8, "application/json");

让我知道它是否也对你有用

关于C# HttpClient - PutAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041997/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com