asp.net - send Http request with Content Type application Json on C# -


am trying send http request content type application/json via c#. don't find how create request. http request it's this:

post /messaging/registrations/(registration_id_for_destination_app_instance)/messages http/1.1 host: api.amazon.com authorization: bearer (my_access_token) content-type: application/json x-amzn-type-version: com.amazon.device.messaging.admmessage@1.0 accept: application/json x-amzn-accept-type: com.amazon.device.messaging.admsendresult@1.0  {     "data":{"key1":"value1","key2":"value2"},     "consolidationkey":"some key",     "expiresafter":86400 } 

someone can me, please. thinks all.

assuming have class represent payload,

class payload {         public dictionary<string, string> data { get; set; }         public string consolidationkey { get; set;}         public long expiresafter { get; set; } } 

you can use httpclient, this.

string url = "http://api.amazon.com/messaging/registrations/1234/messages"; var client = new httpclient();  client.defaultrequestheaders.authorization = new authenticationheadervalue(                                               "bearer", "token"); client.defaultrequestheaders.accept.add(               new mediatypewithqualityheadervalue("application/json")); client.defaultrequestheaders.add("x-amzn-type-version",                                    "com.amazon.device.messaging.admmessage@1.0"); client.defaultrequestheaders.add("x-amzn-accept-type",                                 "com.amazon.device.messaging.admsendresult@1.0");  var kvp = new dictionary<string, string>(); kvp.add("key1", "value1"); kvp.add("key2", "value2");  var payload = new payload() {     consolidationkey = "some key", expiresafter = 86400, data = kvp };  var result = client.postasjsonasync<payload>(url, payload).result; 

Comments