httpc: decode body in http response
@TarantoolBot document Title: Document a body decoding in http response New method "response:decode()" has been introduced for a HTTP response. It allows to decode body to a Lua object. Decoding depends on content type passed in HTTP header and decoding function for it defined in table `http.decoders` or `http.new().decoders`. Function `response:decode()` will raise an error when content type is present in the response, but there is no appropriate decoder. By default decoders for the following content types are defined: - to JSON with content-type "application/json" - to MsgPack with content-type "application/msgpack" - to YAML with content-type "application/yaml" If a content type is not present in the response, :decode() assumes "application/json". ``` tarantool> resp = require('http.client').put( 'http://127.0.0.1:8080', '{"productId": 123456, "quantity": 100}') --- ... tarantool> resp.body --- - '{"productId": 123456, "quantity": 100}' ... tarantool> tarantool> resp:decode() --- - productId: 123456 - quantity: 100 ... tarantool> ``` For content types missed in `http.decoders` user could define it's own by defining a new record with a key equal to desired MIME type in lowercase and it's decoding function that must accept HTTP body, content type and must return a decoded value: ``` local http = require("http") local xml = require("luarapidxml") http.decoders = { ['application/xml'] = function(body, _content_type) return xml.decode(body) end, } ``` Closes #6833
Showing
- changelogs/unreleased/gh-6833-decode-body-http-response.md 7 additions, 0 deletionschangelogs/unreleased/gh-6833-decode-body-http-response.md
- src/lua/httpc.lua 46 additions, 1 deletionsrc/lua/httpc.lua
- test/app-luatest/http_client_test.lua 54 additions, 0 deletionstest/app-luatest/http_client_test.lua
- test/app-luatest/http_client_unit_test.lua 137 additions, 0 deletionstest/app-luatest/http_client_unit_test.lua
Loading
Please register or sign in to comment