JSON API via curl

So, I’m using the JSON API to add a user to a group, it works as expected when I used the terminal but when I’m calling curl from Java I’m getting {"status":400,"error":"Bad Request"}

So, I looked in the logs and saw:

Does anyone know what I’m doing wrong?

Hi there,

most probably, the arguments to curl (I assume you are shelling out from Java) are not forwarded correctly. This is often related to incorrect quoting.

If you share the corresponding Java code here, people might be able to guess what might be going wrong.

With kind regards,

1 Like

Totally! Here’s what I have so far:

command = "curl -X PUT https://discourse.domain.tld/groups/" + roleID + "/members.json \\\n" +
                    "     -d \'{\"usernames\" : \"" + user.get("username") + "\"}\'\\\n" +
                    "     -H \"Content-Type: application/json\"\n" +
                    "     -H \"Accept: application/json\"\n" +
                    "     -H \"Api-Key: redacted\"\\\n" +
                    "     -H \"Api-Username: Bot\"";
Process nitroProcess = Runtime.getRuntime().exec(command);
            String outputString = new BufferedReader(
                    new InputStreamReader(nitroProcess.getInputStream(), "UTF-8"))
            System.out.println("Output String: " + outputString);

I’m passing an integer for the roleID and user.get("username") is just a String from a JSON request.

I can post more if it would help, but I am trying to be careful to not leak any sensitive information.

Can you System.out.println(command) and share the output, with the content of the variables roleID and user.get("username") redacted, if appropriate?

With kind regards,

I am not sure if quoting \' is correct. What about this?

-d '{\"usernames\": \"" + user.get("username") + "\"}'\\\n"

So, my CURL command looks like:

curl -X PUT https://discourse.domain.tld/groups/50/members.json \
     -d '{"usernames" : "Torbjörn"}'\
     -H "Content-Type: application/json"
     -H "Accept: application/json"
     -H "Api-Key: [redacted]"\
     -H "Api-Username: Bot"

As far as the quoting goes, I was following the docs here.

If I paste that command in my WSL terminal it runs and works as intended.

Thank you. I want to note that some lines end with \ and some not. Also, might the Umlaut have the chance to be the culprit here? Can you also test it with a username without an Umlaut?

I removed the \ from some lines because it was throwing a different error. When I have a username without any special characters, it’s the same issue.

curl -X PUT https://discourse.domain.tld/groups/50/members.json \
     -d '{"usernames" : "Av3r4ge"}'\
     -H "Content-Type: application/json"
     -H "Accept: application/json"
     -H "Api-Key: [redacted]"\
     -H "Api-Username: Bot"
Output String: {"status":400,"error":"Bad Request"}
{"error":"Bad Request","status":400}

I am out of ideas here.

Maybe, while it should still be valid JSON, can you remove the space after the key for the JSON payload, like this:

{"usernames": "Av3r4ge"}

Yeah, same issue - at this point, I’m most likely going to look into using a library for doing the request. I just have no idea why it’s being so difficult. Thank you for helping me troubleshoot.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.