Uniface User Forum

 View Only
Expand all | Collapse all

Rest WS with JSON parameter

  • 1.  Rest WS with JSON parameter

    Posted 10-14-2020 20:59

    Hello,

    I'm trying to called a REST API (created by us) that return client information with UHTTP.

    But i need to send the REST API a customer (in JSON format) but i can't seem to make it work.

    Here's what i have so far :

    I'm creating a struct to define the customer number :

    v_getMigrationStatus = $newstruct
    v_getMigrationStatus->customerAccountNumber = $newstruct
    v_getMigrationStatus->customerAccountNumber = "1324567890"


    Then, i transform the struct in json :

    structToJson vjson, v_getMigrationStatus


    I'm creating the headers for the call :

    putitem/id/case vHeaders, "Content-Type", "application/json"


    then, finally, i activate the UHTTP :

    activate "UHTTP".SEND("myUrl","GET","","",vHeaders,vjson,vResponse)


    The called is working but i'm receiving all customer information, not just about the the one being sent?

    From my understanding, the content parameter in UHTTP, is the one to use to pass parameter to the body section? Maybe i am mistaken.

    What i am doing wrong? How can i send the customer number in JSON format to the REST API WS and receive only information about the customer i want?


    Thanks!



  • 2.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 06:03

    Hi Pierre,

    From your description I understand the communication between your Uniface application and your REST service is working, also if not as you're expecting.

    You have described the Uniface side of this communication but my gotfeel is your problem could also be on the server side. Your REST service should filter based on what is receiving as request and probably is NOT doing it properly or is not receiving the filter information where expected.

    Check your REST services, it should:
    - somehow get your filter (CustomerID) from content received call (Address, Content, Header)
    - extract only the requested customer(s)
    - pack and send back only retrieved customer

    Check your Uniface application:
    - is it sending filter to customer where expected from REST service?

    Hope it helps!

    Gianni



  • 3.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 06:53

    Hi

    I agree with Gianni, check in your REST that body receives any value. I guess with method GET, the body is ignored (following HTTP specs).


    If so, I sugest to call with GET myURL/customerAccountNumber/1324567890

    In the USP that implements the RESTful, $webinfo(PATHINPUT) should give you "customerAccountNumber·;1324567890"


    Hope it helps!

    Luis



  • 4.  RE: Rest WS with JSON parameter

    PARTNER
    Posted 10-15-2020 08:29

    I don;t know if it makes a difference, but I'm pretty sure the line 

    v_getMigrationStatus->customerAccountNumber = $newstruct

    is redundant, you don't need to declare a scalar as a newstruct before assigning it. 

    Can you copy in the JSON frim vJSON? And do you have a sample which works outside of Uniface? 



  • 5.  RE: Rest WS with JSON parameter

    PARTNER
    Posted 10-15-2020 08:30

    There is a piece of freeware called SoapUI which you can use to test both SOAP and REST web services, which will allow you easier methods of checking what is, and isn't, the right thing to be sending, then you can see what does and doesn't match in your encoded data for the UHTTP. 



  • 6.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 13:21

    I used postman and the GET is working if i submit a account number and received everything (from every customer) when i don't submit an account number like it's doing in uniface.




  • 7.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 13:59

    Hi Pierre,

    it seems your REST service is NOT recognizing your selection parameter; are you sure you are passing it (the AccountNumber) in the correct way as expected from your REST service?

    In my experience a parameter to a REST service could be passed:
    - as part of the Content (Body)
    - as part of the Header
    - as part of the Address

    In your example you are passing it in the Content/Body. Is that right?

    If yes, is the JSON received from the REST service having the requested format/structure?

    Gianni



  • 8.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:02

    Hello! 

    Yes, i'm passing the accountNumber via the content parameter.




  • 9.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:08

    But method GET does not support data in body (or it shouldn't). Although, you can send it and the server can ignore the Standard.
    Probably, UHTTP component is following this RFC and ignoring body in case of GET method.





  • 10.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:12

    Hi Pierre,

    is selection in the body part of the calling message what your REST service is REALLY expecting?

    if yes, is the structure of your received JSON correct? You could compare JSON from Uniface with JSON from another caller if your REST service support a log.

    Gianni




  • 11.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:18

    Hello,

    Yes, the service is expecting the parameter in the body part.

    I'm being told that is not standard but it's had been done this way because we can pass up to 13 parameter to the service so the URL would be too long.


    So, if i understand what you're saying; because the GET does not allow parameter in the body section, UHTTP is ignoring the parameter when calling the REST service?


    Also, i checked the log (what we have as log) on the server side and when i called it with Uniface, it does a select all as if the parameter is being ignored.



  • 12.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:37

    Hi Pierre,

    you can get more informations looking for Issue #31200 in the support area.

    Gianni




  • 13.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 14:55

    I suggest to check whether curl is able to send body data using GET method. If so, install curl.exe in bin folder (it will use libcurl.dll and others already in place), then replace activate UHTTP by activate OperatingSystem service with appropriate curl command line to get the result. Maybe giving curl an output parameter to a file that you will need to load from Uniface, afterwards.

    To be precise, watch the properties of libcurl.dll in bin folder and search for same version curl.exe (7.40?).

    Or, modify webservice to receive url-encoded JSON through query-string, and continue using UHTTP.



  • 14.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 17:51

    Thansk everyone for your help!

    As mentionned in issue #31200, the GET method ignore headers and content. So we modified the REST service to allow the url to get the customerAccountNumber as a parameter and it worked.


    Also i tried the REST service with method "POST" and got it working to update data with 3 JSON parameter and it was working. We also considered creating a POST method to get the data but we taught it would not be a good idea to do so.


    Once again, thanks everyone!




  • 15.  RE: Rest WS with JSON parameter

    Posted 10-15-2020 18:38

    Hi Pierre,

    if your REST service has many parameters you could always receive them in a GET as part of Headers; calling it from a Uniface application you should have care to call set_flags(8) before send().

    Gianni