4

I am trying to prepare a JSON payload to a Post method. The server fails unable to parse my data. ToString() method on my values would not convert it to JSON correctly, can you please suggest a correct way of doing this.

var values = new Dictionary<string, string>
{
    {
        "type", "a"
    }

    , {
        "card", "2"
    }

};
var data = new StringContent(values.ToSttring(), Encoding.UTF8, "application/json");
HttpClient client = new HttpClient();
var response = client.PostAsync(myUrl, data).Result;
using (HttpContent content = response.content)
{
    result = response.content.ReadAsStringAsync().Result;
}
4
  • 1
    Have you looked at what values.ToString() generates? Commented Aug 31, 2017 at 13:16
  • 1
    Look into the JsonConvert class. Commented Aug 31, 2017 at 13:19
  • what does the signature of the server call look like? Commented Aug 31, 2017 at 13:20
  • @fran its a remote api, haven't got a chance to look at it, its a payment gateway that accepts Json only Commented Aug 31, 2017 at 13:21

3 Answers 3

11

You need to either manually serialize the object first using JsonConvert.SerializeObject

var values = new Dictionary<string, string> 
            {
              {"type", "a"}, {"card", "2"}
            };
var json = JsonConvert.SerializeObject(values);
var data = new StringContent(json, Encoding.UTF8, "application/json");

//...code removed for brevity

Or depending on your platform, use the PostAsJsonAsync extension method on HttpClient.

var values = new Dictionary<string, string> 
            {
              {"type", "a"}, {"card", "2"}
            };
var client = new HttpClient();
using(var response = client.PostAsJsonAsync(myUrl, values).Result) {
    result = response.Content.ReadAsStringAsync().Result;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Worth pointing out that both of these require the 3rd party library Json.Net (the latter being an extension method provided by that library). As of posting, the download location for Json.Net is: newtonsoft.com/json
@nkosi, just out of curiosity, which platforms are the ones that allow the use of PostAsJsonAsync?
@MiguelMateo have a look at the API doc learn.microsoft.com/en-us/previous-versions/aspnet/…
Might be also worth to mention that since .NET Core 2.1 there is a built-in constant for "application/json" string so instead of hardcoding we can just use: MediaTypeNames.Application.Json
1

https://www.newtonsoft.com/json use this. there are already a lot of similar topics. Send JSON via POST in C# and Receive the JSON returned?

2 Comments

Thanks, cannot use any other libraries. Will try serializing manually or through PostAsJsonAsync method as @Nkosi suggested.
@RkRBairi Both the options I suggested use Json.Net.
1

values.ToString() will not create a valid JSON formatted string.

I'd recommend you use a JSON parser, such as Json.Net or LitJson to convert your Dictionary into a valid json string. These libraries are capable of converting generic objects into valid JSON strings using reflection, and will be faster than manually serialising into the JSON format (although this is possible if required).

Please see here for the JSON string format definition (if you wish to manually serialise the objects), and for a list of 3rd party libraries at the bottom: http://www.json.org/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.