POST api Rest with AT Commands - SIM800 - http

I'm having problems sending post parameters over a SIM800 module using AT. For while I'm using FTDI and mac screen application terminal.
Here is my command sequence:
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","myhost/deviceRegister"
AT+HTTPPARA="CONTENT","application/json"
AT+HTTPDATA=94,120000
OK
DOWNLOAD
POST deviceRegister HTTP/1.1
Host: myhost
Content-Length:13
{"IMEI":"aa"}
AT+HTTPACTION=1
AT+HTTPREAD
Until here, everything looks fine, but when I log my post request in my server what I have is this:
+HTTPREAD: 259
POST deviceRegister HTTP/1.1
Host: myhost
Accept: */*
Connection: Keep-Alive
Content-Type: application/json
User-Agent: SIMCOM_MODULE
Content-Length: 94
{"IMEI":"aa"}gister HTTP/1.1
OK
It looks like it is posting to the body without \n
Why my post body has headers parameters ?

That s what i tried on my sim900 modem
{"myvar1="myvalue1","myvar2"="myvalue2"} is my json object
AT+CGATT?
AT+SAPBR=3,1,"CONTYPE","GPRS"
AT+SAPBR=3,1,"APN","mynetworkprovider.com"
AT+SAPBR=3,1,"USER","mylogin"
AT+SAPBR=3,1,"PWD","mypassword"
AT+SAPBR=1,1
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","http://myserveraddress/comm.php"
AT+HTTPPARA="CONTENT","application/json"
AT+HTTPDATA=137,5000 **=>SIM response : DOWNLOAD**
Content-Length: 414
{"myvar1="myvalue1","myvar2"="myvalue2"}
AT+HTTPACTION=1
AT+HTTPREAD =>should respond 200 http ok status
hth

I m not an expert but on my sim900 modem i tried this and it works for me
AT commands to initialize Modem for data transfert
and
AT+HTTPINIT
AT+HTTPPARA="CID",1
AT+HTTPPARA="URL","myhost/deviceRegister"
AT+HTTPPARA="CONTENT","application/json"
AT+HTTPDATA=94,120000
OK
DOWNLOAD
Content-Length:13
{"IMEI":"aa"}
AT+HTTPACTION=1
AT+HTTPREAD
Hope this help

Related

wget says 406 Not acceptable

I have a simple file on my web server, and when I request it in a browser, it loads without problems:
http://example.server/report.php
But when I request the file with wget from a Raspberry Pi, I get this:
$ wget -d --spider http://example.server/report.php
Setting --spider (spider) to 1
DEBUG output created by Wget 1.18 on linux-gnueabihf.
Reading HSTS entries from /home/pi/.wget-hsts
URI encoding = 'ANSI_X3.4-1968'
converted 'http://example.server/report.php' (ANSI_X3.4-1968) -> 'http://example.server/report.php' (UTF-8)
Converted file name 'report.php' (UTF-8) -> 'report.php' (ANSI_X3.4-1968)
Spider mode enabled. Check if remote file exists.
--2018-06-03 07:29:29-- http://example.server/report.php
Resolving example.server (example.server)... 49.132.206.71
Caching example.server => 49.132.206.71
Connecting to example.server (example.server)|49.132.206.71|:80... connected.
Created socket 3.
Releasing 0x00832548 (new refcount 1).
---request begin---
HEAD /report.php HTTP/1.1
User-Agent: Wget/1.18 (linux-gnueabihf)
Accept: */*
Accept-Encoding: identity
Host: example.server
Connection: Keep-Alive
---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 406 Not Acceptable
Date: Fri, 15 Jun 2018 08:25:17 GMT
Server: Apache
Keep-Alive: timeout=3, max=200
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
---response end---
406 Not Acceptable
Registered socket 3 for persistent reuse.
URI content encoding = 'iso-8859-1'
Remote file does not exist -- broken link!!!
I read somewhere that it might be an encoding problem, so I tried
$ wget -d --spider --header="Accept-encoding: *" http://example.server/report.php
but that gives me the exact same error.
That's because the server you're connecting to serves only to certain User-Agents.
Change the user agent and it works fine:
wget -d --user-agent="Mozilla/5.0 (Windows NT x.y; rv:10.0) Gecko/20100101 Firefox/10.0" http://example.server/report.php

