0

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.1\r\n"
            "Host: user:[email protected]:5984/ \r\n"
            "Content-Type: application/json\r\n"
            "\r\n"
            "'{\"a\":1}'\r\n\r\n";

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:[email protected]:5984/iot/ -H "Content-Type: application/json" -d '{"a":3}'

2 Answers 2

0

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

instead of

 "'{\"a\":1}'\r\n\r\n"

it should be

 "{\"a\":1}\r\n\r\n"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this didn't make any difference... :-/
0

I was missing the line:

"Content-Length: " + String(json.length()) + "\r\n"

in the header.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.