0

I want to send Post Request with C# WebClient with this Json Schema :

 [
 {
 "id": "00000000-0000-0000-0000-000000000000",
 "points": [
 {
 "timestamp": "2017-12-04T16:07:44.562Z",
 "value": 0
 }
 ]
 }
]

I've tried This :

public class RequestData
{
 public string id {get; set; }
 public points points { get; set; }
}
public class points
{
 public DateTime timestamp { get; set; }
 public float value { get; set; }
}

My Program :

Random number = new Random();
var req = new RequestData();
req.id = "0e13d9c-571c-44f4-b796-7c40c0e20a1d";
req.points = new points { timestamp = DateTime.UtcNow, value = 
number.Next(100, 99999) };
JsonSerializerSettings settings = new JsonSerializerSettings();
var data = JsonConvert.SerializeObject(req);
WebClient client = new WebClient(); 
client.Headers.Add(HttpRequestHeader.Authorization, 
AquaAtuhorization.accessToken);
client.Headers.Add(HttpRequestHeader.ContentType, "application/json"); 
client.UploadString ("http://localhost:8080/api/v1/data/writeNumericValues", 
data ); 

And I always get Http 415 (Unsupported Media Type).

How could I format my C# Object as the restApi accepeted Format.

asked Dec 4, 2017 at 17:07

2 Answers 2

1

Look at the JSON, the square brackets [ ] denote that something is an array. In this case both RequestData and points have to be an array, see the example below:

public class RequestData
{
 public string id { get; set; }
 public List<points> points { get; set; } // I use list, could be an array
}
public class points
{
 public DateTime timestamp { get; set; }
 public float value { get; set; }
}

Then construct your req object like this:

var req = new List<RequestData> // Again I use list, could be an array
{
 new RequestData
 {
 id = "0e740d9c-571c-44f4-b796-7c40c0e20a1d",
 points = new List<points> // Defined as a list, even though it has one entry
 {
 new points
 {
 timestamp = DateTime.UtcNow,
 value = number.Next(100, 99999)
 }
 }
 }
};

Then just serialize it as normal, the result will be the below:

[
 {
 "id":"0e740d9c-571c-44f4-b796-7c40c0e20a1d",
 "points":[
 {
 "timestamp":"2017-12-04T17:12:25.8957648Z",
 "value":59522.0
 }
 ]
 }
]
answered Dec 4, 2017 at 17:17

Comments

0

Your Json class needs to be like this, see http://json2csharp.com/ or use paste as JSON from VS https://blog.codeinside.eu/2014/09/08/Visual-Studio-2013-Paste-Special-JSON-And-Xml/

public class Point
{
 public DateTime timestamp { get; set; }
 public int value { get; set; }
}
public class RootObject
{
 public string id { get; set; }
 public List<Point> points { get; set; }
}`
answered Dec 4, 2017 at 17:11

2 Comments

Thank you Saj, but i still get the same Http 415 error
use fiddler and check if it's setting a content type header, also check if your API is wanting a string

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.