erlyaws / yaws

Yaws webserver
https://erlyaws.github.io
BSD 3-Clause "New" or "Revised" License
1.28k stars 267 forks source link

No result after yaws_api:parse_post(A) on MacOS Mojave #392

Closed alexandrkirilov closed 4 years ago

alexandrkirilov commented 4 years ago

Just installed Yaws from scratch (from git repository, master branch) and got this result on yaws_api:parse_post [{[],undefined}] and all data is in result of yaws_api:parse_query.

The environment:

The HTTP POST request is: POST /api/post_form?Name=3533&Age=2352353&Formula=235325352 HTTP/1.1 Content-Type: application/x-www-form-urlencoded Host: local.universal.backend:8080 Connection: close Content-Length: 0

The out() code is: out(Yaws_arguments) -> io:format("Parsed POST data: ~p~n", [yaws_api:parse_post(Yaws_arguments)]), io:format("Parsed query data: ~p~n",[yaws_api:parse_query(Yaws_arguments)]), [ a_http_headers:cache(no), {ehtml,[ {pre,[],"1"} ]} ].

When I send GET request I see this in console: Parsed POST data: [] Parsed query data: [{"Name","3533"},{"Age","2352353"},{"Formula","235325352"}]

When I send POST request I see this in console: Parsed POST data: [{[],undefined}] Parsed query data: [{"Name","3533"},{"Age","2352353"},{"Formula","235325352"}]

vinoski commented 4 years ago

Thanks for the details, I'll take a closer look.

vinoski commented 4 years ago

OK, I've reviewed your issue and have duplicated what you're seeing, but Yaws is operating correctly. Here's the relevant part of the Yaws PDF documentation (which is generated from source file yaws.tex:

The differences between using the query part of the URL and a form are the following:

  • Using the query arg works with any kind of requests. We parse the query argument with the function yaws_api:parse_query(Arg)
  • If we use a form and POST the user data the client will transmit the user data in the body of the request. That is, the client sends a request to get the page using the POST method and it then attaches the user data—encoded—into the body of the request.

A POST request can have a query part in its URL as well as user data in the body.

Preceding this text is an example that shows how yaws_api:parse_post/1 is used to handle the encoded data in the body of the POST.

Try changing your POST request to the following:

POST /api/post_form HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: local.universal.backend:8080
Connection: close
Content-Length: 39

Name=3533&Age=2352353&Formula=235325352

and then your appmod will print:

Parsed POST data: [{"Name","3533"},{"Age","2352353"},{"Formula","235325352"}]
Parsed query data: []
vinoski commented 4 years ago

If my previous reply addresses your issue @alexandrkirilov please close the issue, but if you have further comments or questions, feel free to add them.

alexandrkirilov commented 4 years ago

My apology for delay of closing this issue. This issue totally my bad. I've been using Yaws since 2012 (even lecturing examples of Yaws usage in one of university, already been sending to you email about about 3 years ago), but not last year. All of it totally forgotten. All of it my bad.

vinoski commented 4 years ago

Thanks, and no worries. I thought you might just be experimenting with my reply and maybe had some more questions.