Reputation: 17527
I have an ASP site that has the simple job of storing and retrieving values. This is an example URL for storing values using the site:
http://oneurloranother.cloudapp.net/default.aspx?store=[{"name":"timsplate","values":[1,2,3,4]},{"name":"miwasplate","values":[2,1,4,3]}]
In the browser that works fine, the data gets stored. But I want to call the code from an Arduino. I'm using fairly boiler plate code adapted from the Arduino wifi library sample:
client.println("GET /default.aspx?store=[{\"name\":\"timsplate\",\"values\":[1,2,3,4]},{\"name\":\"miwasplate\",\"values\":[2,1,4,3]}] HTTP/1.1");
client.println("Host:oneurloranother.cloudapp.net");
client.println("Connection: close");
client.println();
But the response I get back from the server is:
HTTP Error 400. The request verb is invalid.
If I try HEAD
or POST
instead of GET
I still receive the same error. If I miss out the payload and just use this code:
client.println("GET /default.aspx HTTP/1.1");
client.println("Host:oneurloranother.cloudapp.net");
client.println("Connection: close");
client.println();
Then the response is great - the HTML of the requested page.
Can anyone see what I am getting wrong?
========== EDIT ==========
OK, this hurts my head. I had some other suggestions from the Arduino forum and rewrote my HTTP command to remove a few of the escaped quotation marks that the System.Web.Script.Serialization.JavaScriptSerializer
in the ASP code did not require, and put the HTTP message in a separate line and sent it to the console (so that I could check my escaped quotation marks were correct). The string looks correct but, and this is the bit that makes my head hurt, the act of declaring the HTTP message separately makes it work! I.e. this code works:
String httpMssg = "GET /default.aspx?store=[{name:\"timsplate\",values:[1,2,3,4]},{name:\"miwasplate\",values:[2,1,4,3]}] HTTP/1.1";
client.println(httpMssg);
client.println("Host: oneurloranother.cloudapp.net");
client.println("Connection: close");
client.println();
whereas this causes the server to reply HTTP Error 400. The request verb is invalid
:
client.println("GET /default.aspx?store=[{name:\"timsplate\",values:[1,2,3,4]},{name:\"miwasplate\",values:[2,1,4,3]}] HTTP/1.1");
client.println("Host: oneurloranother.cloudapp.net");
client.println("Connection: close");
client.println();
That is mad. I have working code now, but I am intrigued; why would the second code block fail and the first succeed?
Upvotes: 0
Views: 1049
Reputation: 60143
When I issue your original URL in the browser, it's escaped thusly: http://oneurloranother.cloudapp.net/default.aspx?store=[{%22name%22:%22timsplate%22,%22values%22:[1,2,3,4]},{%22name%22:%22miwasplate%22,%22values%22:[2,1,4,3]}]
so you might try that.
Having said that, I have no explanation for your followup question. Those look identical to me. What programming language is that?
Upvotes: 1
Reputation: 7356
Many of the characters in your query string are reserved characters. Try applying percent-encoding to them. Many HTTP libraries have a function for that, but since you're using println
to send the request you might have to do it yourself.
Upvotes: 1