[英]Azure Function Contentful Webhook Error in C#
我在嘗試從內容豐富的Webhooks中獲取http響應時遇到問題。 我不斷收到此錯誤:
“ WebHook請求包含無效的JSON:'沒有MediaTypeFormatter可用於從媒體類型為'application / vnd.contentful.management.v1 + json的內容中讀取'JToken'類型的對象”
這是我的功能代碼:
#r "Newtonsoft.Json"
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Net.Http.Formatting;
using System.Web.Http;
public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
@json(body('formdataAction'));
log.Info($"wjat");
// req.Headers.ContentType = new MediaTypeHeaderValue("application/json");
log.Info($"Webhook was triggered!");
string jsonContent = await req.Content.ReadAsStringAsync();
// BodyParser.Of(BodyParser.TolerantJson.class);
dynamic data = JsonConvert.DeserializeObject(jsonContent);
log.Info(data.sys);
if (data.first == null || data.last == null)
{
return req.CreateResponse(HttpStatusCode.BadRequest, new
{
error = "Please pass first/last properties in the input object"
});
}
return req.CreateResponse(HttpStatusCode.OK, new
{
greeting = $"Hello {data.first} {data.last}!"
});
}
問題是ReadAsAsync
嘗試使用默認的媒體格式化程序,默認情況下僅允許application/json
。
當然,您也可以注冊以將json格式化程序用於內容類型,但是我建議您建議使用NewtonSoft.Json並執行類似的操作。
string json = await req.Content.ReadAsStringAsync();
dynamic data = JsonConvert.DeserializeObject(json);
這是與Contentful的webhook一起使用的完整功能:
#r "Newtonsoft.Json"
using System.Net;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,
TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// Get request body
string json = await req.Content.ReadAsStringAsync();
log.Info($"Read json: {json}");
dynamic data = JsonConvert.DeserializeObject(json);
string deserialized = data?.ToString();
return req.CreateResponse(HttpStatusCode.OK, deserialized);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.