Merly
  • Quick start
  • Guide
    • Routing
    • Start
    • Headers
    • CORS
    • Http methods available
    • Statics files
    • Custom 404 Handler
    • Body parser
    • Middleware
  • Website
  • Code
  • Examples
Powered by GitBook
On this page

Was this helpful?

  1. Guide

Body parser

The data sent sent in the body of a request of a client, can be processed previously.

formats

If you want to format the data sent in the body by the client to an endpoint, to be used in a certain format within the body of the function that processes said request, you should only use the formats dictionary.

To this dictionary, the value of the Content-Type of the sent data will be added as a key and it will be associated with a custom method, which must necessarily receive a string and return a single value, which will be the data already processed.

function tojson(data::String)
   return JSON.parse(data)
end

formats["application/json"] = tojson

Once this is done, when the server is executed, the data with the respective Content-Type specified previously will be processed by the custom function and sent to the function associated with the determined endpoind.

Post("/data", (request,HTTP)-> begin

  HTTP.Response(200
          , HTTP.mkheaders(["Content-Type" => "text/plain"])
          , body=string("I did something! ", request.body["query"]))

end)

POST /data

Headers

Name
Type
Description

Content-Type

string

application/json

Request Body

Name
Type
Description

query

string

text

"I did something! text"
PreviousCustom 404 HandlerNextMiddleware

Last updated 4 years ago

Was this helpful?