i have a server with POST e REST services. Using micropython i need to perform POST request.
GET is performed and respond correctly using the following format:
s.send(b"GET /sensordata/premises HTTP/1.1\r\nHost:XX.XXX.XXX.XXX\r\n" + "Accept: application/json\r\n\r\n")
but for the POST looks like the request arrived at server, but the payload body is empty. For some reason, the JSON body of the request is not interpreted in the correct way.
Code: Select all
from network import WLAN
import socket
import machine
import time
import struct
import json
import pycom
wlan = WLAN(mode=WLAN.STA)
wlan.connect("*****", auth=(WLAN.WPA2, "*****"), timeout=5000)
while not wlan.isconnected():
machine.idle()
print("Connected to WiFi\n")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
url = 'XX.XXX.XXX.XXX'
sockaddr = socket.getaddrinfo(url, 80) [0][-1]
s.connect(sockaddr)
print('socket connected')
httpreq = b'POST /sensordata/insertrecords HTTP/1.1 \r\n Host:XX.XXX.XXX.XXX \r\n' + 'Accept: application/json \r\n' + 'Content-Type: application/json\r\n\r\n' + ' {\"node_id\":\"1\",\"value\":[{\"measure_time_stamp\":\"2020-10-06T09:25:43\",\"temp\":\"14\",\"humidity\":\"75\",\"ph1\":11,\"ph2\":12,\"ph3\":13}]}'
s.send(httpreq)
time.sleep(1)
rec_bytes = s.recv(4096)
print("RESPONSE = " + str(rec_bytes))
print('end')
s.close()