4

I am sending post request with AT commands using sim800 module using python

g = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2)
g.write('AT+HTTPPARA="URL","http://server/path"')
print g.read(128)

OK

g.write('AT+HTTPPARA="CONTENT","application/json"')
print g.read(128)

OK

g.write('AT+HTTPDATA=150,5000'+'\r\n')
print g.read(128)

DOWNLOAD OK

g.write("{\"data\":\"123\"}"+'\r\n')
print g.read(128)

{"data":"123"} ERROR

g.write('AT+HTTPACTION=1' + '\r\n')  # -> POST session start
print g.read(128)

+HTTPACTION:1,400,31

I want to send as json format but when i execute this commands using python it shows outputs like this its not taking json format i have tried using json.loads(json.dumps(data)) but it still not sending to server on server side i wont get data in request.body Object (as per post request)

How to send data in json format in post data ?? or which configuration needed to post data ?

NOTE: I have already configured GSM module for GPRS

Krunal Sonparate
  • 1,122
  • 10
  • 29
  • Your code will **NEVER** work reliably when you are not [reading and parsing](http://stackoverflow.com/a/33266839/23118) the response given back from the modem, waiting for the Final result code before sending the next command. You are just extremely lucky if this works most of the time. You should definitely fix that. – hlovdal Apr 18 '17 at 20:22

1 Answers1

3

Solved using json.dumps() to convert json data to string and to get on server side in request.body object

data = {"param":"value","param2":0.01}
g = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=2)
g.write('AT+HTTPPARA="URL","http://server/path"')
print g.read(128)

OK

g.write('AT+HTTPPARA="CONTENT","application/json"')
print g.read(128)

OK

g.write('AT+HTTPDATA=1500,5000'+'\r\n')
print g.read(128)

DOWNLOAD OK

g.write(json.dumps(data))
print g.read(128)

OK

g.write('AT+HTTPACTION=1' + '\r\n')  
print g.read(128)

+AT+HTTPACTION:1,200,31

Krunal Sonparate
  • 1,122
  • 10
  • 29