Parse push REST API error 400 “Bad Request”

I am developing an app for iOS using Xamarin iOS & MonoGame. I want to use Parse's push notifications through their REST API since the Parse component has push functionality excluded, so first I must create an installation object:

var bundle = new Dictionary<string, object>();
bundle.Add("channels", new [] { "" });
bundle.Add("deviceType", "ios");
bundle.Add("deviceToken", _deviceToken);

string urlpath = "https://api.parse.com/1/installations";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("X-Parse-Application-Id", _parseAppID);
httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", _parseRestAPIKey);
httpWebRequest.Method = "POST";
string bundleString = bundle.ToJson();
byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(bundleString);
string result = Convert.ToBase64String(buffer);
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
requestWriter.Write(result, 0, result.Length);
requestWriter.Flush();
requestWriter.Close();
WebResponse httpResponse = await httpWebRequest.GetResponseAsync();

Stream stream = httpResponse.GetResponseStream();
string json = string.Empty;

using (StreamReader reader = new StreamReader(stream))
{
    while (!reader.EndOfStream)
    {
        json += reader.ReadLine();
    }
}

JsonObject jsonObject = JsonObject.Parse(json);
_varStorage.Save("ObjectId", jsonObject.Get<string>("objectId"));

The inspected bundleString value is:

"{\"channels\":[\"\"],\"deviceType\":\"ios\",\"deviceToken\":\"46becd0a165be042eeab5a1ec96b8858065cbea7311479da16c0fd1c9428e2eb\"}"

This code raises a System.Net.WebExceptionStatus.ProtocolError error 400 "Bad Request", he content is '"code":107,"error":"invalid json'.

As for the device token, when I receive the token in 'RegisteredForRemoteNotifications' method, I process it by removing brackets and spaces 'deviceToken.ToString().Trim('<', '>').Replace(" ", "")' then I use it. I am not sure parse requires this but I do it anyhow. I tried keeping the brackets and spaces, and I still get the same error.

I can't understand why I am getting this error.

Posts

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    Have you asked on the Parse forums?

    https://parse.com/help

  • RamsayAmarinRamsayAmarin JOMember

    Well they don't have their own forums anymore, its basically a redirect to Stack Overflow, and I did post there, but the responses stopped and no solution was found.

  • JasonAwbreyJasonAwbrey USInsider, University, Developer Group Leader mod

    They have a Google forum.

    Do the curl samples work for you? Have you tried construction the json by hand, or using a different json library?

    Does this code work for you? I know it's not the exact same endpoint, but they are using the REST API from C#. The two things that stand out to me are that they are building their json manually, and are setting the content-length header.

  • RamsayAmarinRamsayAmarin JOMember
    edited July 2014

    After more trail and error, I found that replacing this

    byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(bundleString);
    string result = Convert.ToBase64String(buffer);
    StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
    requestWriter.Write(result, 0, result.Length);
    requestWriter.Flush();
    requestWriter.Close();
    

    with this

    httpWebRequest.ContentLength = bundleString.Length;
    StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
    requestWriter.Write(bundleString);
    requestWriter.Flush();
    requestWriter.Close();
    

    fixed the problem, I don't know why though.

  • TeoTeo DKMember ✭✭

    @RamsayAmarin‌ Hey, could you give some more info on how you setup your project to receives pushes? Especially how you set up permission and the like. Thanks.

Sign In or Register to comment.