FooSoft / anki-connect

Anki plugin to expose a remote API for creating flash cards.
https://foosoft.net/projects/anki-connect/
Other
1.92k stars 218 forks source link

help wanted: post only returns "AnkiConnect v.6" #401

Open TheGha opened 1 year ago

TheGha commented 1 year ago

Hi, i am trying to figure this out in C#, since that is the language i am the most used to by far. but this is the first time i am doing http, and jsons in this manner. to dumb it down to general coding concepts: i am posting to "http://localhost:8765" with the string "{"action":"findNotes","version":6,"params":{"cards":"deck:current"}}" and the response i am getting is "AnkiConnect v.6"

the relevant code lines are:

using HttpClient httpClient = new() { BaseAddress = new Uri("http://localhost:8765"), };
public class Params1
{
    public string cards { get; set; }
}

public class GetCards
{
    public string action { get; set; }
    public int version { get; set; }
    public Params1 @params { get; set; }
}

and

Params1 params1 = new Params1();
params1.cards = "deck:current";
GetCards getCards = new GetCards();
getCards.action = "findNotes";
getCards.@params = params1;
getCards.version = 6;

string json1 = JsonSerializer.Serialize(getCards);
Console.WriteLine("json sent: " + json1);

HttpResponseMessage response = await httpClient.PostAsJsonAsync(
"",
getCards);

string jsonResponse = await response.Content.ReadAsStringAsync();

Console.WriteLine("response: " +jsonResponse);

giving me this in the console:

json sent: {"action":"findNotes","version":6,"params":{"cards":"deck:current"}}
response: AnkiConnect v.6
snoyes commented 1 year ago

with the string "{"action":"findNotes","version":6,"params":{"cards":"deck:current"}}"

"cards" is not correct there. Should be "query".

TheGha commented 1 year ago

thanks, tho it still does the same thing after changing it. console now shows:

json sent: {"action":"findNotes","version":6,"params":{"query":"deck:current"}}
response: AnkiConnect v.6
snoyes commented 1 year ago

The response is what would be expected if the request doesn't actually include the JSON mentioned.

The code only prints what getCards contains; it doesn't prove that the expected JSON is actually being sent to AnkiConnect. Log what is actually going out, e.g. https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient

TheGha commented 1 year ago

oh using the LoggingHandler in response to the stack overflow link you sent i managed to figure out that what i did wrong was the post as json and such did not take a json as input, as they serialized it themselves. thanks a ton for your help!

snoyes commented 12 months ago

Good deal. This issue should be closed then; not a bug in AnkiConnect.