4

我们正在使用 3rd 方 api kraken.io来优化我们的图像。

优化图像的结果发布在Webhook.

在他们的 api 文档中它指出:优化结束后,Kraken 将向您的请求中指定的格式POST发送消息。callback_urlJSONapplication/json

我正在使用ngrok 允许远程webhooks将数据发送到我的开发机器,使用这篇文章。

结果发布到回调 URL:

HTTP/1.1 200 OK

{
    "id": "18fede37617a787649c3f60b9f1f280d",
    "success": true,
    "file_name": "header.jpg",
    "original_size": 324520,
    "kraked_size": 165358,
    "saved_bytes": 159162,
    "kraked_url": "http://dl.kraken.io/18/fe/de/37617a787649c3f60b9f1f280d/header.jpg"
}

类映射

public class KrakenOptimizedResults
{
public string id { get; set; }
public bool success { get; set; }
public string file_name { get; set; }
public int original_size { get; set; }
public int kraked_size { get; set; }
public int saved_bytes { get; set; }
public string kraked_url { get; set; }
}

动作方法

[HttpPost]
public ActionResult OptimizedWebHook()
{

  Request.InputStream.Position = 0;

  string jsonString = new System.IO.StreamReader(Request.InputStream).ReadToEnd();

  KrakenOptimizedResults obj = new JavaScriptSerializer().Deserialize<KrakenOptimizedResults>
  (jsonString);


  return Json(obj);

 }     

但是当我在 Html Visualizerdebug中收到jsonString它时,它看起来像配对keyvalue不是 Json 格式。

收到的结果不是 Json 格式:

file_name=header.jpeg&original_size=118066&kraked_size=102459&saved_bytes=15607

我猜收到的数据content-type:application/x-www-form-urlencoded.

为什么我接收keyvalue配对而不是 Json 格式?如何在 asp.net mvc 中反序列化 Json 数据?

4

3 回答 3

5

https://kraken.io的联合创始人在这里。

我们的文档中有一个明显的遗漏,我将在今天修复。要取回 JSON,您需要"json": true在请求中设置一个标志。省略该标志或设置"json": false将返回 URLEncoded。示例 cURL 请求:

curl http://api.kraken.io/v1/upload -X POST --form data='{"auth":{"api_key":"YOUR_KEY", "api_secret":"YOUR_SECRET"}, "wait": true, "lossy": true, "callback_url": "http://requestb.in/wbhi63wb", "json": true}' --form upload=@test.jpg

带来不便敬请谅解 :-(

于 2017-02-17T13:41:43.953 回答
1

我能够使用这个这个Key帖子将查询字符串和Value对转换为格式,将表单转换为有一些延迟,所以如果有更好的答案,那么做帖子和建议,下面是我的解决方案。JsonDictionaryJson

动作方法

[HttpPost]
public ActionResult OptimizedWebHook()
{

 Request.InputStream.Position = 0;

 string data = new System.IO.StreamReader(Request.InputStream).ReadToEnd();
 var dict = HttpUtility.ParseQueryString(data);
 var json = new JavaScriptSerializer().Serialize(dict.AllKeys.ToDictionary(k => k, k =>
            dict[k]));

 KrakenOptimizedResults obj = new JavaScriptSerializer().Deserialize<KrakenOptimizedResults>
                             (json);

  return Json(obj);

}

从 kraken API 接收 JSON 格式的优化结果。

正如@karim79 所提到的,要取回 JSON,您需要在请求中设置一个 "json": true 标志。

由于 Kraken .Net/C#SDK 没有设置“json”的选项:true,所以我必须扩展他们的基类。

扩展基类:

public class OptimizeRequestBaseExtended : OptimizeRequestBase,
IOptimizeUploadRequest, IRequest
{

    public OptimizeRequestBaseExtended(Uri callbackUrl)
    {
        CallbackUrl = callbackUrl;
    }

   [JsonProperty("callback_url")]
   public Uri CallbackUrl { get; set; }

   [JsonProperty("json")]
   public bool JsonFormat { get; set; }

}

请求 Kraken API:

var callbackUrl = new Uri("http://localhost:0000/Home/OptimizedWebHook");

OptimizeRequestBaseExtended settings = new OptimizeRequestBaseExtended(callbackUrl);

settings.Lossy = true;
settings.JsonFormat = true;

var response = client.Optimize(image: image, filename: filename, optimizeRequest: settings);

动作方法

[HttpPost]
public ActionResult OptimizedWebHook()
{

Request.InputStream.Position = 0;

string jsonString = new System.IO.StreamReader(Request.InputStream).ReadToEnd();


KrakenOptimizedResults obj = JsonConvert.DeserializeObject<KrakenOptimizedResults>
(jsonString);

  return Json(obj);

}
于 2017-02-15T11:14:14.363 回答
0

第 1 步:创建一个 aspx 页面。此页面必须能够接受 HTTP POST 请求。第 2 步:添加此代码以获取 HTTP POST 数据。文件:default.aspx.cs 文件:default.aspx.cs

var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();

FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(json.ToString() + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

第 3 步:创建 webhook。此代码可以链接到单击 event.File:default.aspx.cs 上的按钮

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
List<string> events = new List<string>();
events.Add("Update");

string postback = list.CreateWebhook(events, "URL", "json");

FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(postback + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

第 4 步:激活 webhook。从文本文件中复制该 webhook id 并将其粘贴到下面的代码中。文件:default.aspx.cs

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
list.ActivateWebhook("webhook id");

第 5 步:测试 weebhook.File:default.aspx.cs

AuthenticationDetails auth = new ApiKeyAuthenticationDetails("your api key");
string listID = "";
listID = "your list id";
List list = new List(auth, listID);
string postback = list.TestWebhook("webhook id").ToString();
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;

ostrm = new FileStream(@"C:\logfile4webhook.txt", FileMode.Append, FileAccess.Write);
writer = new StreamWriter(ostrm);
Console.SetOut(writer);
Console.Write(DateTime.Now + " ");
Console.WriteLine(postback + " ");
Console.SetOut(oldOut);
writer.Close();
ostrm.Close();

第 6 步:反序列化 JSON 对象的主体。我们需要基于 JSON 数据创建类结构。我将示例 json 放在这里,它创建了所需的类

public class CustomField
{
    public string Key { get; set; }
    public string Value { get; set; }
}

public class Event
{
    public List<CustomField> CustomFields { get; set; }
    public string Date { get; set; }
    public string EmailAddress { get; set; }
    public string Name { get; set; }
    public string SignupIPAddress { get; set; }
    public string Type { get; set; }
}

public class RootObject
{
    public List<Event> Events { get; set; }
    public string ListID { get; set; }
}

创建类后,将步骤 2 中的代码附加到后面

var json = reader.ReadToEnd();

反序列化和解析json。

RootObject myClass = JsonConvert.DeserializeObject(json);

if (myClass != null)
{
    List<Event> t = myClass.Events;

    string old_email = "", new_email = "";
    old_email = t[0].OldEmailAddress;
    new_email = t[0].EmailAddress;

    //now you can do your logic with old_email and new_email
}
于 2017-02-15T10:25:04.973 回答