![](/img/trans.png)
[英]HttpRequest object is null in response from Azure Function
[英]How to convert an Azure Function HttpRequest body to an object
我在經典的 web apis 中做過幾次,但在 Azure 函數中沒有,所以我不確定我在這里缺少什么:
我的實體用戶:
[SharedCosmosCollection("shared")]
public class User : ISharedCosmosEntity
{
/// <summary>
/// User id
/// </summary>
[JsonProperty("Id")]
public string Id { get; set; }
/// <summary>
/// Cosmos entity name for shared collection
/// </summary>
[CosmosPartitionKey]
public string CosmosEntityName { get; set; }
public string GivenName { get; set; }
public string FamilyName { get; set; }
public string NickName { get; set; }
public string Name { get; set; }
public string Picture { get; set; }
public string Locale { get; set; }
public DateTime UodatedAt { get; set; }
public string Email { get; set; }
public bool EmailVerified { get; set; }
public string Sub { get; set; }
}
我的 CreateUser 的 Function 代碼:
[FunctionName("CreateUser")]
public static async Task<IActionResult> CreateUser(
[HttpTrigger(AuthorizationLevel.Function,
"post", Route = "user")]
HttpRequest req)
{
var telemetry = new TelemetryClient();
try
{
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
var input = JsonConvert.DeserializeObject<User>(requestBody);
var userStore = CosmosStoreHolder.Instance.CosmosStoreUsers;
var added = await userStore.AddAsync(input);
return new OkObjectResult(added);
}
catch (Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return new BadRequestResult();
}
}
在門戶中,我在請求正文中發送此 JSON
{
"given_name": "aaa",
"family_name": "bbb",
"nickname": "xx.xx.psg",
"name": "bbbb",
"picture": "https://lh3.googleusercontent.com/a-/AOhsGg8qaBLDPubSaNb3u8zMyiUGrwFE3zhQ8MMqLALjGc",
"locale": "es",
"updated_at": "2020-04-20T15:33:16.133Z",
"email": "xx.xx.psg@gmail.com",
"email_verified": true,
"sub": "google-oauth2|1111"
}
但是我收到 http 500 錯誤,不確定是什么問題
如果 class 屬性與 Z0ECD10F1C1D7ABBD784A 屬性不完全匹配,則序列化程序無法在沒有一點幫助的情況下將 JSON 屬性與 class 屬性匹配。 對於 map JSON 屬性到 class 屬性,請為每個適用屬性使用JsonProperty
屬性。 這將適用於序列化和反序列化。
[JsonProperty("given_name")]
public string GivenName { get; set; }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.