[Erlang Study Notes] Cowboy receives post-based HTTP requests

Source: Internet
Author: User

Cowboy is used as the container for network requests, and the POST request for HTTP is processed. Due to the small amount of information, it took a long time to find a good solution on a foreign website. (In fact, it's just a method. I 've been reading the source code for a long time, but the English language is not very good. So I can't find the corresponding processing method. Although binary matching is used in the first place, feeling stupid ).

The following is a small example:

Hhh.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/strict.dtd">

Hhh. erl

-Module (hhh ). -behavior (cowboy_http_handler ). -Export ([init/3, handle/2, terminate/2]). % API % Init ({_ Any, HTTP}, req, [])-> {OK, req, undefined }. handle (req, state)-> % get content in POST request % username % password passwd {[{_ n, name }, {_ p, passwd}], _ REQ} = cowboy_http_req: body_qs (req), IO: Format ("name is :~ TS passwd is :~ Ts ~ N ", [name, passwd]), {OK, _ REQ} = cowboy_http_req: reply (200, [], <" OK ">, req), {OK, _ REQ, State }. terminate (_ REQ, _ state)-> OK.

Through a cowboy_http_req: body_qs (req) function, you can pass the value in the body through post and obtain the desired information through matching.

Because the format returned by this function is {list ({binary (), binary () | true}), # http_req {}}

That is, {[{"name", "name on the page" },{ "passwd", "passwd on the page"}], req1}

After obtaining the information, you can start your business with peace of mind.

Attach the code matching the binary:

<<First:1/binary,Second:1/binary,Last:2/binary>>=<<"fuck">>First=fSecond=uLast=ck


Hope to help =. =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.