# HTTP POST with form data but content type application/json

**URL:** https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639
**Category:** General
**Created:** [30 November 2019 17:38 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639 "2019-11-30T17:38:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [30 November 2019 17:38 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/1 "2019-11-30T17:38:03Z")

</div>

I want to perform a HTTP POST request with form(?) data.

Function node:

```auto
msg.headers = {}
msg.method = "POST"
msg.url = "https://api.goslide.io/api/auth/login" 
msg.payload = {"email": "email","password": "pass"}
msg.headers["content-type"] = "application/json"
msg.headers["X-Requested-With"] = "XMLHttpRequest"
return msg

```

[API](https://documenter.getpostman.com/view/6223391/S1Lu2pSf?version=latest) documentation states that headers should be:

> Content-Type : application/json  
> X-Requested-With: XMLHttpRequest

The info for the http request node states:

> To encode the request as form data, `msg.headers["content-type"]` should be set to `application/x-www-form-urlencoded`.

With `application/json` I get authorisation failed, with the `application/x-www-form-urlencoded` it failed and expects `application/json`

How can I post this with the correct header(s) ?

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [1 December 2019 10:56 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/2 "2019-12-01T10:56:15Z")

</div>

> [@bakman2](#):
>
> application/json

A look in the API documentation confirms that the body of the POST request has to be JSON, like below to login and request a token

```auto
{
	"email": "your@email.com",
	"password": "yourpassword"
}

```

In such case the content-type should be application/JSON.

The reply I got when testing the API was:

```auto
"{"message":"Unauthorized"}"

```

which confirms that the POST request is OK, it is jus the server that is not authorizing.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [1 December 2019 11:13 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/3 "2019-12-01T11:13:35Z")

</div>

I was wondering about that.  
What is the application/x-www-form-urlencoded used for?

---

<div class="post-metadata">

### Author: ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)
#### Post date: [1 December 2019 12:25 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/4 "2019-12-01T12:25:10Z")

</div>

> [@bakman2](#):
>
> application/x-www-form-urlencoded

In the case the body of the request will be like below, which is very similar to the query parameters of a GET request (key,values separated by &):

```auto
q=node+red&num=10

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [1 December 2019 16:48 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/5 "2019-12-01T16:48:47Z")

</div>

Got it. makes sense.  
I will contact the folks behind the api.

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [1 December 2019 20:40 UTC](https://discourse.nodered.org/t/http-post-with-form-data-but-content-type-application-json/18639/6 "2019-12-01T20:40:42Z")

</div>

Tried it via curl (should have done that earlier) and indeed same error.
