How to handle QUrl with spaces
-
I'm passing a file path ("C:/Desktop/My Files/Somefile.csv") to a server using QUrl. But when I print the QUrl object I see that its highlighting "C:/Desktop/My" and " Files/Somefile.csv" as something not part of Url, hence the server is failing to give back a response. How can I make it consider the entire thing as url.
-
@Abhi_Varma
Did you look at using [static]QUrl QUrl::fromLocalFile(const QString &localFile) ? What does that return for your path with spaces? -
@Abhi_Varma said in How to handle QUrl with spaces:
to a server using QUrl
What does this mean? How?
-
-
@Christian-Ehrlicher I'm sharing a file to a device via a server. For that I created a Qurl object set the path, Scheme, Host, Port for the Url. Then I created a QNetworkRequest object for the url and calling a post method of QNetworkAccessManager. Since the path has spaces in it the server cannot find it and I'm not getting a response
-
@Abhi_Varma
Did you look at using [static]QUrl QUrl::fromLocalFile(const QString &localFile) ? What does that return for your path with spaces? -
@Abhi_Varma said in How to handle QUrl with spaces:
Since the path has spaces in it the server cannot find it and I'm not getting a response
I doubt that this is the problem on the Qt side. Please show how/what you pass because 'Then I created a QNetworkRequest object for the url ' does not tell u what/how you're doing it. Do you pass a string? A json object, ...? Please show some code!
-
@Christian-Ehrlicher I have a function to which file path is passed. In that function I do something like
Say filepath = "C:\Desktop\My files\file.csv"QUrl url(ServerApiPath + "/" + filepath);
url.setSchema("http");
url.setHost("127.0.0.1");
url.setPort("8006");
qDebug() <<url;After that I create the some data and then the network request object... But when url is printed on the terminal
QUrl("ServerApiPath/C:\Desktop\My file\file.csv")
In this upto "My" it's considering as url and its highlighted as blue and not considering rest of the string(" files\file.csv") due to that space. My doubt is how to make QUrl consider the entire filepath string with space.
I have tried the above thing with the following path it works fine if my folder name doesn't have a space.
C:\Desktop\Myfiles\file.csv -
So you create an url which contains a path which is in fact a local file? I don't understand what this should do or why this is needed at all. What strange api is this?
-
@Christian-Ehrlicher it really a intermidiate server between app and device. It launches along with the app and used to communicate with devices via LAN.
But still how handle a path with a space in url. That is the issue not the server api -
You still did not show us how you send the data...
-
@Christian-Ehrlicher I can't share the code as it's a project of the company I'm working for. It's confidential. But what does that have to do I have Shared the basic issue I'm facing with QUrl that it's not considering the entire path if there are any spaces in the path. Isn't there way for this in qt to handle like by setting a mode or something
-
If you can't show us three lines on how you pass the QUrl as request to a QNetworkmanager we can't help either.
-
@Christian-Ehrlicher
QUrl url(ServerApiPath + "/" + filepath);
url.setSchema("http");
url.setHost("127.0.0.1");
url.setPort("8006");
qDebug() <<url;Qbytearray data;
"Add some string say hello world text to qbytearray"QNetworkreqyest request(url)
(QNetworkAccessManager Object).post(request, data)I'm doing this becoz we r using the concept of rest calls. Here there is no use for data for my scenario, but passing some qbytearray obj just to see if it works and also the post method needs a second argument.
Note:The above code is working if the file path doens't have a space in it
-
Since QNAM properly encodes spaces I don't see where there should be an error on the Qt side.
-
@Abhi_Varma Have you checked if the URL is valid? What do you get when you call:
qDebug() << url.isValid(); qDebug() << url.errorString();