none
BadRequest when using AppCenter Push RRS feed

  • Question

  • Hi,

    I am getting Bad Request in this error:

    StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
      Transfer-Encoding: chunked
      Connection: keep-alive
      Access-Control-Allow-Origin: *
      Strict-Transport-Security: max-age=15724800; includeSubDomains
      Date: Fri, 07 Jun 2019 20:43:54 GMT
      Server: nginx/1.15.8
      Content-Type: application/json; charset=utf-8
    }

    when I try to send a Push Notification although everything works perfect when I pass the same parameters on the:

    https://openapi.appcenter.ms/#/push/Push_Send

    The problem occurred when I added the NotificationTarget and if I remove the NotificationTarget then things works fine but in my case I need to define the target

    Kindly help..

    Here is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Text.RegularExpressions;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Security.Cryptography;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Collections.ObjectModel;
    using System.Windows.Forms;
    
    namespace AppCenterTest
    {
        public partial class Form1 : Form
        {
            public class NotificationContent
            {
                [JsonProperty("name")]
                public string Name { get; set; }
    
                [JsonProperty("title")]
                public string Title { get; set; }
    
                [JsonProperty("body")]
                public string Body { get; set; }
            }
    
            public class NotificationTarget
            {
                [JsonProperty("type")]
                public string Type { get; set; }
    
                [JsonProperty("devices")]
                public string Devices { get; set; }
            }
    
            public class PostObject
            {
                [JsonProperty("notification_content")]
                public NotificationContent NotificationContent { get; set; }
    
                [JsonProperty("notification_target")]
                public NotificationTarget NotificationTarget { get; set; }
            }
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private async void Button1_Click(object sender, EventArgs e)
            {
                using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
                {
                    PostObject postObject = new PostObject
                    {
                        NotificationTarget = new NotificationTarget
                        {
                            Type = "devices_target",
                            Devices = "dc180bc9-fccc-4439-89a6-55ec4a4e333e"
                        },
                        NotificationContent = new NotificationContent
                        {
                            Name = "General",
                            Title = "Using Push API",
                            Body = textBox1.Text
                        }
                    };
    
                    var myContent = JsonConvert.SerializeObject(postObject);
                    client.DefaultRequestHeaders.Add("X-API-Token", "xxxxxxxxxxxxxxxxxxxxxxxxxxx");
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    var builder = new UriBuilder(new Uri("https://api.appcenter.ms/v0.1/apps/SXXXXXXXX/ZXXXXXX/push/notifications"));
    
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
                    request.Content = new StringContent(myContent, Encoding.UTF8, "application/json");
    
                    HttpResponseMessage response = await client.SendAsync(request);
    
                    textBox2.Text = Convert.ToString(response);
                };
            }
        }
    }
    

    Thanks,

    Jassim

    Friday, June 7, 2019 8:49 PM

All replies

  • Hi Jassim Rahma,

    Thank you for posting here.

    For your question, what is the error message? Why do you must define this? Could you provide more details for me to test the code?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 10, 2019 2:26 AM
    Moderator
  • This is teh error I am getting:

    StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
      Transfer-Encoding: chunked
      Connection: keep-alive
      Access-Control-Allow-Origin: *
      Strict-Transport-Security: max-age=15724800; includeSubDomains
      Date: Mon, 10 Jun 2019 10:41:23 GMT
      Server: nginx/1.15.8
      Content-Type: application/json; charset=utf-8
    }

    Monday, June 10, 2019 10:41 AM
  • Hi Jassim Rahma,

    I have seen that in your description, the message you provided do not provide the more details about the error.

    Based on my search, the link below has the similar issue with yours. Please check it.

    https://integrate.hubspot.com/t/statuscode-400-and-bad-request-with-c-when-call-createorupdate-contact/249/4

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 11, 2019 8:45 AM
    Moderator