Post a Json string in Xamarin












2














In my app, I am posting json string to the server in this manner:



string url = "my/url";
HttpClient newClient = new HttpClient();

string contentType = "application/json";
JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var jon = JsonConvert.SerializeObject(json);
var content = new StringContent(jon, Encoding.UTF8, contentType);
var TaskPostAsync = await newClient.PostAsync(url, content);


if (TaskPostAsync.IsSuccessStatusCode)
{
var contentString = await TaskPostAsync.Content.ReadAsStringAsync();}


The response I am getting is that it is not in a Json format. Where am I going wrong. Any help would be much appreciated.
Data is a string.










share|improve this question
























  • Can you get what you server has received?
    – Jack Hua - MSFT
    Nov 22 at 5:34
















2














In my app, I am posting json string to the server in this manner:



string url = "my/url";
HttpClient newClient = new HttpClient();

string contentType = "application/json";
JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var jon = JsonConvert.SerializeObject(json);
var content = new StringContent(jon, Encoding.UTF8, contentType);
var TaskPostAsync = await newClient.PostAsync(url, content);


if (TaskPostAsync.IsSuccessStatusCode)
{
var contentString = await TaskPostAsync.Content.ReadAsStringAsync();}


The response I am getting is that it is not in a Json format. Where am I going wrong. Any help would be much appreciated.
Data is a string.










share|improve this question
























  • Can you get what you server has received?
    – Jack Hua - MSFT
    Nov 22 at 5:34














2












2








2


1





In my app, I am posting json string to the server in this manner:



string url = "my/url";
HttpClient newClient = new HttpClient();

string contentType = "application/json";
JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var jon = JsonConvert.SerializeObject(json);
var content = new StringContent(jon, Encoding.UTF8, contentType);
var TaskPostAsync = await newClient.PostAsync(url, content);


if (TaskPostAsync.IsSuccessStatusCode)
{
var contentString = await TaskPostAsync.Content.ReadAsStringAsync();}


The response I am getting is that it is not in a Json format. Where am I going wrong. Any help would be much appreciated.
Data is a string.










share|improve this question















In my app, I am posting json string to the server in this manner:



string url = "my/url";
HttpClient newClient = new HttpClient();

string contentType = "application/json";
JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var jon = JsonConvert.SerializeObject(json);
var content = new StringContent(jon, Encoding.UTF8, contentType);
var TaskPostAsync = await newClient.PostAsync(url, content);


if (TaskPostAsync.IsSuccessStatusCode)
{
var contentString = await TaskPostAsync.Content.ReadAsStringAsync();}


The response I am getting is that it is not in a Json format. Where am I going wrong. Any help would be much appreciated.
Data is a string.







c# json xamarin xamarin.ios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 1:27

























asked Nov 21 at 0:36









Prashant Bhandari

216




216












  • Can you get what you server has received?
    – Jack Hua - MSFT
    Nov 22 at 5:34


















  • Can you get what you server has received?
    – Jack Hua - MSFT
    Nov 22 at 5:34
















Can you get what you server has received?
– Jack Hua - MSFT
Nov 22 at 5:34




Can you get what you server has received?
– Jack Hua - MSFT
Nov 22 at 5:34












1 Answer
1






active

oldest

votes


















1














By calling



 var jon = JsonConvert.SerializeObject(json);


You are serializing it twice.



JObject is already JSON so all you need to do is call .ToString to get the JSON



//...

JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var content = new StringContent(json.ToString(), Encoding.UTF8, contentType);

//...


Reference Write JSON text with JToken.ToString



Another option would be to use an anonymous object and then serialize that



//...

var model = new {
id = "id",
apiKey = "apiKey",
encryptedData = Data
};

var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, contentType);

//...





share|improve this answer























  • Tried it, still doesnt work. I have no idea whats going wrong.
    – Prashant Bhandari
    Nov 21 at 2:07










  • @PrashantBhandari can you show the actual error
    – Nkosi
    Nov 21 at 2:08










  • { "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
    – Prashant Bhandari
    Nov 21 at 2:14










  • This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
    – Prashant Bhandari
    Nov 21 at 2:17











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%2f53403702%2fpost-a-json-string-in-xamarin%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














By calling



 var jon = JsonConvert.SerializeObject(json);


You are serializing it twice.



JObject is already JSON so all you need to do is call .ToString to get the JSON



//...

JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var content = new StringContent(json.ToString(), Encoding.UTF8, contentType);

//...


Reference Write JSON text with JToken.ToString



Another option would be to use an anonymous object and then serialize that



//...

var model = new {
id = "id",
apiKey = "apiKey",
encryptedData = Data
};

var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, contentType);

