0
votes

I have a REST webservice that accepts feedback from a Windows 8.1 app. All works well until a user inserts an emoticon into the text.

Example: this is awesome!! ????

When this happens I get:{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Thu, 16 Jul 2015 21:05:54 GMT Server: Microsoft-IIS/10.0 X-Powered-By: ASP.NET Content-Length: 1806 Content-Type: text/html }}

I have no idea why this is happening.

Here is my code for the service:

    [OperationContract]
    [WebInvoke(
        Method = "POST", 
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        UriTemplate = "PostFeedback")]
    void CaptureFeedback(feedback tmpFeedback);

The processing code:

    public void CaptureFeedback(feedback myFeedBack)
    {
        using (dbemployeedirectoryEntities entities = new dbemployeedirectoryEntities())
        {
            feedback tmpFeedback = new feedback();

            tmpFeedback.FeedbackGUID = System.Guid.NewGuid().ToString();
            tmpFeedback.Rating = myFeedBack.Rating;
            tmpFeedback.FeedbackMessage = myFeedBack.FeedbackMessage;
            tmpFeedback.TimeStamp = myFeedBack.TimeStamp;
            tmpFeedback.Hostname = myFeedBack.Hostname;

            entities.feedbacks.Add(myFeedBack);

            entities.SaveChanges();
        }
    }

The class:

    public partial class feedback
    {
       public string FeedbackGUID { get; set; }
       public Nullable<int> Rating { get; set; }
       public string FeedbackMessage { get; set; }
       public Nullable<System.DateTime> TimeStamp { get; set; }
       public string Hostname { get; set; }
    }

I am encoding the request as UTF8:

HttpResponseMessage myResponse = null;

        var myClient = new HttpClient();

        myClient.BaseAddress = new Uri(BaseAddress);

        var content = new StringContent(myJSON, Encoding.UTF8, "application/json");

        try
        {
            myResponse = await myClient.PostAsync(myURI, content);
            return myResponse;
        }
        catch (Exception ex)
        {
            throw ex;
        }

Any ideas?

1
The client app will need to escape emoticon strings which are usually things like :< :> :} :P etc. Some of the characters will create a bad request if not properly escaped.Namphibian

1 Answers