Server utility: receive HTTPS POST requests, cat the data

To test something, I want to run a simple web server that:
Will listen for HTTPS POST requests
Print the POST data received to STDOUT (along with other stuff, potentially, so it's fine if it just cats the whole HTTP request)
Is there a quick way to set something like this up? I've tried using OpenSSL's s_server, but it only seems to want to respond to GET requests.
Since s_server does not support POST requests, you should use socat instead of openssl s_server:
# socat -v OPENSSL-LISTEN:443,cert=mycert.pem,key=key.pem,verify=0,fork 'SYSTEM:/bin/echo HTTP/1.1 200 OK;/bin/echo;/bin/echo this-is-the-content-of-the-http-answer'
Here are essential parameters:
fork: to loop for many requests
-v: to display the POST data (and other stuff) to STDOUT
verify=0: do not ask for mutual authentication
Now, here is an example:
We use the following POST request:
% wget -O - --post-data=abcdef --no-check-certificate https://localhost/
[...]
this-is-the-content-of-the-http-answer
We see the following socat output:
# socat -v OPENSSL-LISTEN:443,cert=mycert.crt,key=key.pem,verify=0,fork 'SYSTEM:/bin/echo HTTP/1.1 200 OK;/bin/echo;/bin/echo this-is-the-content-of-the-http-answer'
> 2017/08/05 03:13:04.346890 length=212 from=0 to=211
POST / HTTP/1.1\r
User-Agent: Wget/1.19.1 (freebsd10.3)\r
Accept: */*\r
Accept-Encoding: identity\r
Host: localhost:443\r
Connection: Keep-Alive\r
Content-Type: application/x-www-form-urlencoded\r
Content-Length: 6\r
\r
< 2017/08/05 03:13:04.350299 length=16 from=0 to=15
HTTP/1.1 200 OK
> 2017/08/05 03:13:04.350516 length=6 from=212 to=217
abcdef< 2017/08/05 03:13:04.351549 length=1 from=16 to=16
< 2017/08/05 03:13:04.353019 length=39 from=17 to=55
this-is-the-content-of-the-http-answer

How can I access web site with ESP8266?

I'm trying to retrieve a HTTP resource (http://srcafe21.cafe24.com/mylight.php?id=2) using ESP8266:
ESP8266 AT commands :
AT+CWMODE=3
AT+RST
AT+CWJAP="iptime-3","20030214"
AT+CIFSR
AT+CIPMUX=0
AT+CIPSTART="TCP","srcafe21.cafe24.com",80
AT+CIPSEND=60
GET/HTTP/1.1
Host:srcafe21.cafe24.com/mylight.php?id=2
AT+CIPCLOSE
I get this response:
SEND OK +IPD,311:HTTP/1.1 400 Bad Request
Server: nginx Date: ....
What am I doing wrong?
Your use of HTTP is wrong. It should be
GET /mylight.php?id=2 HTTP/1.1
Host: srcafe21.cafe24.com

Can the HTTP version of response differ from the HTTP version of the request

If I use curl to get a websites data it sends this packet header (with an empty body):
GET / HTTP/1.1
User-Agent: curl/7.38.0
Host: someurl.com
Accept: */*
HTTP version 1.1 is used here.
My question: could it happen that a server answers with a 200 OK Response with a different HTTP version. E.g.:
HTTP/1.0 200 OK
...

What's wrong with this HTTP request?

I am sending the following HTTP request:
POST /input/8dZ8bgapvjfYzmwWno6W.txt HTTP/1.1
Host: data.sparkfun.com
Phant-Private-Key: pz5ga4pkydHgpEb8v608
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 7
temp=44
In my code, I am sending it using UART tx requests to the xbee module, for which the translates to:
POST /input/8dZ8bgapvjfYzmwWno6Wr.txt HTTP/1.1\r\n
Host: data.sparkfun.com\r\n
Phant-Private-Key: pz5ga4pkydHgpEb8v608\r\n
Connection: close\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 7\r\n
\r\n
temp=44
This is to communicate to the phant dataserver at data.sparkfun.com, and it responds with the following data:
HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
I found the answer:
The packet is correct.
While configuring the Xbee Wifi module using XCTU, I had to give the correct port numbers of the server and client Xbee, which were wrong.
Server was 80, client was any port I think.

Resources