Closed digizen closed 10 years ago
Hi Greg,
On the client-side, you need to set the content type of the request:
var url = "http://127.0.0.1:8080/user/login";
HttpRequest.request(url, method:'POST',
requestHeaders: {"content-type": "application/json"},
sendData: JSON.encode(
{"username" : "test",
"password" : "ok" });
I hope that helps.
Also, when you stumble on a problem like this, you can change the log level:
import 'package:logging/logging.dart';
main() {
app.setupConsoleLog(Level.ALL);
...
}
So Redstone will give you some hints on why a request is returning an error code.
Thank you for the help. It's now giving me a different error (405). Here's the code (client-side):
HttpRequest.request(url, method:'POST', requestHeaders: {"content-type": "application/json"},
sendData: JSON.encode(
{"username" : "test",
"password" : "ok" })
The error message on the client:
Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://127.0.0.1:8080/user/login
The server, with the elevated log level (Level.ALL) is still not supplying enough info to diagnose the problem:
FINE: 2014-05-15 08:13:01.354: Received request for: /user/login FINER: 2014-05-15 08:13:01.362: Invoking interceptor: .handleResponseHeader adding cors headers. FINER: 2014-05-15 08:13:01.396: Closed request for: /user/login
Thanks for any insight you can shed into this.
Greg
It seems Redstone is not generating a log message when a 405 error is returned... I will fix that in a future release.
But I could not reproduce this specific error. Can you send me your script, so I can run it at my side?
Sure. I created a test project on github:
https://github.com/digizen/redstone_test/tree/master/redstone_test
Thanks again for your help, Greg
It seems a issue with CORS.
When your client code sends the request, what the browser actually sends is a request with the OPTIONS method. Since your route only accepts POST, the server responds with a 405 status code.
So, I changed your interceptor to:
@app.Interceptor(r'/.*')
handleResponseHeader() {
addCorsHeaders(app.request.response.headers);
if (app.request.method == "OPTIONS") {
app.chain.interrupt();
} else {
app.chain.next();
}
}
And it seems to work fine.
Let me know if this solves your problem.
Edit: About CORS: http://www.html5rocks.com/en/tutorials/cors/#toc-adding-cors-support-to-the-server
Thanks, that fixed the issue!
Can you let me know if my understanding of this is correct?
Thanks again, Greg
yes, that's correct.
I opened a new issue ( #10 ) concerning the log problem.
I'm not sure if I'm doing something wrong or I've stumbled into a bug.
Server-side, this works fine:
Same thing, with a "body" request annotation fails:
The error message I receive (client-side) is below. The server doesn't respond with any error at all.
This is my client-side code:
Can you please assist?
Thanks, Greg