Question

I am getting a 400 bad request error when trying to connect to the Urban Airship Rest API. Below is the curl command I am trying to replicate in .NET. The .NET code is at the end. Please help.

curl -v -X POST 
-u "username:passowrd" 
-H "Content-type: application/json" 
-H "Accept: application/vnd.urbanairship+json; version=3;" 
--data '{"audience" : {"tag":"1_13_98"},                  
       "device_types" : "all",                    
       "notification" : {"alert": "Tag push alert"}       
  }' 
  https://go.urbanairship.com/api/push

The c# code I am trying to use is:

    var json = gcm.ToJsonString();
    Console.WriteLine("JSON GCM Message: " + json);
    var uri = new Uri("https://go.urbanairship.com/api/push/?");
    var encoding = new UTF8Encoding();
    var request = (HttpWebRequest)WebRequest.Create(uri);

    request.Method = "POST";
    request.Credentials = new NetworkCredential(username, master);
    request.ContentType = "application/json";

    WebHeaderCollection myWebHeaderCollection = request.Headers;
    myWebHeaderCollection.Add(HttpRequestHeader.Accept, "application/vnd.urbanairship+json; version=3;");

    request.ContentLength = encoding.GetByteCount(json);

    using (var stream = request.GetRequestStream())
    {
        stream.Write(encoding.GetBytes(json), 0, encoding.GetByteCount(json));
        stream.Close();
        var response = request.GetResponse();
        response.Close();
    }

    return true;

The API for urbanAirship can be found here: http://docs.urbanairship.com/reference/api/v3/push.html

And here is an example request..

POST /api/push HTTP/1.1
Authorization: Basic <master authorization string>
Content-Type: application/json
Accept: application/vnd.urbanairship+json; version=3;
{
  "audience" : {
      "device_token" : "998BAD77A8347EFE7920F5367A4811C4385D526AE42C598A629A73B94EEDBAC8"
  },
  "notification" : {
       "alert" : "Hello!"
  },
  "device_types" : "all"
}
Was it helpful?

Solution 2

Instead of using the header collection (which is throwing an exception) try setting the Accept property on the Request object, e.g. request.Accept = "application/vnd.urbanairship+json; version=3;"

If you still get a 400, try looking in the response body for more details.

OTHER TIPS

I've created a c# .Net library for talking to Urban Airship API V3

You can find it here:

https://github.com/JeffGos/urbanairsharp

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top