Evan
Evan

Reputation: 41

How to fix JSON-RPC error code 591 with swift

I am making an iOS app with Swift that I want to use with a server that has JSON-RPC (server is not owned by me). I have been able to login to the server and save the cookies which I know are working because of being able to view content on other parts of the server that are only viewable when authenticated, however when I try to use the JSON-RPC it throws me an error which says

{\"id\":3,\"error\":{\"code\":591,\"msg\":\"method with the requested number of arguments not found (session may have timed out)\"}}

The JSON-RPC works for logging in and listing all methods, so I think that there must be an issue with ones that require you to be logged in

I've tried to fix this by putting in quotation marks into the params field, but this also did not work. When I login I have to submit the request body I have to use a string instead of a dictionary.

The code I use to send the request is this:

    guard let url = URL(string: "JSON-RPC URL") else { return; };
    let params: Data? = "{id:3,method:\"mailDelegate.getMailData\",params:[]}".data(using: .utf8);
    var request = URLRequest(url: url);
    request.httpMethod = "POST";
    request.httpBody = params;
    request.addValue(cookies, forHTTPHeaderField: "Cookie");
    let session = URLSession.shared;
    print("Sending request with cookies of " + cookies);
    session.dataTask(with: request) { (data, response, err) in
        print(String(data: data, encoding: .utf8));
    }.resume();

When I do this I expect it to show the email data (which can be seen if I use chrome developer tools and copy the request as curl and paste it in terminal), but instead it gets the error that I have shown in the first part of the question.

EDIT 1: I have found that the JSESSIONID which my app collects is not "authenticated" so to speak. I copied the original curl request from using my browser and it works just fine, then I replaced the JSESSIONID with one from my app and it stops working, so is this a problem with how I am using JSON-RPC? Is there any form of "authentication"/verification that I need to do before using it?

EDIT 2: I have found that when I open the website in Chrome and replace the JSESSIONID with my apps sessionid then click on the mail page, the request in my app works, which again leads me to believe that there is some sort of authentication that is occurring when I load the page.

Upvotes: 0

Views: 712

Answers (1)

Evan
Evan

Reputation: 41

Ok so I know this is late but to anyone that wants to know how I fixed it or if any of you have the same error, I was able to fix this by sending a GET request to the Mail Page of the website before trying to use mailDelegate, because I guess the website doesn't initialize the delegate until the page is loaded under that session.

Upvotes: 1

Related Questions