ESP8266 client connection to MongoDB

I'm trying to post a new record to my MongoDB (actual CouchDB), but it seems that I'm having problems with the format of my request.

I'm using the following code: (not showing debug and validations)

WiFiClient client;
client.connect("172.16.1.4", 5984)

String connStr = "POST /iot/ HTTP/1.1rn"
            "Host: user:password@172.16.1.4:5984/ rn"
            "Content-Type: application/jsonrn"
            "rn"
            "'{"a":1}'rnrn";

client.print(connStr);

I get this response back:

HTTP/1.1 400 Bad Request
Server: CouchDB/1.6.1 (Erlang OTP/18)
Date: Sat, 07 Oct 2017 11:57:50 GMT
Content-Type: text/plain; charset=utf-8
Content-Length: 48 Cache-Control: must-revalidate

{"error":"bad_request","reason":"invalid_json"}

HTTP/1.1 400 Bad
Request Server: MochiWeb/1.0 (Any of you quaids got a smint?)
Date: Sat, 07 Oct 2017 11:57:50 GMT
Content-Length: 0

I have tried with differet json/data...

Using Linux - I have no problems:

curl -X POST user:password@172.16.1.4:5984/iot/ -H "Content-Type: application/json" -d '{"a":3}'


The issue might be with the single quotes(') before { and at the end of }

instead of

 "'{"a":1}'rnrn"

it should be

 "{"a":1}rnrn"

I was missing the line:

"Content-Length: " + String(json.length()) + "rn"

in the header.

链接地址: http://www.djcxy.com/p/48554.html

上一篇: HTTP POST和GET在Linux中使用cURL

下一篇: ESP8266客户端连接到MongoDB