Actually I am trying to send PUT request to a remote server the request is successfully send to the remote server but unfortunately the request is considered a bad request it can not see the body of the request I hope my discription is clear and here is the code for sending the request in Arduino programming language:
Serial.println("Sending to Server: ");
client.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1\n");
Serial.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1");
client.print("Host: ********.koding.io\n");
client.print("Cache-Control: no-cache\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n\r\n");
client.print("status=1");
one thing I want to mention that from the server side I used a tool that is called postman and from it it works successfully without any error and actually I have taken it's http request and convert it into the code to be send and you can find the http request below:
PUT /***/***/sensor/uod/1/A/1 HTTP/1.1
Host: *******.koding.io
Cache-Control: no-cache
Postman-Token: 8740ccb4-c0f2-a916-fd52-3089e4f3cbc9
Content-Type: application/x-www-form-urlencoded
status=0
I have solve the problem by adding content-Length: 10 header
Serial.println("Sending to Server: ");
client.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1\n");
Serial.print("PUT /***/***/sensor/uod/1/A/1 HTTP/1.1");
client.print("Host: *********.koding.io\n");
client.print("Cache-Control: no-cache\n");
client.print("Content-Type: application/x-www-form-urlencoded\r\n");
client.print("Content-Length: 10\r\n\r\n");
client.print("status=1\r\n");
Related
I'm trying to test sending HTTP requests from my Arduino. I decided to use Free RESTful web service - http://services.groupkt.com. But something goes wrong and I don't understand what.
GET request:
GET /country/get/all HTTP/1.1
Host: 45.79.172.152
Connection: keep-alive
Serial Monitor:
AT+CIPMUX=0
OK
AT+CIPSTART="TCP","45.79.172.152",80
CONNECT
OK
AT+CIPSEND=74
OK
>
busy s...
Recv 74 bytes
SEND OK
+IPD,493:HTTP/1.1 408 Request Timeout
Date: Thu, 07 Jun 2018 16:10:59 GMT
Server: Apache/2.4.25 (Debian)
Content-Length: 307
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>408 Request Timeout</title>
</head><body>
<h1>Request Timeout</h1>
<p>Server timeout waiting for the HTTP request from the client.</p>
<hr>
<address>Apache/2.4.25 (Debian) Server at services.groupkt.com Port 80</address>
</body></html>
CLOSED
What I'm doing wrong?
HTTP is not like Telnet. You can't enter a HTTP request line by line in Serial Monitor.
HTTP requests are meant to be sent by a program and the timeout to receive the
complete request on servers is one or two seconds. Write a sketch the sends the request.
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
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.
I use SIM900 shield with arduino UNO. I try to make conection with xively server. I follow the instructions from: http://arduino.cc/en/Tutorial/GSMExamplesXivelyClient?from=Tutorial.GSMExamplesPachubeClient
This example is for other GSM shield, but it show me how should look like conection with xively. I've made such code:
gsm.SimpleWrite("PUT /v2/feeds/");
gsm.SimpleWrite(1055165076);
gsm.SimpleWriteln(".csv HTTP/1.1");
gsm.SimpleWrite("Host: api.xively.com\n");
gsm.SimpleWrite("X-ApiKey: ");
gsm.SimpleWriteln("j2AlGTCLxJzArQhL7dqrLYG6BtJd5aI8LoJmc7ha0mgsPWPD");
//gsm.SimpleWrite("User-Agent: ");
//gsm.SimpleWrite(arduino);
gsm.SimpleWrite("Content-Length: ");
gsm.SimpleWriteln(10);
gsm.SimpleWrite("Content-Type: text/csv\n");
gsm.SimpleWriteln("Connection: close");
gsm.SimpleWriteln("");
gsm.SimpleWrite("sensor1,");
gsm.SimpleWriteln(16);
gsm.SimpleWrite(end_c);
By this code I would like to send sensor1 value : 16.
After execution of this code xively server make my some answer:
HTTP/1.1 403 Forbidden
Date: Mon, 25 Nov 2013 08:19:27 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunkd
oncin ls
4
Yud o aetencesr emsin oacs hsrsuc
On web https://xively.com/dev/docs/api/communicating/http/http_status_codes/ i found that error mean:
403 Forbidden Xively understands your request, but refuses to fulfil it. An accompanying error message should explain why.(NOTE: in the V2 API, this is the error returned when the API rate limit is exceeded.)
If I change for example API-KEY, server give me 404 error. I'm trying to solve this problem for several days. Maybe someone has an idea what is wrong?
Try to add "Accept: */*"
I believe your Content-Length should be 11 (including newline).
I'm sending data to a server with an Arduino which requires constructing an HTML POST line-by-line. I don't necessarily know the Content-Length a-priori, so I am using "chunked" encoding.
When I tried this example post from Wikipedia with the "Transfer-Encoding" option as specified in rfc2616
client.println("POST /myurl HTTP/1.1");
client.println("Host: 12.345.679.999"); // replaced with the test server's IP
client.println("User-Agent: Arduino/1.0");
client.println("Transfer-Encoding: chunked");
client.println();
client.println("4");
client.println("test");
client.println("0");
client.println();
or, with escape characters explicit:
client.print("4\r\ntest\r\n0\r\n\r\n");
I receive the error from my server:
HTTP/1.1 411 Length Required
A request of the requested method POST requires a valid Content-length.
Server: Apache/2.2.22 (Ubuntu)
However, "chunked" encoding shouldn't require a Content-Length header field, see 4.4 - Message Length in rfc2616
Am I missing a field? Why doesn't this call work?
For the record, the non-Chunked-Encoding works:
if(client.connect(server, 80)){
String PostData = "test";
Serial.println("POST /myurl HTTP/1.1");
client.println("Host: 12.345.679.999"); // replaced with the test server's IP
Serial.println("User-Agent: Arduino/1.0");
Serial.print("Content-Length: ");
Serial.println(PostData.length());
Serial.println();
Serial.println(PostData);
}
UPDATE
From the apache2 error.log: "chunked Transfer-Encoding forbidden"
After finding
chunked Transfer-Encoding forbidden
in my Apache2 log I concluded that the error was not in the POST that I was making.
I found that modwsgi (the middle-layer between apache and django) does not enable chunked transfer-encoding by default. In the past, chunked wasn't supported at all
Refering to the change-log in the new version of modwsgi, I found that writing
WSGIChunkedRequest On
in my apache httpd.conf file allowed chunked requests (no more 411 error)