Jump to content

Setting HTTP Response Code + String


Recommended Posts

Hi all,

I'm creating a LabVIEW Web Service in LV2013. So far it's been great, but there is one feature I can't find. The "Set HTTP Response Code.vi" doesn't take a string as an input. So I can provide an error code, but no detail string! To make matters worse, if I report a 4xx, the response body is not passed on, so I have to hack the error into a header. There must be a better way. Has anyone encountered this yet?

 

Thanks,

-O

Link to comment
  • 2 months later...

I saw that no one replied yet, so let me give it a try. I *sort of* ran into this issue.  I'm not sure what you're using on the front-end, but I find that AJAX and Javascript work wonders when talking to a web service.

 

For example, let's say I make a POST request (via AJAX) to one of my web service VI's.  Then let's say that the VI throws an error.  What I'll do is output my custom error text to a FP indicator called "statusMsg" as well as set the appropriate response code in the "Set HTTP Response Code" VI just before I exit the VI.  (NOTE: I found that you cannot input an error into the "Set HTTP Response Code" VI or else the response code does not get set.)

 

On the front-end, I can catch both the status message text AND the response code like this:

$.ajax({
	type: "POST",
	url: "/MyWebService/myPostMethod",
	// "data" can be anything (developer must define)
	// must be a corresponding input on LV web service VI
	data: dataToSendToLV, 
	statusCode: {
		// here, catch specific reponse codes. redirect user to
		// the appropriate page.
		// these scenarios execute after the "error" section below
		400: function() { window.location = "/MyWebService/badsyntax/"; }
		401: function() { window.location = "/MyWebService/unauthorized/"; },
		403: function() { window.location = "/MyWebService/forbidden/";	},
		500: function() { window.location = "/MyWebService/error/";	},
		505: function() { window.location = "/MyWebService/unsupported/";	},
	},
	success: function(resp) {
		// when the web service returns with response code "200"
		// then execute this code
		alert(resp.statusMsg);
	},
	error: function(resp) {
		// when the web service returns with response code other than "200"
		// then execute this code. NOTE that this executes before any
		// of the code in the "statusCode" section above
		alert(resp.statusMsg);
	},
}); // end $.ajax()

Hope this helps.

Link to comment

Hi all,

I'm creating a LabVIEW Web Service in LV2013. So far it's been great, but there is one feature I can't find. The "Set HTTP Response Code.vi" doesn't take a string as an input. So I can provide an error code, but no detail string! To make matters worse, if I report a 4xx, the response body is not passed on, so I have to hack the error into a header. There must be a better way. Has anyone encountered this yet?

 

Thanks,

-O

Are you referring to the "Reason-Phrase"(http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html) ( If so, I've tried everything I can think of and have been unable to come up with a way to set that field. LV seems to default to using the ones defined by http. If you just want to send data back you can always just use "set response.vi". I just used that setting the header to 401 and writing a response string ("OH NO! ERROR!" in my case) and the browser correctly displayed that string despite the error code. Are you saying this didn't work for you?

  • Like 1
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.