3

My RESTfull server with Perl Dancer runs perfectly with one exception: send_error("Simple Error string",400) for example removes the error message.

The error code will be sent, but without the actual message "Simple Error string".

I tried return send_entity({ error => "Missing parameter "}, 400) with no avail too.

Here is the code:

#!/usr/bin/perl
##########################################################################################################################################
# V1.0 REST-Server
##########################################################################################################################################


use Dancer;
use Dancer::Plugin::Auth::Basic;
use Dancer::Plugin::Database;
use Dancer::Plugin::REST;

set serializer => 'XML';

post '/hello/:name' => sub 
{
  # Low level test route
  auth_basic realm => 'Authorized personnel only', users => { 'alice' => 'AlicesPassword', 'bob' => 'BobsPassword' };
  send_error("Simple Error string",400);
};
dance;

The tests take place with curl:

#curl -X POST -k -u alice:AlicesPassword http://localhost:5000/hello/abc
An internal error occured

and with RESTer under Firefox (via https proxy).

RESTer reports the same.

Several googling brought no hints, how to produce the correct error code AND the provided message.

I hope anybody can give me a hint.

cablebuddy
  • 33
  • 6
  • Why are you using Rest::Client and XML::Simple and MIME::Base64? Are those leftovers from pruning your code to a [mcve]? Which version of Dancer is this? – simbabque Apr 05 '16 at 13:40
  • Sorry you are right, that are indeed leftovers from the bigger code! – cablebuddy Apr 05 '16 at 13:43
  • Right! I removed the --data extension. It is not necessary to show the error. Thank you for your tips to show a verifiyable example, it's my first post! – cablebuddy Apr 05 '16 at 13:50
  • I can't get it to work either, and I don't see where that error message comes from. I added a `get` which works, and if you just `return "foo"` from the `post` sub it also works, even without anything but `use Dancer;`. But as soon as the `send_error` is there, it breaks. That's weird. But note that `send_error("foo", 500)` will produce an HTTP Response with the status `500 foo`. That `foo` does not go into the body the way you expect it to. – simbabque Apr 05 '16 at 14:57
  • send_error breaks the route. That's documenented. Yes, a single 'return "foo"' works, but with the http status code 200. I look for a way to send a code with 4XX or 5XX together with a message. I am experimenting with `return send_entity({ error => $message}, 500)`. This seems to work better. I'll post here, if I know more. – cablebuddy Apr 05 '16 at 15:18
  • Well you can do https://metacpan.org/pod/Dancer#status and `return "something went horribly wrong";` – simbabque Apr 05 '16 at 15:20
  • For `return "something went horribly wrong"` the http status code is 200. Doesn't help! – cablebuddy Apr 05 '16 at 15:28
  • Even if you set a status explicitly? – simbabque Apr 05 '16 at 15:29
  • How would you set a status explicitly? – cablebuddy Apr 05 '16 at 15:31
  • I just said above. – simbabque Apr 05 '16 at 15:37
  • Oh yes, you meant `status 400; return "error message"`! Indeed that's it what I looked for!! Don't ask me why I discovered the `status` keyword not ealier! How can I mark this thread as solved? Thank you very much! – cablebuddy Apr 05 '16 at 15:46
  • I will post an answer. – simbabque Apr 05 '16 at 15:46
  • You can accept my answer by clicking the check mark. See [ask]. I typed all this from my phone. Not fun. :-P – simbabque Apr 05 '16 at 15:52

1 Answers1

4

To simply change the Http status you can use the status keyword and return your content as the response body.

get '/' => sub {
  status 418;
  return 'I cannot make coffee.';
};
ThisSuitIsBlackNot
  • 23,492
  • 9
  • 63
  • 110
simbabque
  • 53,749
  • 8
  • 73
  • 136