dremas Posted June 13, 2014 Report Share Posted June 13, 2014 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 Quote Link to comment
Roryriffic Posted September 9, 2014 Report Share Posted September 9, 2014 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. Quote Link to comment
smithd Posted September 10, 2014 Report Share Posted September 10, 2014 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? 1 Quote Link to comment
Roryriffic Posted September 11, 2014 Report Share Posted September 11, 2014 Ahhh, I knew there was a technical term for the text associated with the status code; a "like" for your nugget of wisdom, good sir. This is also what I'm doing in my web service. Write your own string and set the response code - works like a charm for me. Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.