0

I have one URL something as below:

QUrl url("https://example.com/send/?apikey=somekey&numbers=mobile&sender=XYZ&message=0000%20is%20your%20OTP%20to%20authenticate%20with%20Server:%20abc.com")

Now I call this URL as below:

QNetworkRequest networkRequest(url);
QNetworkAccessManager manager;
QEventLoop event;
QObject::connect(&manager, &QNetworkAccessManager::finished, &event, &QEventLoop::quit);
auto* const pResponse = manager.get(networkRequest);
event.exec();
pResponse->deleteLater();
return pResponse->readAll();

However, the call doesn't succeed. Upon debugging I found out that, the "&message" part from URL which is

&message=0000%20is%20your%20OTP%20to%20authenticate%20with%20Server:%20abc.com

becomes

&message=0000 is your OTP to authenticate with Server: abc.com

Probably that's the cause of the failure. If I copy paste the URL in the browser, it works fine.

How to make this request a success?
Though I have coded as of it's a blocking call, actually my requirement is to just invoke the above URL.

Update: This problem is fixed by itself. The website, where I was making an API call, was following certain template. I was missing a fullstop "." at the end. Upon adding that, it started working.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 2
    Succeeds on my side with domain from example. Looks like XY problem to me. Due to encoding you probably see a different url in debug. Where do you see that string exactly? I ran your example and watched output from wireshark, headers are intact and request succeeds. I think it may be that your *actual* business logic fails for another reasons we can't deduce from your example. If you are getting those debug values through logging (qDebug or similar), you should check the documentation of QUrl::fromPercentEncoding, there're some caveats. More details from your side would help – vtronko Apr 03 '20 at 16:20
  • 2
    use `QUrl url = QUrl::fromUserInput("https://example.com/send/?apikey=somekey&numbers=mobile&sender=XYZ&message=0000%20is%20your%20OTP%20to%20authenticate%20with%20Server:%20abc.com");` – eyllanesc Apr 03 '20 at 17:52
  • @vtronko, eyllanesc probably your guess of XY problem is right. I missed one dot while calling the API and it was being rejected for the same. If either of you want to post it as an answer then I will be happy to accept. – iammilind Apr 05 '20 at 02:48
  • Glad you figured it out. Would it be a solution for anyone coming upon your question, though? – vtronko Apr 05 '20 at 09:56
  • @vtronko, yes the code as is working with no problem from Qt. It was an API provider issue. – iammilind Apr 06 '20 at 00:18
  • @iammilind, yeah, my point being, since this is issue on API provider side, this topic would not be relevant for anyone coming upon it :) So will be the "answer". It may confuse people. but if there's no answer they come to comments section and see that problem was something else. Cheers – vtronko Apr 06 '20 at 01:20

0 Answers0