我正在尝试使用onesignal页面中的示例发送一些数据
var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest;
request.KeepAlive = true;
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("authorization", "Basic xxx");
var obj = new
{
app_id = "xxx",
contents = new { en = "English Message" },
included_segments = new string[] { "Active Users" }
};
var param = JsonConvert.SerializeObject(obj);
byte[] byteArray = Encoding.UTF8.GetBytes(param);
使用此代码工作正常但我使用Flurl向这样的信号发出请求
var body = new
{
app_id = "xxx",
contents = new
{
es = "Mensaje prueba"
},
included_segments = new string[] { "All" }
};
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
var response = await new Flurl.Url(urlbase)
.AppendPathSegment("notifications")
.WithHeader("Content-Type", "application/json; charset=utf-8")
.WithHeader("Authorization", "Basic xxx")
.PostAsync(content)
.ReceiveString();
但是我得到了“糟糕的请求”。请有人帮忙指出如何与Flurl进行相同的通话?
正如第一条评论中所提到的,你做的工作超出了你的需要。 Flurl会为你序列化body
,所以删除这些行:
string param = JsonConvert.SerializeObject(body);
var content = new System.Net.Http.ByteArrayContent(Encoding.UTF8.GetBytes(param));
并使用body
直接发布PostJsonAsync
:
var response = await urlbase
...
.PostJsonAsync(body)
.ReceiveString();