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 11, 2019 in IoT (Internet of Things) by Bharani
• 4,660 points
1,424 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 11, 2019 by nirvana
• 3,130 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,240 points
947 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,250 points
2,300 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 22, 2018 in IoT (Internet of Things) by Shubham
• 13,490 points
761 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 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,563 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,240 points
1,010 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,130 points
1,125 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,130 points
927 views
0 votes
1 answer

Access Serial Ports in Windows 10 IoT App

On Windows IoT you have to use ...READ MORE

answered Oct 10, 2018 in IoT (Internet of Things) by anonymous2
• 4,240 points
2,173 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 10, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,699 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP