0

I'm trying to send a post request with a json string of an object I have created. However after serializing my c# object and posting I get a 400 error (problems parsing JSON)

I'm using the Newtonsoft.Json dll to serialize my object. Here is the object I am serializing:

public class CreateRepository
{
    [JsonProperty("name")]

    public string Name { get; set; }

    [JsonProperty("description")]

    public string Description { get; set; }

    [JsonProperty("homepage")]

    public object Homepage { get; set; }

    [JsonProperty("gitignore_template")]
    public string GitIgnoreTemplate { get; set; }

    [JsonProperty("license_template")]
    public string LicenceTemplate { get; set; }

    [JsonProperty("private")]

    public bool Private { get; set; }

    [JsonProperty("has_projects")]
    public bool HasProjects { get; set; }

    [JsonProperty("has_issues")]
    public bool HasIssues { get; set; }

    [JsonProperty("has_template")]
    public bool HasTemplate { get; set; }

    [JsonProperty("has_wiki")]
    public bool HasWiki { get; set; }
}

I am then serializing an instance of the object like so:

var content = JsonConvert.SerializeObject(repository);

This then generates the json string below:

{
    \"name\": \"Test\",
    \"description\":null,
    \"homepage\":null,
    \"gitignore_template\":null,
    \"license_template\":null,
    \"private\":false,
    \"has_projects\":false,
    \"has_issues\":false,
    \"has_template\":false,
    \"has_wiki\":false}
}

I get the following back after attempting to post the request:

{
    "message": "Problems parsing JSON",
    "documentation_url": "https://developer.github.com/v3/repos/#create"
}

Does anyone know why my object has been serialized in this way?

Update:

I can deserialize the object using JSON.Net without any errors.

The url I'm posting to is as follows:

https://api.github.com/user/repos

this is how I'm sending the request:

var response = await _httpClient.PostAsJsonAsync("user/repos", content);
7
  • 2
    Please provide a minimal reproducible example rather than just snippets... and check whether the JSON string actually contains those backslashes, or whether that's just what you're seeing in the debugger. The extra brace in that JSON looks dodgy too... it'll be much easier to help when we can reproduce this ourselves. Commented Aug 10, 2019 at 11:30
  • Can you deserialize the string in same application that you serialized the object? If so then either one of two things a happening 1) The server did not get the entire json string 2) The object CreateRepository is defined differently on the server. Every time you change CreateRepository on client you must change the definition on the server and recompile. Commented Aug 10, 2019 at 11:35
  • @JonSkeet I will update my question shortly Commented Aug 10, 2019 at 11:43
  • @jdweng I'll give it a try and update accordingly Commented Aug 10, 2019 at 11:43
  • 3
    try without JsonConvert.SerializeObject(repository); and just say var response = await _httpClient.PostAsJsonAsync("user/repos", repository); Commented Aug 10, 2019 at 12:34

1 Answer 1

1

The problem is that "has_template" is unknown at Github side and probably should be "is_template". See the link you got back in the response of your request for the correct parameter names. So you are not using the correct property names/name attributes in your Class. Serialization is not the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Well spotted, the main issue was that I was serializing the Json string twice causing issues as after this was updated I got successfull post requests - but that is also an issue too I hadn' noticed which would have cropped up when posting that parameter/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.