Coder Social home page Coder Social logo

web-request-role-response's Introduction

NAME

Web::Request::Role::Response - Generate various HTTP responses from a Web::Request

VERSION

version 1.004

SYNOPSIS

# Create a request handler
package My::App::Request;
use Moose;
extends 'Web::Request';
with 'Web::Request::Role::Response';

# Make sure your app uses your request handler, e.g. using OX:
package My::App::OX;
sub request_class {'My::App::Request'}

# in some controller action:

# redirect
$req->redirect('/');
$req->permanent_redirect('/foo');

# return 204 no content
$req->no_content_response;

# return a transparent 1x1 gif (eg as a tracking pixle)
$req->transparent_gif_response;

# file download
$req->file_download_response( 'text/csv', $data, 'your_export.csv' );

DESCRIPTION

Web::Request::Role::JSON provides a few methods that make generating HTTP responses easier when using Web::Request.

Please note that all methods return a Web::Response object. Depending on the framework you use (or lack thereof), you might have to call finalize on the response object to turn it into a valid PSGI response.

METHODS

redirect

$req->redirect( '/some/location' );
$req->redirect( $ref_uri_for );
$req->redirect( 'http://example.com', 307 );

Redirect to the given location. The location can be a string representing an absolute or relative URL. You can also pass a ref, which will be resolved by calling uri_for on the request object - so be sure that your request object has this method (extra points if the method also returns something meaningful)!

You can pass a HTTP status code as a second parameter. It's probably smart to use one that makes sense in a redirecting context...

permanent_redirect

$req->permanent_redirect( 'http://we.moved.here' );

Similar to redirect, but will issue a permanent redirect (who would have thought!) using HTTP status code 301.

file_download_response

$req->file_download_response( $content-type, $data, $filename );

Generate a "Download-File" response. Useful if your app returns a CSV/Spreadsheet/MP3 etc. You have to provide the correct content-type, the data in the correct encoding and a meaningful filename.

no_content_response

$req->no_content_response

Returns 204 No Content.

transparent_gif_response

$req->transparent_gif_response

Returns a transparent 1x1 pixle GIF. Useful as the response of a tracking URL.

THANKS

Thanks to

AUTHOR

Thomas Klausner [email protected]

COPYRIGHT AND LICENSE

This software is copyright (c) 2017 - 2019 by Thomas Klausner.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

web-request-role-response's People

Contributors

domm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.