Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Send data requests http post. **SOLVED**

    Scheduled Pinned Locked Moved LoPy
    http postpostrequest.pyurequest.pyubidots
    40 Posts 7 Posters 22.5k Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      scg94 @livius
      last edited by

      @livius I was confused on line 53, sorry, I already corrected it ...

      The library is https://github.com/micropython/micropython-lib/blob/master/urequests/urequests.py

      And the error line 53 is:

      s = usocket.socket()
      L 1 Reply Last reply Reply Quote 0
      • L Offline
        livius @scg94
        last edited by livius

        @scg94
        are you sure that you are connected to the router?
        And do you have OSError after first good try?
        If you try again after some error request it raise OSError

        for me library work - and i can send POST request
        did you tried

        urlpost.request("POST", "http://itstop.pl/testapi?kind=test&key=secret", '{"temperatura_rpi":' + str(500)+'}').text
        

        but library need some little correction to work with some errors responses
        but if you have good one it success always

        think about people first, then computers

        S 1 Reply Last reply Reply Quote 0
        • S Offline
          scg94 @livius
          last edited by scg94

          @livius Okay, I want you to see this ...

          I was trying to send the data and I got a response:

          >>> urlpost.request("POST", "http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private")
          <Response object at 3ffe5870>
          >>> urlpost.request("POST", "http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}')
          <Response object at 3ffe6250>
          

          However he did not receive anything on the platform. From one moment to another I got an error:

          >>> urlpost.request("POST", "http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}')
          Traceback (most recent call last):
            File "<stdin>", line 1, in <module>
            File "/flash/lib/urequest.py", line 53, in request
          OSError: 23
          

          I have done well the connection with WLAN, because I could enter by FTP and because in the same console I have a response:

          >>> wlan.isconnected()
          True
          >>> wlan.ifconfig()
          ('192.168.1.103', '255.255.255.0', '192.168.1.254', 'Public_IP')
          

          I do not know what else to do...

          L 1 Reply Last reply Reply Quote 0
          • L Offline
            livius @scg94
            last edited by

            @scg94
            try first on e.g.
            https://requestb.in

            create bin and put url

            >>> urlpost.request("POST", "http://requestb.in/1kley5v1", '{"temperatura_rpi":' + str(500)+'}').text
            'ok'
            >>> 
            
            FORM/POST PARAMETERS
            
            None
            HEADERS
            
            Host: requestb.in
            X-Request-Id: b5ff3790-7279-4a4a-8237-b91ac4f26221
            Cf-Ipcountry: PL
            Cf-Ray: 34d8ea4990e56b01-WAW
            Via: 1.1 vegur
            Connection: close
            Total-Route-Time: 0
            Content-Length: 23
            Cf-Visitor: {"scheme":"http"}
            Cf-Connecting-Ip: XXXX
            Connect-Time: 10
            Accept-Encoding: gzip
            RAW BODY
            
            {"temperatura_rpi":500}
            

            think about people first, then computers

            S 1 Reply Last reply Reply Quote 0
            • S Offline
              scg94 @livius
              last edited by scg94

              @livius
              Brother, I just saw the error more clearly...

              >>> urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}').text
              '{"detail": "Unsupported media type \'\' in request."}'
              

              What is it????

              I did the same as you and this was the result:

              >>> urequest.request("POST", "http://requestb.in/1kley5v1", '{"temperatura_rpi":' + str(500)+'}').text
              'ok'
              >>> urequest.request("POST", "http://requestb.in/1kley5v1", '{"temperatura_rpi":500}').text
              'ok'
              
              L 1 Reply Last reply Reply Quote 0
              • L Offline
                livius @scg94
                last edited by livius

                @scg94
                I do not know which media type is supported on your site but
                try to add parameter

                urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}', {"Content-Type": "text/plain"}).text
                

                think about people first, then computers

                S 1 Reply Last reply Reply Quote 0
                • S Offline
                  scg94 @livius
                  last edited by

                  @livius Hiiiii,

                  Yes, that's right, the header is {Content-Type: application / json}. Unfortunately I get error, and this is where everything gets complicated again ...

                  >>> urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}',{"Content-Type":"application/json"}).text
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/flash/lib/urequest.py", line 67, in request
                  AssertionError: 
                  >>> urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}',{"Content-Type : application/json"}).text
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/flash/lib/urequest.py", line 67, in request
                  AssertionError: 
                  >>> 
                  

                  If you want I can send the token for a message so that you help me to make tests and see the error that I get ...

                  L 1 Reply Last reply Reply Quote 0
                  • L Offline
                    livius @scg94
                    last edited by

                    @scg94

                    i miss one parameter
                    try this

                    urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(500)+'}', None, {"Content-Type": "text/plain"}).text
                    

                    think about people first, then computers

                    S 1 Reply Last reply Reply Quote 1
                    • S Offline
                      scg94 @livius
                      last edited by

                      @livius
                      Hey bro! SOLVED

                      >>> urequest.request("POST","http://50.23.124.68/api/v1.6/devices/raspberry-pi?token=private",'{"temperatura_rpi":'+str(30.67)+'}',None,{"Content-Type" : "application/json"}).text
                      '{"temperatura_rpi": [{"status_code": 201}]}'
                      >>>
                      

                      Thanks a Lot...

                      1 Reply Last reply Reply Quote 0
                      • W Offline
                        Wolli01 @danielm
                        last edited by

                        @danielm Hi, I installed your lib, is there a sample code for HTTPS.Post?
                        I then want to get the data from my home page server.

                        PHP Server
                        <?php
                        //Sensor1

                        $p1 = $_GET["p1"]];
                        $p2 = $_GET["p2"];
                        $p3 = $_GET["p3"];

                        $p4 = $_GET["p4"];
                        $p5 = $_GET["p5"];

                        $p6 = $_GET["p6"];
                        $p7 = $_GET["p7"];
                        $p8 = $_GET["p8"];
                        $p9 = $_GET["p9"];
                        $p10 = $_GET["p10"];

                        $p11 = $_GET["p11"];
                        $p12 = $_GET["p12"];
                        $p13 = $_GET["p13"];
                        $p14 = $_GET["p14"];
                        $p15 = $_GET["p15"];

                        $p16 = $_GET["p16"];
                        $p17 = $_GET["p17"];
                        $p18 = $_GET["p18"];
                        $p19 = $_GET["p19"];
                        $p20 = $_GET["p20"];

                        $content = array($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9, $p10, $p11, $p12, $p13, $p14, $p15, $p16, $p17, $p18, $p19, $p20);
                        // Save the file
                        $entry = implode(";", $content);
                        file_put_contents("sensor1.txt", $entry);
                        ?>

                        Translated with www.DeepL.com/Translator

                        D 1 Reply Last reply Reply Quote 0
                        • D Offline
                          danielm @Wolli01
                          last edited by

                          @wolli01
                          Check the test code linked in this post:
                          https://forum.pycom.io/topic/4099/new-firmware-release-candidate-v1-20-0-rc0/12

                          W 1 Reply Last reply Reply Quote 0
                          • W Offline
                            Wolli01 @danielm
                            last edited by

                            @danielm I have tried now but unfortunately only empty strings are written in the file, either no data arrive or they are not read correctly. The PHP file will be called in any case.

                            W 1 Reply Last reply Reply Quote 0
                            • W Offline
                              Wolli01 @Wolli01
                              last edited by

                              @wolli01 Hier mein Code

                              import urequests
                              userdata = {"p1": "Wohnzimmer", "p2": "24.12.2018", "p3": "23.97"}
                              url = 'https://Homepage.de/live1.php'
                              res = urequests.post(url, headers={"Content-Type": "application/json", "Accept": "application/json"}, data= str(userdata))
                              res.close()
                              

                              auf den PHP Server:
                              <?php
                              //Sensor1

                              $p1 = $_GET["p1"];
                              $p2 = $_GET["p2"];
                              $p3 = $_GET["p3"];

                              $inhalt = array($p1, $p2, $p3);
                              // Speichern der Datei
                              $eintrag = implode(";", $inhalt);
                              file_put_contents("sensor1.txt", $eintrag);
                              ?>

                              1 Reply Last reply Reply Quote 0

                              Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                              Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                              With your input, this post could be even better 💗

                              Register Login
                              • 1
                              • 2
                              • 2 / 2
                              • First post
                                Last post

                              Pycom on Twitter

                              Pycom