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);
JsonConvert.SerializeObject(repository);and just say var response =await _httpClient.PostAsJsonAsync("user/repos", repository);