none
How to making a post using custom headers RRS feed

  • Question

  • How to send the following json, in a post, with the headers?

    {  
       "from":"WineShop",
       "to":[  
          "5531984882884",
          "5531984882886"
       ],
       "text":"Wine shop grand opening at Monday 8pm. Don't forget glasses."
    }

    Headers Authorization and Content-Type.

    My code:


    public static async System.Threading.Tasks.Task SendSMSToKeeperAsync(List<string> numbers, string usuario)
            {

                var request = new HttpClient();
                int count = 0;
                string keerpersNumbers = "";
                string urlSMS = "https://myurl";
                string authHeader = "Basic adsfds=";
                string contentTypeHeader = "application/json";

                if (numbers != null && numbers.Count > 0)
                {
                    foreach (string num in numbers)
                    {
                        //keerpersNumbers = num;
                        if (count > 0)
                        {
                            keerpersNumbers = keerpersNumbers + "," + num;
                        }
                        else
                        {
                            keerpersNumbers = num;
                        }
                        count++;
                    }

                    if (usuario == null && usuario.Equals(""))
                    {
                        usuario = "app";
                    }

                }

                string myJson = "{from:" + usuario + "," + "to:[" + keerpersNumbers + "],text:Some text}";           
                string contents = JsonConvert.SerializeObject(myJson);
                request.DefaultRequestHeaders.Add("Authorization", authHeader);           
                var response = await request.PostAsync(urlSMS, new StringContent(contents, System.Text.Encoding.UTF8, "application/json"));
    }



    Monday, May 6, 2019 7:06 PM

Answers

  • Your JSON conversion logic is incorrect. JsonConvert takes a .NET object and converts it to JSON format. You're taking a JSON-formatted string and trying to convert it to JSON but it is already converted so you get a bad encoding. Go one way or the other, not both.

    //keerpersNumbers isn't needed anymore
    var data = new { from = usuario, to = numbers, text = "Some Text" };

    var contents = JsonConvert.SerializeObject(data);

    I'm not showing how you'd convert your raw data to JSON and then forego the need for JsonConvert because I don't believe it is the clean approach you need here.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 7, 2019 1:45 PM
    Moderator

All replies


  • Hi   Henrique, 
     
    >>How to send the following json, in a post, with the headers?

    We can use the following code to send a json.
    string contents = JsonConvert.SerializeObject(Object);
    var response = httpClient.PostAsync(uri, new StringContent(contents, Encoding.UTF8, "application/json"));

    You can use the following code to add headers.

    request.DefaultRequestHeaders.Add(authHeader,contentTypeHeader);

    If I have any misunderstanding, you can describe it clearly with a detailed description about the results including any exception. This will help us quickly analyze your problem.
                    
    Besides, what's the type of your application? Winform? WPF? ASP.NET?


    Best Regards

    Yong Lu


    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, May 7, 2019 10:07 AM
    Moderator

  • Hi   Henrique, 
     
    >>How to send the following json, in a post, with the headers?

    We can use the following code to send a json.
    string contents = JsonConvert.SerializeObject(Object);
    var response = httpClient.PostAsync(uri, new StringContent(contents, Encoding.UTF8, "application/json"));

    You can use the following code to add headers.

    request.DefaultRequestHeaders.Add(authHeader,contentTypeHeader);

    If I have any misunderstanding, you can describe it clearly with a detailed description about the results including any exception. This will help us quickly analyze your problem.
                    
    Besides, what's the type of your application? Winform? WPF? ASP.NET?


    Best Regards

    Yong Lu


    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.

    Hi!

    I'm using C # .NET, I use this code, for the backend of an app. This method is an internal method. Testing json on postman and url with these headers, is correct, I receive SMS.

    I made the changes I suggested and made an edit in the code, but still, it did not work. The format of JSON after serialization is this:

    "\"{from:henrique,to:[5531984882886],text:Some text}\""

    I edited the question, with the new code.

    a few seconds ago

    Tuesday, May 7, 2019 11:49 AM
  • Your JSON conversion logic is incorrect. JsonConvert takes a .NET object and converts it to JSON format. You're taking a JSON-formatted string and trying to convert it to JSON but it is already converted so you get a bad encoding. Go one way or the other, not both.

    //keerpersNumbers isn't needed anymore
    var data = new { from = usuario, to = numbers, text = "Some Text" };

    var contents = JsonConvert.SerializeObject(data);

    I'm not showing how you'd convert your raw data to JSON and then forego the need for JsonConvert because I don't believe it is the clean approach you need here.


    Michael Taylor http://www.michaeltaylorp3.net

    Tuesday, May 7, 2019 1:45 PM
    Moderator