Sending a POST in UWP using C#

0 votes

I'm working with Windows 10 IoT in Universal Windows Platform (UWP) and I have to send a POST method, but it keeps saying invalid JSON content. Can anyone please tell me what I could be doing wrong in the JSON format? Any help is much appreciated. Thanks a lot!

Uri resourceAddress;
        if (!Helpers.TryGetUri(pcHost + pcPort + "/api/code/scan", out resourceAddress))
        {
            rootPage.NotifyUser("Invalid URI.", NotifyType.ErrorMessage);
            return;
        }
        try
        {
            terminalRef = "1";
            //code = "Uc0E17G4nW";
            IHttpContent jsonContent = new HttpJsonContent(JsonValue.Parse("{\"code\":\"" + code +
                                                                            ",\"ref\" : \""+ terminalRef +
                                                                            "\"}"));


            HttpResponseMessage response = await httpClient.PostAsync(resourceAddress, jsonContent).AsTask(cts.Token);
            Debug.WriteLine(".");
            //await Helpers.DisplayTextResultAsync(response, cts.Token);
            rootPage.NotifyUser("Completed", NotifyType.StatusMessage);

        }
        catch (Exception ex)
        {
            rootPage.NotifyUser("Error: " + ex.Message, NotifyType.ErrorMessage);
            String errorMessage = ex.Message.ToString();
        }
Jan 10 in IoT (Internet of Things) by Bharani
• 4,550 points
104 views

1 answer to this question.

0 votes

I couldn't find any proper documentation and that made this difficult, but I think I've finally got it to work. 

Here's how:

              Uri resourceAddress;

            if (!Helpers.TryGetUri(Host + Port + "/XXX/YYY/directory", out resourceAddress))
            {
                return;
            }

            IHttpContent jsonContentCoordinates = new HttpJsonContent(JsonValue.Parse("{\"zzz\": \"" + something
                                                                              + "\", \"xxx\": \"" + somethingXXX
                                                                              + "\",\"yyy\": \"" + somethingYYY
                                                                              + "\" }"));
            HttpResponseMessage httpResponseCoordinates = new HttpResponseMessage();
            string httpResponseBodyCoordinates = "";
            try
            {
                httpResponseCoordinates = await httpClient.PostAsync(resourceAddress, jsonContentCoordinates).AsTask(cts.Token);
                httpResponseBodyCoordinates = await httpResponseCoordinates.Content.ReadAsStringAsync();
                httpResponseCoordinates.EnsureSuccessStatusCode();
                FlagInternetNotConnected = false;

            }
            catch (Exception)
            {
             //Catch it if it fails.

            } 
answered Jan 10 by nirvana
• 3,060 points

Related Questions In IoT (Internet of Things)

0 votes
1 answer

Using FIWARE to start Cygnus as a Service in IoT Solution

Cygnus is supposed to create /var/run/cygnus/ when started. You ...READ MORE

answered Aug 13, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
84 views
0 votes
1 answer

Using Sleep()/Delay() in C# on Windows IoT

You'll be glad to know that C# ...READ MORE

answered Aug 28, 2018 in IoT (Internet of Things) by DataKing99
• 8,130 points
349 views
0 votes
1 answer

Detecting when a sensor is not sending data to Orion CB in FiWARE

Orion in itself has no such mechanism ...READ MORE

answered Nov 21, 2018 in IoT (Internet of Things) by Shubham
• 13,300 points
34 views
0 votes
1 answer

Display time in a Windows Core IoT app with a clock!

It is possible, but you should understand ...READ MORE

answered Jul 9, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
120 views
0 votes
1 answer

Configuring a Windows 10 IoT App for Automatic Restart

Have you tried using powershell right after ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
48 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
164 views
0 votes
1 answer

Finding the Unicast, DNS and Gateway Address in UWP

Try the PInvoke api methods from Iphlpapi.dll. ...READ MORE

answered Aug 20, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
57 views
0 votes
1 answer

Access Serial Ports in Windows 10 IoT App

On Windows IoT you have to use Windows.Devices.SerialCommunication namespace ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,260 points
75 views
0 votes
1 answer
0 votes
1 answer

Setting-up a RFID RC522 chip in Raspberry Pi?

First, let me congratulate you on buying ...READ MORE

answered Jul 9, 2018 in IoT (Internet of Things) by nirvana
• 3,060 points
178 views