# HTTP Request with JSON body

**URL:** <https://discourse.processing.org/t/http-request-with-json-body/11544>\
**Category:** Libraries\
**Created:** [May 24, 2019, 11:55am UTC](https://discourse.processing.org/t/http-request-with-json-body/11544 "2019-05-24T11:55:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [May 24, 2019, 11:55am UTC](https://discourse.processing.org/t/http-request-with-json-body/11544/1 "2019-05-24T11:55:17Z")

</div>

Hello, I’m working with the [TVDB API](https://api.thetvdb.com/swagger) but I’m having trouble getting the access token as I normally do.

This is what the documentation says:

> Authentication  
> Authentication to use the API is similar to the How-to section above. Users must POST to the /login route with their API key and credentials in the following format in order to obtain a JWT token.  
> {“apikey”:“APIKEY”,“username”:“USERNAME”,“userkey”:“USERKEY”}

And this is my code (I’m using @shiffman and @runemadsen 's library)

```auto
PostRequest post = new PostRequest("https://api.thetvdb.com/login");
post.addHeader("Content-Type", "application/json");
post.addHeader("Accept", "application/json");
post.addData("json", "{\"apikey\":\"APIKEY\",\"username\":\"USERNAME\",\"userkey\":\"USERKEY\"}");

```

but it’s not working.

I also tried:

```auto
post.addData("apikey", "APIKEY");
post.addData("username", "USERNAME");
post.addData("userkey", "USERKEY");

```

I think I have add the json to the “body” of the POST request but I can’t figure out how.

Thanks!

---

<div class="post-metadata">

**Author:** ![fedpep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/fedpep/32/52_2.png) [@fedpep](https://discourse.processing.org/u/fedpep)\
**Post date:** [May 25, 2019, 10:26am UTC](https://discourse.processing.org/t/http-request-with-json-body/11544/2 "2019-05-25T10:26:42Z")

</div>

I figure out a solution:

1. I downloaded the jar files of the [Apache HTTPComponents library](https://hc.apache.org) (on which HTTP Requests for Processing is based on) and added to my sketch’s folder

2. I built an HTTP Request like this (following the examples on the website):

```auto
String authentication = "{\"apikey\":\"APIKEY\",\"username\":\"USERNAME\",\"userkey\":\"USERKEY\"}";

HttpClient httpClient = HttpClientBuilder.create().build(); //Use this instead 

try {
    HttpPost request = new HttpPost("https://api.thetvdb.com/login");
    StringEntity params =new StringEntity(authentication);
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);
    HttpEntity entity = response.getEntity();
    JSONObject json = parseJSONObject(EntityUtils.toString(entity));
    println(json.getString("token"));
    //handle response here...

}catch (Exception ex) {

    //handle exception here

} finally {
    //Deprecated
    //httpClient.getConnectionManager().shutdown(); 
}

```

and it works.

Now I’m wondering if there’s a way to do it without importing the Apache’s library or to add this feature to the Processing’s library.
