<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE> [99s-extend] Accept header in POST request
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Accept%20header%20in%20POST%20request&In-Reply-To=%3C52EFEAE0.7050804%40ninenines.eu%3E">
<META NAME="robots" CONTENT="index,nofollow">
<style type="text/css">
pre {
white-space: pre-wrap; /* css-2.1, curent FF, Opera, Safari */
}
</style>
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="000328.html">
<LINK REL="Next" HREF="000330.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[99s-extend] Accept header in POST request</H1>
<B>Loïc Hoguin</B>
<A HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Accept%20header%20in%20POST%20request&In-Reply-To=%3C52EFEAE0.7050804%40ninenines.eu%3E"
TITLE="[99s-extend] Accept header in POST request">essen at ninenines.eu
</A><BR>
<I>Mon Feb 3 20:15:44 CET 2014</I>
<P><UL>
<LI>Previous message: <A HREF="000328.html">[99s-extend] Accept header in POST request
</A></li>
<LI>Next message: <A HREF="000330.html">[99s-extend] Accept header in POST request
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#329">[ date ]</a>
<a href="thread.html#329">[ thread ]</a>
<a href="subject.html#329">[ subject ]</a>
<a href="author.html#329">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>Sure. It won't be called if not a GET or HEAD request so that's probably
the best value you can return in your case.
On 02/03/2014 08:08 PM, Łukasz Biedrycki wrote:
><i> Ok,
</I>><i> it is more clear for me.
</I>><i>
</I>><i> Last question I have is about content_types_provided function.
</I>><i>
</I>><i> Is it safe to define it like this?
</I>><i>
</I>><i> content_types_provided(R, S) ->
</I>><i> ContentTypes = [{{<<"application">>, <<"json">>, '*'}, *undefined*}],
</I>><i> {ContentTypes, Req, State}.
</I>><i>
</I>><i> Callback in content_types_provided is useless for POST requests, as it
</I>><i> won’t be called.
</I>><i> Is it safe to use *undefined *atom, to have a source code clearer?
</I>><i>
</I>><i>
</I>><i>
</I>><i>
</I>><i> On Mon, Feb 3, 2014 at 7:37 PM, Loïc Hoguin <<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>
</I>><i> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>>> wrote:
</I>><i>
</I>><i> If Accept is sent and is different than text/html, yes.
</I>><i>
</I>><i> This is how HTTP is defined. If the client says it speaks only
</I>><i> content-type X but you can only reply with content-type Y, you error
</I>><i> out early and stop processing the request. On the other hand if the
</I>><i> client doesn't say what content-type it speaks then the server can
</I>><i> choose whichever one it wants.
</I>><i>
</I>><i>
</I>><i> On 02/03/2014 07:26 PM, Łukasz Biedrycki wrote:
</I>><i>
</I>><i> My application sends both headers: “Content-type” and “Accept”
</I>><i> header
</I>><i> using POST method.
</I>><i>
</I>><i> For POST rest handler do I have to specify both:
</I>><i> content_types_accepted
</I>><i> and content_types_provided to manage this kind of request?
</I>><i>
</I>><i>
</I>><i> On Mon, Feb 3, 2014 at 7:23 PM, Loïc Hoguin <<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>
</I>><i> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>>
</I>><i> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>>>> wrote:
</I>><i>
</I>><i> The content-type provided is relevant for any response, not
</I>><i> just
</I>><i> responses to GET requests. It defaults to text/html. If
</I>><i> your client
</I>><i> doesn't send that content-type, you have to define the
</I>><i> callback.
</I>><i>
</I>><i> I notice that the documentation is incorrect about the relevant
</I>><i> methods for this callback, I will open a ticket to fix it soon.
</I>><i>
</I>><i>
</I>><i> On 02/03/2014 07:13 PM, Łukasz Biedrycki wrote:
</I>><i>
</I>><i> Hi,
</I>><i> I have a rest handler that accepts POST and PUT
</I>><i> requests with
</I>><i> “application/json” content type.
</I>><i>
</I>><i> I have content_types_accepted function defined as follows:
</I>><i>
</I>><i> content_types_accepted(Req, State) ->
</I>><i>
</I>><i>
</I>><i> {[{‘application/json', from_json}], Req, State}.
</I>><i>
</I>><i>
</I>><i>
</I>><i> The problem I have is within a request that has two
</I>><i> headers:
</I>><i>
</I>><i> *Content-type*: application/json
</I>><i> *Accept*: application/json
</I>><i>
</I>><i> With this combination I receive *406*.
</I>><i>
</I>><i>
</I>><i> You can repeat it with test:
</I>><i>
</I>><i> http_SUITE.erl:
</I>><i> 1072 rest_postonly(Config) ->
</I>><i> 1073 Client = ?config(client, Config),
</I>><i> 1074 Headers = [
</I>><i> 1075 {<<"content-type">>, <<"text/plain">>},
</I>><i> 1076 {<<"accept">>, <<"text/plain">>}
</I>><i> 1077 ],
</I>><i> 1078 {ok, Client2} =
</I>><i> cowboy_client:request(<<"POST"____>>,
</I>><i>
</I>><i> 1079 build_url("/postonly", Config), Headers,
</I>><i> "12345",
</I>><i> Client),
</I>><i> 1080 {ok, 204, _, _} =
</I>><i> cowboy_client:response(____Client2).
</I>><i>
</I>><i>
</I>><i> My solution to that was to add a content_types_provided
</I>><i> function:
</I>><i>
</I>><i>
</I>><i> content_types_provided(Req, State) ->
</I>><i>
</I>><i>
</I>><i> ContentTypes = [{{<<"application">>, <<"json">>, '*'},
</I>><i> to_json}],
</I>><i>
</I>><i>
</I>><i> {ContentTypes, Req, State}.
</I>><i>
</I>><i>
</I>><i>
</I>><i> But it is useless as *to_json* callback registered is
</I>><i> not called
</I>><i> anyhow.
</I>><i>
</I>><i> Adding *content_types_provided* function is a correct
</I>><i> solution
</I>><i> in this case?
</I>><i>
</I>><i> Or I am missing something here?
</I>><i> “Accept” header is not relevant only in case of GET
</I>><i> requests?
</I>><i>
</I>><i> Thank for help,
</I>><i> Łukasz Biedrycki
</I>><i>
</I>><i>
</I>><i> ___________________________________________________
</I>><i> Extend mailing list
</I>><i> <A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.ninenines.eu</A> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.ninenines.eu</A>>
</I>><i> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.__ninenines.eu</A>
</I>><i> <mailto:<A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.ninenines.eu</A>>>
</I>><i> <A HREF="https://lists.ninenines.eu/____listinfo/extend">https://lists.ninenines.eu/____listinfo/extend</A>
</I>><i> <<A HREF="https://lists.ninenines.eu/__listinfo/extend">https://lists.ninenines.eu/__listinfo/extend</A>>
</I>><i>
</I>><i> <<A HREF="https://lists.ninenines.eu/__listinfo/extend">https://lists.ninenines.eu/__listinfo/extend</A>
</I>><i> <<A HREF="https://lists.ninenines.eu/listinfo/extend">https://lists.ninenines.eu/listinfo/extend</A>>>
</I>><i>
</I>><i>
</I>><i> --
</I>><i> Loïc Hoguin
</I>><i> <A HREF="http://ninenines.eu">http://ninenines.eu</A>
</I>><i>
</I>><i>
</I>><i>
</I>><i> --
</I>><i> Loïc Hoguin
</I>><i> <A HREF="http://ninenines.eu">http://ninenines.eu</A>
</I>><i>
</I>><i>
</I>
--
Loïc Hoguin
<A HREF="http://ninenines.eu">http://ninenines.eu</A>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000328.html">[99s-extend] Accept header in POST request
</A></li>
<LI>Next message: <A HREF="000330.html">[99s-extend] Accept header in POST request
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#329">[ date ]</a>
<a href="thread.html#329">[ thread ]</a>
<a href="subject.html#329">[ subject ]</a>
<a href="author.html#329">[ author ]</a>
</LI>
</UL>
<hr>
<a href="https://lists.ninenines.eu/listinfo/extend">More information about the Extend
mailing list</a><br>
</body></html>