//...





share|improve this answer























  • Tried it, still doesnt work. I have no idea whats going wrong.
    – Prashant Bhandari
    Nov 21 at 2:07










  • @PrashantBhandari can you show the actual error
    – Nkosi
    Nov 21 at 2:08










  • { "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
    – Prashant Bhandari
    Nov 21 at 2:14










  • This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
    – Prashant Bhandari
    Nov 21 at 2:17
















1














By calling



 var jon = JsonConvert.SerializeObject(json);


You are serializing it twice.



JObject is already JSON so all you need to do is call .ToString to get the JSON



//...

JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var content = new StringContent(json.ToString(), Encoding.UTF8, contentType);

//...


Reference Write JSON text with JToken.ToString



Another option would be to use an anonymous object and then serialize that



//...

var model = new {
id = "id",
apiKey = "apiKey",
encryptedData = Data
};

var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, contentType);

//...





share|improve this answer























  • Tried it, still doesnt work. I have no idea whats going wrong.
    – Prashant Bhandari
    Nov 21 at 2:07










  • @PrashantBhandari can you show the actual error
    – Nkosi
    Nov 21 at 2:08










  • { "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
    – Prashant Bhandari
    Nov 21 at 2:14










  • This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
    – Prashant Bhandari
    Nov 21 at 2:17














1












1








1






By calling



 var jon = JsonConvert.SerializeObject(json);


You are serializing it twice.



JObject is already JSON so all you need to do is call .ToString to get the JSON



//...

JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var content = new StringContent(json.ToString(), Encoding.UTF8, contentType);

//...


Reference Write JSON text with JToken.ToString



Another option would be to use an anonymous object and then serialize that



//...

var model = new {
id = "id",
apiKey = "apiKey",
encryptedData = Data
};

var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, contentType);

//...





share|improve this answer














By calling



 var jon = JsonConvert.SerializeObject(json);


You are serializing it twice.



JObject is already JSON so all you need to do is call .ToString to get the JSON



//...

JObject json = new JObject
{
{ "id", "id" },
{ "apiKey", "apiKey" },
{ "EncryptedData", Data }
};

var content = new StringContent(json.ToString(), Encoding.UTF8, contentType);

//...


Reference Write JSON text with JToken.ToString



Another option would be to use an anonymous object and then serialize that



//...

var model = new {
id = "id",
apiKey = "apiKey",
encryptedData = Data
};

var json = JsonConvert.SerializeObject(model);
var content = new StringContent(json, Encoding.UTF8, contentType);

//...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 1:52

























answered Nov 21 at 1:45









Nkosi

109k16116184




109k16116184












  • Tried it, still doesnt work. I have no idea whats going wrong.
    – Prashant Bhandari
    Nov 21 at 2:07










  • @PrashantBhandari can you show the actual error
    – Nkosi
    Nov 21 at 2:08










  • { "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
    – Prashant Bhandari
    Nov 21 at 2:14










  • This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
    – Prashant Bhandari
    Nov 21 at 2:17


















  • Tried it, still doesnt work. I have no idea whats going wrong.
    – Prashant Bhandari
    Nov 21 at 2:07










  • @PrashantBhandari can you show the actual error
    – Nkosi
    Nov 21 at 2:08










  • { "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
    – Prashant Bhandari
    Nov 21 at 2:14










  • This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
    – Prashant Bhandari
    Nov 21 at 2:17
















Tried it, still doesnt work. I have no idea whats going wrong.
– Prashant Bhandari
Nov 21 at 2:07




Tried it, still doesnt work. I have no idea whats going wrong.
– Prashant Bhandari
Nov 21 at 2:07












@PrashantBhandari can you show the actual error
– Nkosi
Nov 21 at 2:08




@PrashantBhandari can you show the actual error
– Nkosi
Nov 21 at 2:08












{ "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
– Prashant Bhandari
Nov 21 at 2:14




{ "hasError": true, "error": { "code": 601, "developer_message": "input not is JSON format", "user_message": "error message to show to the user" } }
– Prashant Bhandari
Nov 21 at 2:14












This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
– Prashant Bhandari
Nov 21 at 2:17




This is what server returns. If the the hasError is false, I would get the encryptedData back from the server.
– Prashant Bhandari
Nov 21 at 2:17


















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53403702%2fpost-a-json-string-in-xamarin%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'