Question

I just started working with Yaws to try to create some simple RESTful web services, however I ran into an unexpected issue: I can't seem to access my data when I do a PUT request. When I try to use the yaws_api:parse_post function, I get the following error:

ERROR: Can't parse post body for 'PUT' requests: URL: ...

I wrote out the entire request and everything looks identical, so I'm very confused. Am I doing something wrong? GETs and POSTs work properly. In fact, the only difference between how I handle POSTs and PUTs right now is just what I display for each as right now I'm just writing test code to show success.

Thanks in advance.

Was it helpful?

Solution

I also find it weird that there's no equivalent of parse_post/1 for non-POST HTTP methods in the Yaws API.

In any case, I simply use parse_query/1 for PUTs. Given a PUT request with param1=abc, param2=def:

index(Args) ->
  case yaws_arg:method(Args) of
  'PUT' ->
    Parsed = yaws_api:parse_query(Args),
    io:format("PUT PARAMS=~p", [Parsed]),
    ....
  'POST' ->
    ....

the output is:

PUT PARAMS=[{"param1","abc"}, {"param2","def"}}]

(The above example is in a Erlyweb controller.)

OTHER TIPS

You should add (or change) the parameter "dav = true" in the file "yaws.conf" into the section <server> like this:

<server ...>
        dav = true
</server>

The data for PUT is located in #arg.clidata, like it is for a POST. Internally the call to parse_query and parse_post ultimately use the same function to parse GET and POST.

A work around for PUT parameters in the body is therefore

Parsed = yaws_api:parse_query( Arg#arg{ querydata = Arg#arg.clidata } ),

It works by copying the clidata field (data in the body) to the querydata field and parsing it like a GET.

This assumes that the body is urlencoded like for a POST.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top