RejectedSoftware Forums

Sign up

How send a different HTTP status with a post of a REST API?

I want to use some REST APIs but I don't understand what need to be done to be able to change the HTTP status of a post service.

I actually have :

interface MyAPI
{
	void	postNewsletter(string email);
}

class MyAPIImplementation : MyAPI
{
	void	postNewsletter(string email)
	{
		registerToNewsletter(email);
	}
}

How I can manipulate the HTTPServerResponse will be sent to the client for the postNewsletter method?

Re: How send a different HTTP status with a post of a REST API?

On Fri, 14 Nov 2014 08:12:26 GMT, Flamaros wrote:

I want to use some REST APIs but I don't understand what need to be done to be able to change the HTTP status of a post service.

I actually have :

interface MyAPI
{
	void	postNewsletter(string email);
}

class MyAPIImplementation : MyAPI
{
	void	postNewsletter(string email)
	{
		registerToNewsletter(email);
	}
}

How I can manipulate the HTTPServerResponse will be sent to the client for the postNewsletter method?

The REST generator will returns 200 by default (or 204 if there's no content). A way to return another code is to throw an HTTPStatusException if you have an error.

Could you provide more info about your use case / what you are trying to achieve ?

Re: How send a different HTTP status with a post of a REST API?

On Fri, 14 Nov 2014 12:56:21 GMT, Mathias LANG wrote:

On Fri, 14 Nov 2014 08:12:26 GMT, Flamaros wrote:

I want to use some REST APIs but I don't understand what need to be done to be able to change the HTTP status of a post service.

I actually have :

interface MyAPI
{
	void	postNewsletter(string email);
}

class MyAPIImplementation : MyAPI
{
	void	postNewsletter(string email)
	{
		registerToNewsletter(email);
	}
}

How I can manipulate the HTTPServerResponse will be sent to the client for the postNewsletter method?

The REST generator will returns 200 by default (or 204 if there's no content). A way to return another code is to throw an HTTPStatusException if you have an error.

Could you provide more info about your use case / what you are trying to achieve ?

I want display a banner with various messages corresponding to the status code:
200 successfully registered
201 operation failed
409 email already registered

Re: How send a different HTTP status with a post of a REST API?

On Fri, 14 Nov 2014 15:19:19 GMT, Flamaros wrote:

On Fri, 14 Nov 2014 12:56:21 GMT, Mathias LANG wrote:

Could you provide more info about your use case / what you are trying to achieve ?

I want display a banner with various messages corresponding to the status code:
200 successfully registered
201 operation failed
409 email already registered

So you have user-facing code on page A, which will request this API and display the banner according to the API return code, am I right ?

Then you can just throw HTTPStatusException with 201 or 409 in the API. 200 Will be the default. However, you should not use 201 to notice of a failure. 200's are success codes. 400's are for client error, and 500's for server error. So you should probably replace 201 with 400.

Re: How send a different HTTP status with a post of a REST API?

On Sat, 15 Nov 2014 09:03:57 GMT, Mathias LANG wrote:

On Fri, 14 Nov 2014 15:19:19 GMT, Flamaros wrote:

On Fri, 14 Nov 2014 12:56:21 GMT, Mathias LANG wrote:

Could you provide more info about your use case / what you are trying to achieve ?

I want display a banner with various messages corresponding to the status code:
200 successfully registered
201 operation failed
409 email already registered

So you have user-facing code on page A, which will request this API and display the banner according to the API return code, am I right ?

Then you can just throw HTTPStatusException with 201 or 409 in the API. 200 Will be the default. However, you should not use 201 to notice of a failure. 200's are success codes. 400's are for client error, and 500's for server error. So you should probably replace 201 with 400.

You are right, that exactly what I am doing.

For sure I didn't look precisely to the http codes, I don't remember which values I'll have to use.
I think it's better with http status codes cause it's standard instead of my own values in an enum. When reading javascript code it will simpler to understand.