Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help debugging API call #592

Closed
davidbannon opened this issue Aug 25, 2020 · 9 comments
Closed

Help debugging API call #592

davidbannon opened this issue Aug 25, 2020 · 9 comments

Comments

@davidbannon
Copy link

Hi Folks, like the project !
I am trying to use the API from a client of my own, while I have no trouble with GET, getting the full list of notes, I cannot POST, post a new note.

I am posting to http://192.168.1.137/nextcloud/index.php/apps/notes/api/v1/notes and sending

{"title":"Some New Note","content":"This is stuff in a new note",}

The new note is created, I get back the new note's ID but its title and content are ignored. The note is created but has a blank title and content.

I can see the post arrive in Apache, DumpIO shows me -
[Tue Aug 25 12:05:52.287857 2020] [dumpio:trace7] [pid 6127] mod_dumpio.c(100): [client 192.168.1.250:37448] mod_dumpio: dumpio_in (data-HEAP): =%7B%22title%22%3A%22Some%20New%20Note%22,%22content%22%3A%22This%20is%20stuff%20in%20a%20new%20note%22,%7D

which is what I would have expected. I am experiencing the same problem on both a multipass snap based thing and a properly installed system.

Can someone suggest how I can debug further ? I have no history with Nextcloud at all !

Davo

@jeffslofish
Copy link

Maybe try removing the trailing comma at the end?

@davidbannon
Copy link
Author

Thanks Jeff, no, I have tried it both with and without the comma.
Davo

@davidbannon
Copy link
Author

I just realized that I trigger a error that shows up in nextcloud's logging each time I send the above -

Error PHP session_start(): A session had already been started - ignoring at /var/www/nextcloud/lib/private/Session/Internal.php#209

sounds like usefull information......

@korelstar
Copy link
Member

You can safely ignore the session error message. It's a bug in Nextcloud server (see nextcloud/server#20490) and was fixed in nextcloud/server#22243 (will be part of 19.0.2).

Please provide some code if you want us to help. I'm wondering about the URL-encoded JSON in the DumpIO output. That may be the problem.

Please also try to use curl in order to test the behavior of the API. You can create a note with the following curl command line:

curl -u USER:PASSWORD -X POST -H "Content-Type: application/json" -d '{"title": "Test note", "content": "My awesome note"}' https://YOURSERVER/index.php/apps/notes/api/v1/notes

@davidbannon
Copy link
Author

davidbannon commented Aug 25, 2020

Hmm, thanks korelstar, not sure you will want to see my code, its in Free Pascal. Below.
I though you had it with the Content-Type: application/json, I had forgotten that and quickly added it to my header but it still did not work. But now my json string is no longer encoded in the dumpio log ??? Not sure if thats a good thing or not !

And my content is marked, in the logs as application/x-www-form-urlencoded\r\n - so thats clearly wrong.

But interesting, your curl line (with my URL) does not work either. But it does at least set the content type to application/json. So, I think I need to concentrate on getting that content type correct from my (free pascal) client.

I have quite a different URL to you but as I can get the GET to work, don't think thats the issue here.

The code ? really ? OK.

Client := TFPHttpClient.Create(nil);
Client.AddHeader('User-Agent','Mozilla/5.0 (compatible; fpweb)');
Client.AddHeader('Content-type','application/json');
Client.AllowRedirect := true;
Client.UserName:=USER;
Client.Password:=PW;
Response := TStringStream.Create('');
try
    try
        Client.FormPost(TheURL,  Params, Response);
        DumpResponse(Response.DataString);
    except on E:Exception do begin
            Result := 0;
            writeln(' bad things happened');
            writeln(E.Message);
        end;
    end;
finally                                                

Thanks for your interest !

Davo

@davidbannon
Copy link
Author

davidbannon commented Aug 25, 2020

Edit logs from dumpio -
I have removed uninteresting lines and the date/time and client info of each line -

dumpio_in (data-HEAP): POST /nextcloud/index.php/apps/notes/api/v1/notes HTTP/1.1\r\n
dumpio_in (data-HEAP): Authorization: Basic ZGJhbm5vbjpEYXZvc09sZFBX\r\n
dumpio_in (data-HEAP): Host: 192.168.1.137\r\n
dumpio_in (data-HEAP): Content-Type: application/x-www-form-urlencoded\r\n
dumpio_in (data-HEAP): Accept: application/json\r\n
dumpio_in (data-HEAP): Connection: close\r\n
dumpio_in (data-HEAP): {"title":"Some New Note","content":"This is stuff in the new note"}

As you can see, despite my setting of Content-Type = application/json it appears here as application/x-www-form-urlencode but the content itself is not in fact encoded ??

Davo

@korelstar
Copy link
Member

There are at least two things you have to fix:

  1. Why is the curl request not working? If this doesn't work, you should not expect that your own client will work. I assumed that you installed Nextcloud in the root path of your webserver. In your first post, it looks like you have it in a sub-directory called nextcloud. Maybe you have to fix the URL in the curl call. In order to debug, please use the parameter -i. Please try also a GET request: curl -i -u USER:PASSWORD http://SERVER_AND_PATH/index.php/apps/notes/api/v1/notes, check http vs. https and have a look at the Nextcloud server logs (see Logging app or nextcloud.log in Nextcloud's data directory).

  2. It looks like your client still doesn't send the correct content-type. This is mandatory. You have to fix this first!

@davidbannon
Copy link
Author

OK, you make a lot of sense Korestar !
I have reverted back to my multipass snap nextcloud and, indeed, the curl command line works perfectly there !
When I installed Nextcloud, as a stand alone VM, yep, I copied the 'nextcloud' directory into server root, not its contents so, that explains my strange url, it still should work but maybe its another variable I don't need.

Now, my free pascal client does not work with the multipass nextcloud and, as you say, the fact its not sending as content-type-json has to be a clue. Trouble is I cannot set any apache logging in the multipass thing, its an applience not a compuer !

So, I'll fiddle with the curl line's url but probably just build another, correct VM and see how that works. I am moving in the right direction, I thank you !

I will report back here when I have made some more progress.

Davo

@davidbannon
Copy link
Author

OK, all working as expected now. My issue was Content-Type - the FP call I was using, PostForm was overwriting json with x-www-form-urlencoded, perhaps because of 'form' ? Anyway, doing

client.RequestBody := TRawByteStringStream.Create(Params); 
client.Post(TheURL, Response);

bypassed the overwriting and all worked as expected. And, yes, complicated by a badely built NextCloud VM too ! Sigh ...

Thanks Korelstar, I will close this now (hope thats the way its done here).

Davo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants