adamski / RestRequest

JUCE module for making HTTP requests to REST API's
MIT License
63 stars 8 forks source link

unsupported URL error #6

Open dataexcess opened 2 years ago

dataexcess commented 2 years ago

I'm trying to fetch some data of a simple rest api, but I always get a "unsupported URL error" even when using https://www.google.com as the URL...

here's my code:

void ScopeAudioProcessorEditor::tryRESTCall()
{
    RestRequest request;
    request.header ("Content-Type", "application/json");
    RestRequest::Response response = request.get ("https://jsonplaceholder.typicode.com/todos/1").execute();
    DBG ("REST API bodyAsString: " + response.bodyAsString);
}

This is the error:

2022-04-22 17:03:41.969742+0200 Live[5602:150721] Task <FB2F127C-A29C-42D0-82E5-0BA726F84FF3>.<1> finished with error [-1002] Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=/https://jsonplaceholder.typicode.com/todos/1, NSErrorFailingURLKey=/https://jsonplaceholder.typicode.com/todos/1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <FB2F127C-A29C-42D0-82E5-0BA726F84FF3>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <FB2F127C-A29C-42D0-82E5-0BA726F84FF3>.<1>, NSUnderlyingError=0x60000130ba50 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=/https://jsonplaceholder.typicode.com/todos/1, NSErrorFailingURLKey=/https://jsonplaceholder.typicode.com/todos/1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <FB2F127C-A29C-42D0-82E5-0BA726F84FF3>.<1>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <FB2F127C-A29C-42D0-82E5-0BA726F84FF3>.<1>, NSUnderlyingError=0x60000130ba50 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}

Any ideas? Thnx

adamski commented 2 years ago

Which platform are you running this on?

dataexcess commented 2 years ago

macOS 12.3.1 XCode

adamski commented 2 years ago

What do you get when you call getBodyAsString or getURL on the Request object?

dataexcess commented 2 years ago

It gives the same error :(

adamski commented 2 years ago

I mean before you make the call. On the request object, not the response.

dataexcess commented 2 years ago

I could fix the issue by bypassing the url.getChildURL(endpoint) method call in the exectue() method. This seems to add a slash in front of my url...

    RestRequest::Response execute ()
    {
        auto urlRequest = URL(endpoint);
        //auto urlRequest = url.getChildURL (endpoint);
        bool hasFields = (fields.getProperties().size() > 0);
        if (hasFields)
        {

So now it works! thnx

adamski commented 2 years ago

Can you please post the results of getBodyAsString and getURL on the Request object? (before you made your change) - so I can understand the issue.

I suspect it's not expecting a full URL string with http at the front..

dataexcess commented 2 years ago

I called getBodyAsString on the Request object and it gives the same error. Before and after the execute method.

conradfr commented 6 months ago

I think the problem is the prefix slash that Juce::URL adds in concatenatePaths() which is called by URL::getChildURL(), itself called by RestRequest::Response execute().

As you can see in your error : NSErrorFailingURLStringKey=/https://jsonplaceholder.typicode.com/todos/1

I don't really understand the logic of the code and why it works on Windows and not MacOS though.