Chris Hagan
Chris Hagan

Reputation: 2437

Why does my successful streaming response in Webmachine have the status code 500?

The following code is the full source of a webmachine resource. The expected behaviour is that the streaming response should be a 200, and should be a string of specified length, entirely composed of the letter 'a'.

This string is indeed returned as the body of the response, but the status code is a 500. How can this be?

-module(dummy_binary_resource).
-export([init/1, to_html/2]).

-include_lib("webmachine/include/webmachine.hrl").

init(Config)->
    {ok, Config}.

send_streamed_body(Remaining) ->
    PacketSize=1024,
    case Remaining of
        Partial when Partial =< PacketSize ->
            {string:chars($a,Partial),done};
        Full ->
            {string:chars($a,Full), fun() -> send_streamed_body(Remaining - PacketSize) end}
    end.

to_html(ReqData,State)->
    PathInfo = wrq:path_info(ReqData),
    {ok,SizeString} = dict:find(size,PathInfo),
    {Size,[]} = string:to_integer(SizeString),
    {true,wrq:set_resp_body({stream,send_streamed_body(Size)},ReqData),State}.

Upvotes: 0

Views: 272

Answers (1)

Chris Hagan
Chris Hagan

Reputation: 2437

The return value was incorrect.

The appropriate return value is {{halt, 200}, wrq:set_resp_body({stream, send_streamed_body(Size)}, ReqData), State}. because the request data record has been initialized with a default code of 500. It does seem slightly counterintuitive to be returning a "halt" value which isn't going to stop anything, but is in fact going to signify success.

Upvotes: 0

Related Questions