HTTP post Json 400 Error
<p>I am trying to post data to my server from my microcontroller. I need to send raw http data from my controller and this is what I am sending below:</p>

<pre><code>POST /postpage HTTP/1.1
Host: https://example.com
Accept: */*
Content-Length: 18
Content-Type: application/json

{"cage":"abcdefg"}
</code></pre>

<p>My server requires JSON encoding and not form encoded request.</p>

<p>For the above request sent, I get an 400 error from the server, HTTP/1.1 400 Bad Request</p>

<p>However, when I try to reach the post to my server via a python script via my laptop, I am able to get a proper response.</p>

<pre><code>import requests
url='https://example.com'
mycode = 'abcdefg'



def enter():
    value = requests.post('url/postpage', 
                             params={'cage': mycode})
    print vars(value)


enter()
</code></pre>

<p>Can anyone please let me know where I could be going wrong in the raw http data I'm sending above ?</p>

<p>HTTP specifies the separator between headers as a single newline, and requires a double newline before the content:</p>

<pre><code>POST /postpage HTTP/1.1
Host: https://example.com
Accept: */*
Content-Length: 18
Content-Type: application/json

{"cage":"abcdefg"}
</code></pre>

<hr>

<p>If you donât think youâve got all of the request right, try seeing what was sent by Python:</p>

<pre><code>response = ...
request = response.request # request is a PreparedRequest.
headers = request.headers
url = request.url
</code></pre>

<p>Read the <a href="http://docs.python-requests.org/en/master/api/?highlight=logging#requests.PreparedRequest" rel="nofollow">docs for <code>PreparedRequest</code></a> for more information.</p>

<hr>

<p>To pass a parameter, use this Python:</p>

<pre><code>REQUEST = 'POST /postpage%s HTTP/1.1\r\nHost: example.com\r\nContent-Length: 0\r\nConnection: keep-alive\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.4.3 CPython/2.7.9 Linux/4.4.11-v7+\r\n\r\n';
query = ''
for k, v in params.items():
    query += '&amp;' + k + '=' + v # URL-encode here if you want.
if len(query): query = '?' + query[1:]
return REQUEST % query
</code></pre>

python json http-post