Subscribe for automatic updates: RSS icon RSS

Login icon Sign in for full access | Help icon Help
Advanced search

Pages: [1]
  Reply  |  Print  
Author Topic: Unable to Deserialize Field Error  (Read 14709 times)
Gary C.
Posts: 109


« on: November 02, 2019, 10:13:35 am »

Hello

I need to write a web service that will cater for the uploading of a file. It is all a bit new to me and I need some help with and error I am getting. I have been using the examples in the documentation to create some test examples.

Here is the web service module, pretty much cribbed from the documentation example:
Code
  1. import com
  2. import fgl gws_pri
  3.  
  4. main
  5.  
  6.  define ret integer
  7.  
  8.  call com.WebServiceEngine.RegisterRestService("gws_pri", "pri")
  9.  display "Server started"
  10.  call com.WebServiceEngine.Start()
  11.  while true
  12.    let ret = com.WebServiceEngine.ProcessServices(-1)
  13.    case ret
  14.       when 0
  15.         display "Request processed."
  16.       when -1
  17.         display "Timeout reached."
  18.       when -2
  19.         display "Disconnected from application server."
  20.         exit program
  21.       when -3
  22.         display "Client Connection lost."
  23.       when -4
  24.         display "Server interrupted with Ctrl-C."
  25.       when -9
  26.         display "Unsupported operation."
  27.       when -10
  28.         display "Internal server error."
  29.       when -23
  30.         display "Deserialization error."
  31.       when -35
  32.         display "No such REST operation found."
  33.       when -36
  34.         display "Missing REST parameter."
  35.       otherwise
  36.         display "Unexpected server error " || ret || "."
  37.         exit while
  38.     end case
  39.     if int_flag <> 0 then
  40.       let int_flag = 0
  41.       exit while
  42.     end if    
  43.  end while
  44.  
  45.  display "Server stopped"
  46.  
  47. end main
  48.  

Here is the web service function:

Code
  1. import os
  2. import com
  3.  
  4. public define myerror record attribute(WSError="My error")
  5.  code integer,
  6.  reason string
  7. end record
  8.  
  9. public function UploadImage(theimage string attribute(WSMedia="image/*"),
  10.                            submit string)
  11.  attributes (WSPost,
  12.              WSPath="/upload",
  13.              WSDescription="Upload image file to the server",
  14.              WSThrows="400:@myerror")
  15.  returns string attribute(WSMedia="text/html")
  16.  
  17.    define sReturn string
  18.    define bOK integer
  19.  
  20.    let bOK = os.Path.DELETE("myimage.png")
  21.    let bOK = os.Path.RENAME(theimage,"myimage.png")
  22.    let sReturn = "<HTML><body><h1>Got image</h1></body></HTML>"
  23.  
  24.    return sReturn
  25.  
  26. end function
  27.  

and here is the associated html:

Code
  1. <!DOCTYPE html>
  2.   <FORM NAME="upload" method="post" action="http://localhost:8090/pri/upload" enctype='multipart/form-data'>
  3.    <div>
  4.        <label>Image File: </label>
  5.        <input type="file" name="myimage" accept="image/png" />
  6.    </div>
  7.    <br>
  8.    <input type="submit" name="submit" value="Send"/>
  9.  
  10.    </FORM>
  11. </html>
  12.  

Here is a snippet of the output, with the full text in the attached file:

Code
  1. Server started
  2.  
  3. WS-DEBUG (Receive)
  4. POST /pri/upload HTTP/1.1
  5. WS-DEBUG END
  6.  
  7.  
  8. WS-DEBUG (Receive)
  9. Host: localhost:8090
  10. Connection: keep-alive
  11. Content-Length: 5520
  12. Cache-Control: max-age=0
  13. Upgrade-Insecure-Requests: 1
  14. Origin: null
  15. Content-Type: multipart/form-data; boundary=----WebKitFormBoundary3oLWn5iowKFYOJAP
  16. User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36
  17. Sec-Fetch-Mode: navigate
  18. Sec-Fetch-User: ?1
  19. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
  20. Sec-Fetch-Site: cross-site
  21. Accept-Encoding: gzip, deflate, br
  22. Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
  23.  
  24. WS-DEBUG END
  25.  
  26. WS-DEBUG (Receive)
  27. ------WebKitFormBoundary3oLWn5iowKFYOJAP\r\nContent-Disposition: form-data; name="myimage"; filename="assess.png"\r\nContent-Type: image/png
  28. .
  29. .
  30. .
  31.  
  32. WS-DEBUG END
  33.  
  34. WS-DEBUG (Send)
  35. HTTP/1.1 400 Unable to deserialize field
  36. WS-DEBUG END
  37.  

Whereas, if I make this web service available, it works:

Code
  1. public function add2(a integer,
  2.                     b integer,
  3.                     submit string)
  4.  attributes (WSPost,
  5.              WSPath="/add2",
  6.              WSDescription="Adds A and B",
  7.              WSThrows="400:@myerror")
  8.  returns string attribute(WSMedia="text/html")
  9.  
  10.    define sReturn string
  11.    define iAnswer integer
  12.  
  13.    let iAnswer = a + b
  14.  
  15.    let sReturn = sfmt("<HTML><body>Got answer %1</body></HTML>", iAnswer)
  16.  
  17.    return sReturn
  18.  
  19. end function
  20.  

Can anyone see why the former is not working?



* debug.txt (5.77 KB - downloaded 1262 times.)
Reuben B.
Four Js
Posts: 1047


« Reply #1 on: November 06, 2019, 09:44:10 pm »

Are you missing a WSAttachment?



Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Gary C.
Posts: 109


« Reply #2 on: November 07, 2019, 02:48:17 pm »

Hi Reuben

Yes, perhaps. I see that the documentation has been updated and the example given for uploading an image in multipart has changed and does now include the WSAttachment attribute - my fault for not checking the very latest documentation.

However, I am now getting a BadHTTPRequest error (-5 being returned from com.WebServiceEngine.ProcessServices). This is using the exact code within the documentation on my local machine.

Gary
Gary C.
Posts: 109


« Reply #3 on: November 13, 2019, 09:07:08 am »

Hi

Just by way of an update - the examples that are contained in the latest version of the documentation that demonstrate a restful web service to upload a file to a server do indeed work.

Using that as the basis I have now managed to achieve my goal of creating a web component I can add to forms to provide a "drag and drop" area for uploading files to our archive management, so I and the users are very pleased!

Gary
Reuben B.
Four Js
Posts: 1047


« Reply #4 on: November 13, 2019, 08:31:57 pm »

Hi

Just by way of an update - the examples that are contained in the latest version of the documentation that demonstrate a restful web service to upload a file to a server do indeed work.

Using that as the basis I have now managed to achieve my goal of creating a web component I can add to forms to provide a "drag and drop" area for uploading files to our archive management, so I and the users are very pleased!

Gary

feel free to share more details here of your solution or perhaps on GitHub.  Or work with your support contact to pull out the important bits to create a simple example.  I know some customers are impacted when moving from desktop to web, and the inability to drop files onto a display array and don't really like the model of an openfile front-call being required before the FGL_GETFILE.

Reuben

PS Make sure you test what happens if your users just miss the drop area when attempting to drop in your web component...

Product Consultant (Asia Pacific)
Developer Relations Manager (Worldwide)
Author of https://4js.com/ask-reuben
Contributor to https://github.com/FourjsGenero
Pages: [1]
  Reply  |  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines