简体   繁体   English

具有自定义标头和主体C#的application / json的HttpClient postasync

[英]HttpClient postasync with custom header and application/json for body C#

Hello I want to run push app center from its api . 您好,我想从其api运行push app center。 But I don't know how to make the proper format. 但是我不知道如何制作正确的格式。

I want to postasync from this api: https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications 我想从此api进行postasynchttps : postasync

What it needs for Headers is: X-API-Token ="{api token}" and Content Type="application/json" 标头所需的是:X-API-Token =“ {api token}”和Content Type =“ application / json”

For the body(content) I want to put this: 对于正文(内容),我想说一下:

{
    "notification_content" : {
        "name" : "Campaign Name",
        "title" : "Expired Warning",
        "body" : "You have items that almost expired"
    }
}

I have difficulties how to write in the correct format for HttpClient. 我很难为HttpClient编写正确的格式。 I tried this and no work.. 我尝试了这个,没有工作..

Content = new Content
{
   Name = "Campaign Name",
   Title = "Expired Warning",
   Body = "You have items that almost expired"
};
using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
{
   var myContent = JsonConvert.SerializeObject(data);
   client.DefaultRequestHeaders.Add("X-API-Token", "{my api token}");
   client.DefaultRequestHeaders.Accept.Add(new 
   MediaTypeWithQualityHeaderValue("application/json"));
   var builder = new UriBuilder(new Uri("https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications"));
   HttpResponseMessage response = await client.PostAsync(builder.Uri, content);
};

But I know this is code: 但是我知道这是代码:

 {
        "notification_content" : {
            "name" : "Campaign Name",
            "title" : "Expired Warning",
            "body" : "You have items that almost expired"
        }
    }

is not same with this to convert the json format: 转换json格式与此不同:

Content = new Content
{
    Name = "Campaign Name",
    Title = "Expired Warning",
    Body = "You have items that almost expired"
};

Can help me with the correct Serialize Json Format? 可以为我提供正确的序列化Json格式吗? and the correct format of httpclient header and body? 以及httpclient标头和正文的正确格式? I already found lot of sample but still no clue with the one that I want. 我已经找到了很多样本​​,但是仍然对我想要的样本一无所知。 Really appreciate your help guys :) 非常感谢您的帮助:)

You need to structure your objects similar to your required JSON . 您需要构造类似于所需JSON对象。

Create classes like below. 创建如下所示的类。

public class NotificationContent
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("body")]
    public string Body { get; set; }
}

public class PostObject
{
    [JsonProperty("notification_content")]
    public NotificationContent NotificationContent { get; set; }
}

Above is the right structure, now when you will call JsonConvert.SerializeObject , your json will be 上面是正确的结构,现在当您调用JsonConvert.SerializeObject ,您的json将是

 {
    "notification_content" : {
        "name" : "Campaign Name",
        "title" : "Expired Warning",
        "body" : "You have items that almost expired"
    }
} 

Below is the code for http call 以下是http呼叫的代码

using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
    {
        PostObject postObject = new PostObject
        {
            NotificationContent = new NotificationContent
            {
                Name = "Campaign Name",
                Title = "Expired Warning",
                Body = "You have items that almost expired"
            }
        };

        var myContent = JsonConvert.SerializeObject(postObject);
        client.DefaultRequestHeaders.Add("X-API-Token", "{my api token}");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var builder = new UriBuilder(new Uri("https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications"));

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
        request.Content = new StringContent(myContent, Encoding.UTF8, "application/json");//CONTENT-TYPE header

        HttpResponseMessage response = await client.SendAsync(request);
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM