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












0















Hello I want to run push app center from its api. 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



What it needs for Headers is:
X-API-Token ="{api token}" and 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.
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:



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? and the correct format of httpclient header and body?
I already found lot of sample but still no clue with the one that I want.
Really appreciate your help guys :)










share|improve this question

























  • There are many articles available in the net, e.g. stevejgordon.co.uk/…

    – Christoph Lütjen
    Nov 22 '18 at 11:59
















0















Hello I want to run push app center from its api. 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



What it needs for Headers is:
X-API-Token ="{api token}" and 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.
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:



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? and the correct format of httpclient header and body?
I already found lot of sample but still no clue with the one that I want.
Really appreciate your help guys :)










share|improve this question

























  • There are many articles available in the net, e.g. stevejgordon.co.uk/…

    – Christoph Lütjen
    Nov 22 '18 at 11:59














0












0








0








Hello I want to run push app center from its api. 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



What it needs for Headers is:
X-API-Token ="{api token}" and 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.
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:



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? and the correct format of httpclient header and body?
I already found lot of sample but still no clue with the one that I want.
Really appreciate your help guys :)










share|improve this question
















Hello I want to run push app center from its api. 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



What it needs for Headers is:
X-API-Token ="{api token}" and 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.
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:



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? and the correct format of httpclient header and body?
I already found lot of sample but still no clue with the one that I want.
Really appreciate your help guys :)







c# json httpclient json-serialization






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:31









JohnB

1,78411117




1,78411117










asked Nov 22 '18 at 11:51









Yeremia DanangYeremia Danang

457




457













  • There are many articles available in the net, e.g. stevejgordon.co.uk/…

    – Christoph Lütjen
    Nov 22 '18 at 11:59



















  • There are many articles available in the net, e.g. stevejgordon.co.uk/…

    – Christoph Lütjen
    Nov 22 '18 at 11:59

















There are many articles available in the net, e.g. stevejgordon.co.uk/…

– Christoph Lütjen
Nov 22 '18 at 11:59





There are many articles available in the net, e.g. stevejgordon.co.uk/…

– Christoph Lütjen
Nov 22 '18 at 11:59












1 Answer
1






active

oldest

votes


















1














You need to structure your objects similar to your required 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



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


Below is the code for http call



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);
};





share|improve this answer
























  • Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

    – Yeremia Danang
    Nov 22 '18 at 12:35











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430418%2fhttpclient-postasync-with-custom-header-and-application-json-for-body-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You need to structure your objects similar to your required 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



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


Below is the code for http call



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);
};





share|improve this answer
























  • Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

    – Yeremia Danang
    Nov 22 '18 at 12:35
















1














You need to structure your objects similar to your required 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



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


Below is the code for http call



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);
};





share|improve this answer
























  • Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

    – Yeremia Danang
    Nov 22 '18 at 12:35














1












1








1







You need to structure your objects similar to your required 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



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


Below is the code for http call



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);
};





share|improve this answer













You need to structure your objects similar to your required 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



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


Below is the code for http call



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);
};






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 12:24









MohsinMohsin

8119




8119













  • Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

    – Yeremia Danang
    Nov 22 '18 at 12:35



















  • Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

    – Yeremia Danang
    Nov 22 '18 at 12:35

















Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

– Yeremia Danang
Nov 22 '18 at 12:35





Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks

– Yeremia Danang
Nov 22 '18 at 12:35


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430418%2fhttpclient-postasync-with-custom-header-and-application-json-for-body-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'