# JSON API via curl

**URL:** https://meta.discourse.org/t/json-api-via-curl/197412
**Category:** Support
**Created:** [July 16, 2021, 10:02pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412 "2021-07-16T22:02:28Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![cookieman768](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cookieman768/32/118511_2.png) [@cookieman768](https://meta.discourse.org/u/cookieman768)
#### Post date: [July 16, 2021, 10:02pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/1 "2021-07-16T22:02:29Z")

</div>

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:  
 ![image](https://global.discourse-cdn.com/meta/original/3X/8/9/896d1f6491dbd6f29259032d0b8e5040bccf9df3.png)

Does anyone know what I’m doing wrong?

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [July 16, 2021, 10:24pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/2 "2021-07-16T22:24:42Z")

</div>

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,  
Andreas.

---

<div class="post-metadata">

### Author: ![cookieman768](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cookieman768/32/118511_2.png) [@cookieman768](https://meta.discourse.org/u/cookieman768)
#### Post date: [July 16, 2021, 10:28pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/3 "2021-07-16T22:28:12Z")

</div>

Totally! Here’s what I have so far:

```java
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"))
                    .lines()
                    .collect(Collectors.joining("\n"));
            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.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [July 16, 2021, 10:33pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/4 "2021-07-16T22:33:23Z")

</div>

> [@cookieman768](#):
>
> `Process nitroProcess = Runtime.getRuntime().exec(command);`

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,  
Andreas.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [July 16, 2021, 10:36pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/5 "2021-07-16T22:36:07Z")

</div>

> [@cookieman768](#):
>
> `-d \'{\"usernames\" : \"" + user.get("username") + "\"}\'\\\n"`

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

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

---

<div class="post-metadata">

### Author: ![cookieman768](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cookieman768/32/118511_2.png) [@cookieman768](https://meta.discourse.org/u/cookieman768)
#### Post date: [July 16, 2021, 10:42pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/6 "2021-07-16T22:42:51Z")

</div>

So, my CURL command looks like:

```curl
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](https://docs.discourse.org/#tag/Groups/paths/~1groups~1%7Bid%7D~1members.json/put).

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

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [July 16, 2021, 10:45pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/7 "2021-07-16T22:45:04Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![cookieman768](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cookieman768/32/118511_2.png) [@cookieman768](https://meta.discourse.org/u/cookieman768)
#### Post date: [July 16, 2021, 10:49pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/8 "2021-07-16T22:49:20Z")

</div>

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.

```plaintext
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}

```

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [July 16, 2021, 10:52pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/9 "2021-07-16T22:52:01Z")

</div>

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:

```plaintext
{"usernames": "Av3r4ge"}

```

---

<div class="post-metadata">

### Author: ![cookieman768](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cookieman768/32/118511_2.png) [@cookieman768](https://meta.discourse.org/u/cookieman768)
#### Post date: [July 16, 2021, 10:54pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/10 "2021-07-16T22:54:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 15, 2021, 10:54pm UTC](https://meta.discourse.org/t/json-api-via-curl/197412/11 "2021-08-15T22:54:46Z")

</div>

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