Send data requests http post. **SOLVED**



  • Hello everyone,

    I'm Santiago from Colombia...

    ¿How can I send data requests http POST, from LoPy to other web server?.

    Thank you.
    Regards.



  • @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...



  • @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
    


  • @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 ...



  • @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
    


  • @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'
    


  • @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}
    


  • @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...



  • @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



  • @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()


  • @scg94
    which library do you finally use?
    I supposed that from first post @bucknall - but there is not line like you show



  • @livius

    I already tried with both, look at the error...

    >>> urequest.request("POST","http://50.23.124.68/api/v1.6/devices/rasxxxxx?token=xxxxxxxxxxx",'{"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
    >>> 
    

    And in the line 53 in library urequest.py is:

    s = usocket.socket()
    

    Thanks for your help...



  • @scg94 said in Send data requests http post.:
    third parameter must be buffered
    e.g. string or bytes
    you can not send dictionary

    {"temperatura_rpi": bytes(500)}
    

    do you try to send string?
    if yes then concat it

    '{"temperatura_rpi": 500}'
    

    or if you have int variable

    '{"temperatura_rpi": ' + str(int_var) + '}'
    


  • @livius Hi,

    I tried with this and look at the result ...

    >>> datos={"temperatura_rpi": 500}
    >>> data=bytes([datos])
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cant convert dict to int
    >>> urequest.request("POST" ,"http://50.23.124.68/api/v1.6/devices/raspxxx?token=xxxxxxxxx",{"temperatura_rpi": bytes(500)})
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/flash/lib/urequest.py", line 74, in request
    TypeError: object with buffer protocol required
    >>>
    

    Can you help me?? Thank u...



  • @scg94
    cast data as bytes

    urequest.request("POST", url, bytes([your_data])
    

    or create it at start as buffered



  • @bucknall Hello again,

    I think I'm slowly solving things, what I did was change the domain name by IP, and now it works, but I have an error again with the library, this time it speaks of: "TypeError: object with buffer protocol required"...

    0_1491834052837_Error.PNG

    Do you know what it is about?



  • @bucknall @RadioTek @jmarcelino

    Hello everyone,

    I keep trying to send the requests but I still can not, I want you to see this that came out...

    0_1491830116867_Ping.PNG

    I do not know how to send requests, I even tried connecting the LoPy to the Raspberry through Tx and Rx but I also did not find positive result.

    I really need help! Thank you...



  • @jmarcelino said in Send data requests http post.:

    os.uname()

    (sysname='LoPy', nodename='LoPy', release='1.6.10.b1', version='v1.8.6-556-g989d5ac9 on 2017-03-30', machine='LoPy with ESP32', lorawan='1.0.0'

    Yes connection to my mobile network was via WIFI using phone as AP

    My Laptop "as using now" is worked through my office WIFI network back in to ADSL2. DNS functioning of all computers on the network seems perfect.

    Regards Alex



  • @RadioTek
    Thank you for posting those. Just to be sure you're running the latest firmware on the LoPy?

    os.uname()

    Also when you connected to the mobile network was that using a mobile phone as the WiFi AP?



  • @jmarcelino Hi ,
    The result of print(wlan.ifconfig()) is ('10.1.1.37','255.255.255.0','10.1.1.1','8.8.4.4')
    I have set the WIFI to a static address. I have tried 10.1.1.1, 8.8.8.8, 8.8.4.4, and
    my providers DNS address, and all return the same result.
    addr = socket.getaddrinfo('http://www.checkthat.com.au', 80)[0][-1]
    print(addr) # returns ('200.249.64.63',80')
    Then entering the known IP address for "www.checkthat.com.au"
    addr = ('116.0.20.2',80)
    print(addr) # returns ('116.0.20.2',80)
    sa = socket.socket()
    sa.connect(addr)
    sa.send(b'GET / HTTP/1.1\r\nHost: checkthat.com.au\r\n\r\n')
    data = sa.recv(1000)
    sa.close()
    print(data) # returns the header information of the page

    I have tried this on my mobile network as well, with alternate IP address's
    wlan.ifconfig(config=('192.168.43.10', '255.255.255.0', '192.168.43.1', '192.168.43.1')) and it has behaved exactly the same way.

    Thanks from Alex



Looks like your connection to Pycom Forum was lost, please wait while we try to reconnect.