• Recent
    • Tags
    • Popular
    • Register
    • Login

    Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

    Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website Mango 5 Documentation Website

    How to negotiate a Plain text response from Mango 3.6 Rest API

    User help
    3
    18
    3.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • raylatbasixR
      raylatbasix
      last edited by

      Rather new to the Rest API. Looking for help on changing the Response type when I request the following point Value URL:
      http://localhost:8080/rest/v2/point-values/latest/DP_059c7915-c0e7-48dc-b9e3-28ca8b92da01?fields=VALUE&limit=1&useCache=NONE
      Returns JSON:

      [ {
        "value" : "Community Center"
      } ]
      

      Instead, I need to strip off any JSON characters, and simply recieve a plain text response with the following:

      Community Center
      

      Any help on What I need to do to accomplish this, would be appreciated.

      1 Reply Last reply Reply Quote 0
      • phildunlapP
        phildunlap
        last edited by

        Hi raylatbasix,

        Can you say more about the context? If there a reason whatever is making the API request cannot handle the JSON as it wishes to use it?

        There is not currently an endpoint that returns a response body as you seek it. But you could certainly do the equivalent of JSON.parse(response.text)[0].value upon receiving it

        1 Reply Last reply Reply Quote 0
        • raylatbasixR
          raylatbasix
          last edited by raylatbasix

          Hi Phil,

          There is a third-party app looking for plain text, when it query's another rest server's URL. This third-party app is currently connecting to a DGLux5 Rest server service which is responding with the plain text that the third-party app needs. I am looking to move to the Mango Rest API in order to eliminate the DGLux5 from the equasion. I am not able to modify the script on the third-party app that is currently looking for plain text, so I need a solution on the Mango side. Any ideas as to how I would go about creating a script that would accomplish stripping the JSON down to just the value alone being returned from the the third-party apps URL Get Request? Thanks for the help.

          1 Reply Last reply Reply Quote 0
          • phildunlapP
            phildunlap
            last edited by phildunlap

            Hmm. So can you control the URL it is getting from, or you need that endpoint to supply plain text?

            Edit: Nevermind, I see that you arrived at that URL probably through playing around since the DGLux 5 API wouldn't be available

            1 Reply Last reply Reply Quote 0
            • raylatbasixR
              raylatbasix
              last edited by

              Hi Phil,

              I can update the URL on the third-party app, Just not modify any scripting on that side.

              1 Reply Last reply Reply Quote 0
              • phildunlapP
                phildunlap
                last edited by phildunlap

                I think there's two solutions here. The better would be to write a REST endpoint for what you're looking to do, the faster / dirtier way would be a virtual serial server socket and a script mocking up HTTP to send out the TCP connection. So, let's try out the better solution!

                Presumably your question shows you do not need timestamps, you do not need anything other than one XIDs current (rendered?) value.

                We can find a lot of existing examples of API endpoints in the Mango API module on Github, although other modules do provide their own rest controllers. https://github.com/infiniteautomation/ma-modules-public/tree/main/Mango API

                In this case, I'm just going to make a new rest controller to house this one method (one could of course add other methods to it),

                package com.infiniteautomation.mango.rest.v2;
                
                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.http.ResponseEntity;
                import org.springframework.security.core.annotation.AuthenticationPrincipal;
                import org.springframework.web.bind.annotation.PathVariable;
                import org.springframework.web.bind.annotation.RequestMapping;
                import org.springframework.web.bind.annotation.RequestMethod;
                import org.springframework.web.bind.annotation.RestController;
                
                import com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException;
                import com.serotonin.m2m2.Common;
                import com.serotonin.m2m2.db.dao.DataPointDao;
                import com.serotonin.m2m2.db.dao.PointValueDao;
                import com.serotonin.m2m2.rt.dataImage.PointValueTime;
                import com.serotonin.m2m2.view.text.TextRenderer;
                import com.serotonin.m2m2.vo.DataPointVO;
                import com.serotonin.m2m2.vo.User;
                import com.serotonin.m2m2.vo.permission.Permissions;
                
                import io.swagger.annotations.Api;
                import io.swagger.annotations.ApiOperation;
                
                /**
                 * @author Phillip Dunlap
                 */
                @Api(value = "Custom Mango Endpoints")
                @RestController("CustomizedRestController")
                @RequestMapping("/custom")
                public class CustomizedRestController {
                    private final PointValueDao dao = Common.databaseProxy.newPointValueDao();
                    
                    @Autowired
                    public CustomizedRestController() { }
                    
                    @ApiOperation(
                            value = "Get the current rendered value of a data point",
                            notes = "Custom endpoint for raylatbasix on the forum"
                            )
                    @RequestMapping(method = RequestMethod.GET, value = "/latest-value-only/{xid}", produces = "text/plain")
                    public ResponseEntity<String> getLatestRenderedValueOnly(
                            @PathVariable("xid") String xid,
                            @AuthenticationPrincipal User user
                            ) {
                        
                                DataPointVO dataPoint = DataPointDao.getInstance().getByXid(xid);
                                if (dataPoint == null) {
                                    throw new NotFoundRestException();
                                }
                                DataPointDao.getInstance().loadPartialRelationalData(dataPoint);
                
                                Permissions.ensureDataPointReadPermission(user, dataPoint);
                                
                                //Cache was false in the original post, so go through DAO, not runtime manager
                                PointValueTime pvt = dao.getLatestPointValue(dataPoint.getId());
                                String renderedValue;
                                if(pvt != null) {
                                    renderedValue = dataPoint.getTextRenderer().getText(pvt.getValue(), TextRenderer.HINT_FULL);
                                } else
                                    renderedValue = null;
                                
                                return ResponseEntity.ok(renderedValue);
                    
                    }
                }
                

                This class would provide such an endpoint. To get it to load, you could....

                cd /oath/to/Mango/web/modules/mangoApi/lib;
                #**get that code into CustomizedRestController.java in this directory
                
                #create directories to match the package path of the class
                mkdir -p com/infiniteautomation/mango/rest/v2
                
                #compile the class
                javac -cp /path/to/Mango/lib/*;/path/to/Mango/web/modules/mangoApi/lib/* CustomizedRestController.java
                
                #copy it to the package path directory
                cp CustomizedRestController.class com/infiniteautomation/mango/rest/v2/
                
                #add it to the Mango API jar
                jar uf mango-api-3.6.0.jar com/infiniteautomation/mango/rest/v2/CustomizedRestController.class
                
                #start Mango, check it out in swagger
                

                Adding it into the jar would have to be redone when upgrading the API module, though.

                1 Reply Last reply Reply Quote 0
                • raylatbasixR
                  raylatbasix
                  last edited by

                  Hi Phil,
                  Thanks so much for offering a solution!! I believe I understand what I need to do.
                  However, When I try to compile the class in Windows 7 using the following command:

                  C:\Program Files\Java\jdk1.8.0_144\bin>javac -cp C:\Mango356\lib\*; C:\Mango356\
                  web\modules\mangoApi\lib\CustomizedRestController.java
                  

                  I Recieve the following error:

                  C:\Mango356\web\modules\mangoApi\lib\CustomizedRestController.java:11: error: pa
                  ckage com.infiniteautomation.mango.rest.v2.exception does not exist
                  import com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException;
                                                                       ^
                  1 error
                  

                  I had copied the CustomizedRestController.java to my "C:\Mango356\web\modules\mangoApi\lib" directory,
                  and also created the folders "com\infiniteautomation\mango\rest\v2" within the "C:\Mango356\web\modules\mangoApi\lib" directory as well. I'm sure I'm missing something. I do not do much java compiling.

                  Thanks again for the help!

                  1 Reply Last reply Reply Quote 0
                  • phildunlapP
                    phildunlap
                    last edited by

                    The javac command doesn't look like mine. My -cp argument has no space in it, and has the Mango API lib directory as the second classpath location (they are semicolon delimited), and I did not provide an absolute path to the CustomizedRestController.java since I was in the same directory (but the issue with the class not found is to do with no API in the classpath argument).

                    1 Reply Last reply Reply Quote 0
                    • raylatbasixR
                      raylatbasix
                      last edited by raylatbasix

                      Thanks Phil,

                      I was able to get it to compile by switching to the C:\Mango356\web\modules\mangoApi\lib directory, and then use the following command:

                      javac -cp C:\Mango356\lib\*;C:\Mango356\web\modules\mangoApi\lib\* CustomizedRestController.java
                      

                      I then copied the CustomizedRestController.class file that was created, to the C:\Mango356\web\modules\mangoApi\lib\com\infiniteautomation\mango\rest\v2 directory.

                      I then ran jar uf mango-api-3.6.0.jar com/infiniteautomation/mango/rest/v2/CustomizedRestController.class and did not recieve any errors at all. I then restarted Mango, and went into swagger, but fail to see any URL path t o access this custom endpoint. I tried accessing: http://localhost:8080/rest/v2/custom/latest-value-only/DP_059c7915-c0e7-48dc-b9e3-28ca8b92da01?fields=VALUE&limit=1&useCache=NONE but got a "page not found" error. Again, thanks for all the help!

                      1 Reply Last reply Reply Quote 0
                      • raylatbasixR
                        raylatbasix
                        last edited by

                        Hi Phil,

                        Nevermind! Apparently the command:

                        jar uf mango-api-3.6.0.jar com/infiniteautomation/mango/rest/v2/CustomizedRestController.class
                        

                        did not actually take the first time. I think Mango was running by mistake, when I ran it the first time.

                        It's actually all working now, and I see the plain text data that I need for my application!!

                        Brilliant as always Phil!!

                        Thanks again!!

                        1 Reply Last reply Reply Quote 0
                        • phildunlapP
                          phildunlap
                          last edited by

                          :D Glad to hear it, I was trying to figure out what could have gone awry as I had tested it before posting!

                          Happy hacking!

                          1 Reply Last reply Reply Quote 0
                          • raylatbasixR
                            raylatbasix
                            last edited by

                            On a side note to this application, When I use this Rest URL from my third-party app on the same network, Will I recieve any authentication challenges "Auth Tokens" before Mango will display the data? Right now, It works fine in my localhost browser, but I'm not sure about a remote application.

                            1 Reply Last reply Reply Quote 0
                            • terrypackerT
                              terrypacker
                              last edited by

                              @raylatbasix all endpoints located below /rest are restricted to authenticated users. For a 3rd party app I would suggest token authentication which is quite easy to use from the client side by including the token in the Authorization header as shown on this page:

                              https://help.infiniteautomation.com/mango-rest-api-authentication?rq=token

                              Ensure you use https to protect the token.

                              1 Reply Last reply Reply Quote 0
                              • raylatbasixR
                                raylatbasix
                                last edited by

                                Hi Terry,
                                Sorry for being a newbie, but How would I pass this JWT Auth token within a request URL to the Rest API?

                                1 Reply Last reply Reply Quote 0
                                • phildunlapP
                                  phildunlap
                                  last edited by phildunlap

                                  The header is Authorization, the value is Token <token value> like
                                  Authorization: Bearer <token value> and you can generate auth tokens for users on the users page.

                                  1 Reply Last reply Reply Quote 0
                                  • phildunlapP
                                    phildunlap
                                    last edited by

                                    Whoops, wrote that wrong, don't trust the email notification text!

                                    Authorization: Bearer <token value>

                                    1 Reply Last reply Reply Quote 0
                                    • raylatbasixR
                                      raylatbasix
                                      last edited by raylatbasix

                                      Hi Phil,

                                      Yes, I saw that, and forgive my ignorance, I can generate user tokens. My problem is that I can only modify the URL on my third party app. I am not understanding how to pass this authorization token from within my request URL.

                                      Sorry for the crazy question :)

                                      EDIT: In other words, what is my URL's format for reqesting data? ie:

                                      http://localhost:8080/rest/v2/custom/latest-value-only/DP_e64bb726-ddd4-4065-8972-21c9441af408?access_token=<auth token here>??????
                                      
                                      1 Reply Last reply Reply Quote 0
                                      • raylatbasixR
                                        raylatbasix
                                        last edited by

                                        Good Morning Phil,

                                        You can cancel this request, I was able to solve my Authorization Request Header issue.

                                        Thanks again for all the help!!

                                        1 Reply Last reply Reply Quote 0
                                        • First post
                                          Last post