Invalid path Exception Unable to run Test in WebService | Upload file

Error on service request, have updated the variable in profile as used variable in filepath
Works well with if direct path applied(without variable)

used \\ in variables declared as fpath

image
image
still getting exception

image

java.nio.file.InvalidPathException: Illegal char <:> at index 2: [C:\Users\Myside\Documents\Uploadfiles\test.txt]
	at com.kms.katalon.core.util.internal.PathUtil.relativeToAbsolutePath(PathUtil.java:25)
	at com.kms.katalon.core.testobject.internal.impl.HttpBodyContentReader.lambda$0(HttpBodyContentReader.java:64)
	at com.kms.katalon.core.testobject.internal.impl.HttpBodyContentReader.fromSource(HttpBodyContentReader.java:68)
	at com.kms.katalon.controller.WebServiceController.getRequestObject(WebServiceController.java:88)
	at com.kms.katalon.controller.WebServiceController.sendRequest(WebServiceController.java:149)
	at com.kms.katalon.composer.webservice.parts.RestServicePart$8.run(RestServicePart.java:282)
	at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:119)

What is the test STEP that is failing? Show us more of the error…

Are you running on Windows?

1 Like

yes windows 10, is that a bug for windows ?

No. Just checking. Once again it seems to indicate the colon is a problem but once again it’s getting the index wrong - before it was 3, now it’s 2. It should be 1.

Try switching to forward slashes - you don’t need 2 or 4, just one "/"

1 Like

3? when… when ever the \ or \ is updated before colon, the index value says the same
if for e.g i used \c:\ (then its 3)
like

java.nio.file.InvalidPathException: Illegal char <:> at index 3: [\C:\Users\gmypath\Documents\Document_Loan_Upload_Repo\Docs_for_uploadfile\test.txt]

FORWARD slashes…

C:/users/...

1 Like

Contrary to popular belief, forward slashes work on Windows.

image

Whether java.nio allows them is another matter entirely…

1 Like

image

It’s reading \u as an escaped character.

1 Like

tried this also didnt work
image

Okay, let’s get some more eyes on this…

@duyluong @ThanhTo @kazurayam @Brandon_Hein

1 Like

its a Bug confirmed
if used variable as String image it works
but with list it is not working
image

java.nio.file.InvalidPathException: Illegal char <:> at index 2: [C:\Users\Myside\Documents\Uploadfiles\test.txt]
	at com.kms.katalon.core.util.internal.PathUtil.relativeToAbsolutePath(PathUtil.java:25)

A string literal [C: ... ] contains a colon character : at the index 2.

index 0 → [
index 1 → C
index 2 → :

This is the whole reason why you got the Exception. I guess internally there runs a code like this:

def path = valueAssignedInTheGUI         // e.g, '[C: ...'
...
PathUtil.relativeToAbsolutePath(path.toString(), ...)

this will cause the method to raise an Exception if the valueAssignedInTheGUI variable is of type List.

You can read the source code of PathUtils class, and you will find String public static String relativeToAbsolutePath(String relativePath, String projectFolderPath) requires 1st argument to be a string as a valid relative file path. But [C: is not a valid relative file path. Therefore an Exception is raised.

Katalon Studio seems to offer List and File type as variable type but I guess KS is not defensive enough. When a variable fpath is declared as a String, and if the value given is [C: ..., then Katalon Studio GUI should check the validity, find error, and show a modal dialog which warns (before executing the test script)

“a valid file path string is required, but [C: … is not, it looks like a List literal. Type mismatch”

It is too late to raise an Exception saying shortly Illegal char <:> at index 2 that puzzle us.


By the way, I do not see why @gsp wrote [C: ... as the Value for the variable fpath.

And I do not see which data type @gsp wants to declare for the variable fpath; String, List? He should chose one. Otherwise he can not decide how he should write a literal as its value: C:... or ["C:..."].

By the way, if he chooses File type, then how he should write the value in the GUI field? — I have no idea, as Java/Groovy language dos not define “a literal for File type”.

Conclusion:

I would suggest to him: don’t use List and File type as they introduce more troubles than helps. Use String. Simplicity is golden.

2 Likes

By the way, we can read the source of com.kms.katalon.core.util.internal.PathUtil class at

When I found this, I wondered why Katalon Team reinvented a wheel; as you know, Java 7 supported java.nio.file.Path.relativize(Path other) and toAbsolutePath() . Possibly the PathUtil class was initially written before Java7 which was released July 2011.

1 Like

@kazurayam Thanks for your comment an info

I was trying to automate the with multiple files for upload file scenario
With single file only it works well both directly providing the path and with String
I want the TestCase for same service request to be iterated and execute for upload file for

  1. doc
  2. jpeg
  3. pdf
  4. txt

Any help from you ? :slight_smile:

You should make a search in this forum with keyword “upload multiple files”.

I found a few topics, for example:

If you are to take this approach, in GUI, you are supposed to use String type for the fpath variable, you would not use List.

I haven’t tried this approach. But, I guess, you would find a problem. WebUI.uploadFile() keyword requires 2nd argument of String, which contain multiple file paths delimited by a NEWLINE character: path1 + “\n” + path2 + “\n” + …

How, in the GUI field, can you insert a NEWLINE (\n) character in the string value : path1 + “\n” + path2? I do not know any immediate answer to this. Please try to help yourself.

I think WebUI.uploadFile() keyword is poorly designed. It should rather accept 2nd argument of List<String>.

@gsp
You may want to invent your own custom keyword which accepts 2nd argument of List, for example

class MyUploader {

    static def uploadFiles(TestObject tObj, List<String> paths) {
        String pathlist = paths.join("\n")
        WebUI.uploadFile(tIObj, pathlist)
    }
}
1 Like

I noticed, @gsp wants to work on WS, rather than WebUI. He wants to attach multiple files in a WebService request.

I searched this forum and found some relevant topics. For example.

@gsp would want to develop a Tese Case and a custom Keyword that drives RestRequestObjectBuilder.withMultipartFormDataBodyContent().

@gsp has got fair amount of development works to do. Enjoy!

1 Like