Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

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

Open
TheGha opened this issue Sep 2, 2023 · 5 comments
Open

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

TheGha opened this issue Sep 2, 2023 · 5 comments

Comments

@TheGha
Copy link

TheGha commented Sep 2, 2023

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
Copy link
Contributor

snoyes commented Sep 5, 2023

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

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

@TheGha
Copy link
Author

TheGha commented Sep 6, 2023

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
Copy link
Contributor

snoyes commented Sep 6, 2023

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
Copy link
Author

TheGha commented Sep 7, 2023

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
Copy link
Contributor

snoyes commented Sep 7, 2023

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

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

No branches or pull requests

2 participants