# How to Send Data with POST Request in Ionic to localhost

**URL:** <https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765>\
**Category:** ionic-v3\
**Created:** [March 15, 2018, 7:06pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765 "2018-03-15T19:06:28Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 7:06pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/1 "2018-03-15T19:06:29Z")

</div>

I am using JSON server in my application.

I am trying to post my data to localhost:3000.

**home.ts**

```auto
 var headers = new Headers();
          headers.append("Accept", 'application/json');
          headers.append('Content-Type', 'application/json' );
          let options = new RequestOptions({ headers: headers });

          this.http.post('localhost:3000', JSON.stringify(this.data), options).subscribe(
            data => {
              alert.setSubTitle("Ordered");
              alert.present();
            },
            error => {
              console.log(error);
          });

```

error:

Response {\_body: “{}”, status: 404, ok: false, statusText: “Not Found”, headers: Headers, …}  
headers:Headers {\_headers: Map(4), \_normalizedNames: Map(4)}  
ok:false  
status:404  
statusText:“Not Found"  
type:2  
url:“[http://localhost:3000/db](http://localhost:3000/db)”  
\_body:”{}"  
**proto** :Body

How should I fix it?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [March 15, 2018, 7:12pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/2 "2018-03-15T19:12:20Z")

</div>

`localhost` is whatever device the app is running on, so it’s generally not what you want to be using. Pick a real hostname or IP address. Incidentally, you have a lot of spurious code here: you don’t need custom headers, you don’t need a `RequestOptions`, you don’t need to manually stringify anything. Just pass an object and Angular will take care of all of that.

---

<div class="post-metadata">

**Author:** ![leonardoss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leonardoss/32/114499_2.png) [@leonardoss](https://forum.ionicframework.com/u/leonardoss)\
**Post date:** [March 15, 2018, 7:15pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/3 "2018-03-15T19:15:27Z")

</div>

Remove all these unnecessary code, do a ifconfig(mac) or ipconfig(windows) take your network ip, connect device to the same wifi/network you are with the pc you are running the server and it will work 😉

You will have something like:  
[http://192.168.4.26:3000/](http://192.168.4.26:3000/)

---

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 7:23pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/4 "2018-03-15T19:23:36Z")

</div>

```auto
          this.http.post('http://192.168.XX.X:3000', JSON.stringify(this.data)).subscribe(
            data => {
              alert.setSubTitle("Ordered");
              alert.present();
            },
            error => {
              console.log(error);
          });

```

Still the same.

error:  
Response {\_body: “{}”, status: 404, ok: false, statusText: “Not Found”, headers: Headers, …}  
headers  
:  
Headers {\_headers: Map(4), \_normalizedNames: Map(4)}  
ok:false  
status:404  
statusText:“Not Found"  
type:2  
url:“[http://192.168.XX.X:3000/](http://192.168.XX.X:3000/)”  
\_body:”{}"  
**proto** :Body

---

<div class="post-metadata">

**Author:** ![leonardoss](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/leonardoss/32/114499_2.png) [@leonardoss](https://forum.ionicframework.com/u/leonardoss)\
**Post date:** [March 15, 2018, 7:36pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/5 "2018-03-15T19:36:36Z")

</div>

you don’t have any route??? like [http://192.168.XX.X:3000/api/login](http://192.168.XX.X:3000/api/login) or something like that? I think that you getting a 404 because there’s no route for root /

---

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 7:41pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/6 "2018-03-15T19:41:35Z")

</div>

I’ve tried url:“[http://192.168.XX.X:3000/db](http://192.168.XX.X:3000/db)” but still the same.

I am using _json-server --watch db.json_ in WindowsPowerShell and _ionic lab_ in node.js command prompt. Maybe that’s why?

---

<div class="post-metadata">

**Author:** ![andas](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@andas](https://forum.ionicframework.com/u/andas)\
**Post date:** [March 15, 2018, 7:46pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/7 "2018-03-15T19:46:14Z")

</div>

for Post, use Content-Type : application/x-www-form-urlencoded

---

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 7:55pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/8 "2018-03-15T19:55:25Z")

</div>

Still the same error.  
Please see screenshot

 ![err](https://global.discourse-cdn.com/ionicframework/original/3X/0/9/091e32966f7fe10a35904eacef691434578efb18.png)

---

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 8:02pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/9 "2018-03-15T20:02:57Z")

</div>

I guess I’ve tried everything and it’s still not working.

Maybe someone can help me with choosing some server to send my data 🙂

---

<div class="post-metadata">

**Author:** ![rlouie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rlouie/32/134973_2.png) [@rlouie](https://forum.ionicframework.com/u/rlouie)\
**Post date:** [March 15, 2018, 8:36pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/10 "2018-03-15T20:36:14Z")

</div>

Step 1: Add a GET endpoint to your server, just make it return a string or anything. Hit that from your regular browser and make sure it exists.  
Step 2: Forget completely about Ionic and use POSTman to post data to your server. It’s a free app/chrome extension that lets you make http requests.  
Step3: Try hitting that GET endpoint you added to your server within your Ionic app.

You could have any of a large number of different problems. Try those above steps and see if you can get any of them to work.

---

<div class="post-metadata">

**Author:** ![kinxsta](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kinxsta](https://forum.ionicframework.com/u/kinxsta)\
**Post date:** [March 15, 2018, 9:06pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/11 "2018-03-15T21:06:32Z")

</div>

Step 1: GET is working fine. I’ve tried it before using folder path and localhost as well.  
Step 2: POSTman chrome extension. I’ve tried. Working without issues.  
Step 3: Working.

---

<div class="post-metadata">

**Author:** ![rlouie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rlouie/32/134973_2.png) [@rlouie](https://forum.ionicframework.com/u/rlouie)\
**Post date:** [March 15, 2018, 9:12pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/12 "2018-03-15T21:12:33Z")

</div>

Wow…if all of those work…I have no idea why your post wouldn’t work. It’s nothing to do with localhost vs your network IP because your get request worked, so that’s irrelevant. You clearly have a POST endpoint if postman was able to post to it and get a response back.

So…no idea. Compare the request and request headers that you have in POSTman with the request and request headers that are being sent in Ionic (use chrome network tab in debug window). Post images on here of what your successful request in postman looks like (including the headers) and do the same for your ionic app.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [March 15, 2018, 9:37pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/13 "2018-03-15T21:37:15Z")

</div>

Any chance [the cordova whitelist plugin](https://github.com/apache/cordova-plugin-whitelist) is biting you? I think it causes things to 404 if it doesn’t like them.

---

<div class="post-metadata">

**Author:** ![rlouie](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rlouie/32/134973_2.png) [@rlouie](https://forum.ionicframework.com/u/rlouie)\
**Post date:** [March 15, 2018, 9:39pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/14 "2018-03-15T21:39:22Z")

</div>

It’s running in the browser now, as far as I can tell from their posts, so in theory that’s not it.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:44pm UTC](https://forum.ionicframework.com/t/how-to-send-data-with-post-request-in-ionic-to-localhost/124765/15 "2021-07-19T21:44:47Z")

</div>

Closed on July 19, 2021, 9:44pm UTC


