Stéphane,
The main idea behind the REST spec for creation is that the response provides the URL of the new resource in the Location header. You may respond with an empty structure or provide a data response.
If you want the location header to be exposed to javascript for a cross-domain request, you will also need to add the following header
Access-Control-Expose-Headers: Location
A typical jQuery handling of that header would be:
$.ajax({
type: ‘POST’,
url:‘/api/v1/myhandler’,
data: formData,
success: function(data, textStatus, request){
var resource = request.getResponseHeader(‘location’);
$.get(resource).success(
function(data){
return data;
}
);
}}
});
This methodology requires two HTTP requests, which I dislike personally. There’s nothing in the spec that says you can’t respond with data and my personal preference is to respond with the data of the resource created, unless there is a specific reason not to do so (e.g. - the data marshalling for that resource response is done by a separate controller or other resource).
Here’s a good rundown on various thoughts to your question.
java - Is it ok by REST to return content after POST? - Stack Overflow November 4, 2015 at 8:15:16 AM, Stéphane MERLE (@Stephane_MERLE) wrote:
I try to build the API with those recommendations :
and I see that I should send a 201 for a new POST … so in fact I shouldn’t send any RETURN to my functions ?
or should I catch the onRequestEnd to do like mythrow ?
it seem incredible that no one get stopped by those personnalisation statuscode …
Stéphane
Le mercredi 4 novembre 2015 10:26:37 UTC+1, Stéphane MERLE a écrit :
Le mercredi 4 novembre 2015 02:39:09 UTC+1, Jon Clausen a écrit :
If you want to write the headers to the browser (or flush; in script) will force the currently defined headers to write. Once you call abort; after that, your headers are already sent and may not be changed.
you are right !! this did the trick :
<cffunction name="mythrow" access="private" output="false" returntype="void">
<cfargument name="errorCode" type="numeric" default="500" />
<cfargument name="detail" type="string" required="true" hint="message to return to api consumer" />
<cfargument name="message" type="string" default="" />
<cfcontent reset="true" />
<cfif isdefined("arguments.message") and arguments.message neq "">
<cfset arguments.detail=arguments.detail & arguments.message>
</cfif>
<cfcontent reset="yes"><cfheader statuscode="#arguments.errorCode#" statustext="#arguments.detail#" /><cfflush /><cfabort showerror="#arguments.errorCode# #arguments.detail#" />
</cffunction>
Frankly, though, you’re better off implementing better error handling upstream and letting 500 errors be what they are: server errors. If you want to deliver a 400 or 404, that should be determined before there is ever a chance to throw an exception.
this is exactly what I try to do …
when I do check the params I send back a 400 error with the message … but I leave 500 catched error when there is a bug in my code …
for example a part of my checks :
if (structkeyexists(application.bddinfos, ARGUMENTS.id_base) eq 0) {
erreurmsg=“wrong id_base”;
writeLog(file=application.logname_error, text=“#GetFunctionCalledName()# #erreurmsg# #serialize(ARGUMENTS)#”);
application.mythrow(errorCode=“400”, detail=“ERREUR #erreurmsg# #serialize(ARGUMENTS)#”);
}
And now, thanks to you, it’s working … I spent almost 3 days on that …
THANKS !
On Tuesday, November 3, 2015 at 9:32:34 AM UTC-5, Stéphane MERLE wrote:
I did my own mythrow function and added it to the application scope :
public function mythrow(errorCode, detail) {
writeLog(file=“apidg_custom_error”, text=“ERREUR GENEREE #errorCode# : #detail#”);
header statusCode=errorCode statusText=detail;
//pc = getpagecontext().getresponse();
//pc.getresponse().setstatus(errorCode, detail);
//getPageContext().getResponse().setstatus(errorCode, detail);
exit method='exittag';
}
so I call it like this : application.mythrow(errorCode=‘400’, detail=erreurmsg);
my problem is with the exit …
I have to exit from the request as an error occured but if I used exit or abort, I get a “Page request is aborted”
and if I use a return … of course the script goes on but it’s bad …
How can I stop the process ?
I still need the header to be set with either solutions :
header statusCode=errorCode statusText=detail;
//pc = getpagecontext().getresponse();
//pc.getresponse().setstatus(errorCode, detail);
//getPageContext().getResponse().setstatus(errorCode, detail);
Thanks for your help !
Stéphane
Le mercredi 28 octobre 2015 10:59:26 UTC+1, Jeroen Knoef a écrit :
If you throw an exception and don’t catch it, that’s an internal server error, so a 500 status is correct.
If you want a different status code, use: header statuscode=“400” statustext=“bad request”;
You can do this inside onerror, just don’t rethrow the exception.
You received this message because you are subscribed to the Google Groups “Lucee” group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+unsubscribe@googlegroups.com.
To post to this group, send email to lucee@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/57fcc689-47af-4fd6-80ee-559d2f99a0b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.