Juliano Oliveira
Juliano Oliveira

Reputation: 928

Use accented characters with REST API parse

I'm using parse to send push messages. I created a simple tool to allow admins to send messages to the users.

This function works fine, the problem occurs when I pass a string like "Você recebeu uma mensagem" to the pushMessage variable. This occurs because I'm using the character "^". If I use the same string in the parse push panel, the message is sent.

How can I parse the string to prevent this problem in C#?

Parse.cs

  private bool PushNotification(string pushMessage, string title, string canais)
    {
        bool isPushMessageSend = false;

    string postString = "";
    string urlpath = "https://api.parse.com/1/push";
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
    /*postString = "{ \"channels\":"+ canais +", " +
                     "\"data\" : {\"alert\":\"" + pushMessage + "\"}" +
                     "}";*/
   /* postString = "{ \"channels\":"+ canais +", " +
    "\"data\": {\"message\": \"" + pushMessage + "\",\"title\": \"" + title + "\",\"is_background\": false}}";*/
    postString = "{ \"channels\":" + canais + ", " +
   "\"data\": {\"data\": {\"message\": \""+pushMessage+"\",\"title\": \""+title+"\"},\"is_background\": \"false\"}}";

    httpWebRequest.ContentType = "application/json";
    httpWebRequest.ContentLength = postString.Length;
    httpWebRequest.Headers.Add("X-Parse-Application-Id", "xxxxxxxxxxxxxx");
    httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "xxxxxxxxxxxxxxx");
    httpWebRequest.Method = "POST";
    StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
    requestWriter.Write(postString);
    requestWriter.Close();
    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    try
    {
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();

            if(responseText.Contains("true"))
            {
                MessageBox.Show("Mensagem enviada com sucesso");
            }

        }
    }catch(Exception e)
    {
        MessageBox.Show("Problema ao enviar mensagem\nErro:"+e.ToString());
    }

    reset();
    return isPushMessageSend;
}

Upvotes: 0

Views: 2504

Answers (2)

jlvaquero
jlvaquero

Reputation: 8785

I would stick to UTF-8 when working with JSON.

httpWebRequest.ContentType = "application/json; charset=utf-8";

and put the correct ContentLength in bytes which dependes of Encoding format:

httpWebRequest.ContentLength = Encoding.UTF8.GetBytes(postString).Length;
StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream()); //UTF-8 by default

Full example Here

enter image description here

As you can see in the screenShot the "ê" character is enconded in UTF8 with octal values \303\252 and you can see in this table that this correspond to: LATIN SMALL LETTER E WITH CIRCUMFLEX

If the server is not able to parse this then it has a big problem as parsing UTF-8 is like the bread and butter in internet.

Upvotes: 1

Hady Allam
Hady Allam

Reputation: 128

you will need to encode pushMessage before sending it and ask front end developer to decode message property in JSON

Upvotes: 0

Related Questions