HttpClient postasync with custom header and application/json for body C#
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
add a comment |
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
There are many articles available in the net, e.g. stevejgordon.co.uk/…
– Christoph Lütjen
Nov 22 '18 at 11:59
add a comment |
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
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
c# json httpclient json-serialization
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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);
};
Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks
– Yeremia Danang
Nov 22 '18 at 12:35
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
};
Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks
– Yeremia Danang
Nov 22 '18 at 12:35
add a comment |
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);
};
Thanks a lot sir :) this one really working. I working this things 3 days straight.. Thanks
– Yeremia Danang
Nov 22 '18 at 12:35
add a comment |
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);
};
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);
};
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
There are many articles available in the net, e.g. stevejgordon.co.uk/…
– Christoph Lütjen
Nov 22 '18 at 11:59