# Woocommerce API ionic 3 POST error 401

**URL:** <https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022>\
**Category:** ionic-v3\
**Created:** [October 26, 2017, 8:23am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022 "2017-10-26T08:23:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haroun](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/haroun/32/102024_2.png) [@Haroun](https://forum.ionicframework.com/u/Haroun)\
**Post date:** [October 26, 2017, 8:23am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/1 "2017-10-26T08:23:19Z")

</div>

Hi, i use woocommerce api on ionic app  
(using nodejs module [https://www.npmjs.com/package/woocommerce-api](https://www.npmjs.com/package/woocommerce-api) )

# and all GET works but POST dosen’t !!

i’ve tried :

```auto
wc.postAsync('products', data).then((data) => {
       ...
}, (err) => { console.log('Error : ', err); });

```

and i get 401 error

may any one help me to fix that ?  
thanks 🙂

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [October 26, 2017, 8:40am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/2 "2017-10-26T08:40:45Z")

</div>

How are you testing? OS?  
What is your `ionic info` output?  
Do the GET requests also use authentication?

---

<div class="post-metadata">

**Author:** ![Haroun](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/haroun/32/102024_2.png) [@Haroun](https://forum.ionicframework.com/u/Haroun)\
**Post date:** [October 26, 2017, 9:18am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/3 "2017-10-26T09:18:57Z")

</div>

in my website \> wp-admin \> woocommerce API \> genearate KEY READ / Write  
and then :

```auto
wc : any = WC({
  url: 'http://xxxxx.com',
  consumerKey: 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
});

```

```auto
wc.getAsync('products/categories").then((data) => {
      // THIS IS WORK FINE
}, (err) => { console.log('err', err); });

```

**but POST NOT WORKING with 401 error**  
i tried with chrom simulation (android & ios)  
and **i built apk and installed on my Android phone**

Note that on chrome I add plugin allow controll allow origin to make gets works.

ionic info

cli packages: (/usr/lib/node\_modules)

```
@ionic/cli-utils : 1.13.1
ionic (Ionic CLI) : 3.13.2

```

global packages:

```
cordova (Cordova CLI) : 7.0.1 

```

local packages:

```
@ionic/app-scripts : 3.0.0
Cordova Platforms : android 6.2.3
Ionic Framework : ionic-angular 3.7.1

```

System:

```
Android SDK Tools : 26.1.1
Node : v6.11.2
npm : 3.10.10 
OS : Linux 4.9

```

Misc:

```
backend : legacy
```

---

<div class="post-metadata">

**Author:** ![Vidit](https://avatars.discourse-cdn.com/v4/letter/v/cc9497/32.png) [@Vidit](https://forum.ionicframework.com/u/Vidit)\
**Post date:** [December 21, 2017, 3:36pm UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/4 "2017-12-21T15:36:58Z")

</div>

@Haroun did you solve this problem, I am getting the same error though the post is working from postman

---

<div class="post-metadata">

**Author:** ![Haroun](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/haroun/32/102024_2.png) [@Haroun](https://forum.ionicframework.com/u/Haroun)\
**Post date:** [December 23, 2017, 1:10pm UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/5 "2017-12-23T13:10:30Z")

</div>

@Vidit i use a php library, and from my client side i make simple post to:  
[ww.mywebsite.com/my\_api.php](http://ww.mywebsite.com/my_api.php)  
in this file i make all my calls and authantication.

you can see the example here:

> **[kloon/WooCommerce-REST-API-Client-Library](https://github.com/kloon/WooCommerce-REST-API-Client-Library)**
>
> WooCommerce-REST-API-Client-Library - A PHP wrapper for the WooCommerce REST API

---

<div class="post-metadata">

**Author:** ![johnpeter](https://avatars.discourse-cdn.com/v4/letter/j/45deac/32.png) [@johnpeter](https://forum.ionicframework.com/u/johnpeter)\
**Post date:** [December 28, 2017, 11:01am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/6 "2017-12-28T11:01:44Z")

</div>

Did you find solution ? for post data

---

<div class="post-metadata">

**Author:** ![chiranjitkuirynew](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@chiranjitkuirynew](https://forum.ionicframework.com/u/chiranjitkuirynew)\
**Post date:** [April 15, 2018, 6:45pm UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/7 "2018-04-15T18:45:00Z")

</div>

try with this one  
wc : any = WC({

```
    url:"https://www.xxxxxx.com",
    consumerKey:"ck_xxxxxxx...................xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    consumerSecret:"cs_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    wpAPI : true,
    version:"wc/v2",
    queryStringAuth: true // Force Basic Authentication as query string true and using under HTTPS

});
```

---

<div class="post-metadata">

**Author:** ![JammingJack](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@JammingJack](https://forum.ionicframework.com/u/JammingJack)\
**Post date:** [October 25, 2018, 1:00am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/8 "2018-10-25T01:00:16Z")

</div>

> [@chiranjitkuirynew](#):
>
> wpAPI : true, version:“wc/v2”, queryStringAuth: true

Thank you kind sir, this worked like a charm…but tell why did it work…  
what did these three lines of code change

```auto
    wpAPI : true,
    version:"wc/v2",
    queryStringAuth: true

```

---

<div class="post-metadata">

**Author:** ![sauravcd02](https://avatars.discourse-cdn.com/v4/letter/s/b3f665/32.png) [@sauravcd02](https://forum.ionicframework.com/u/sauravcd02)\
**Post date:** [January 5, 2019, 12:13pm UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/9 "2019-01-05T12:13:00Z")

</div>

i have used this solution but it did not work for me. here is my screenshot. plz help me to solve this problem. ![1](https://global.discourse-cdn.com/ionicframework/original/3X/f/4/f48f1e99cdae3fcf52ff043524bcf15a06c4573e.jpeg)

 ![3](https://global.discourse-cdn.com/ionicframework/original/3X/d/6/d65953c29a35988386ff632bef981b2812ee32a9.jpeg)

---

<div class="post-metadata">

**Author:** ![Hammad6264](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hammad6264/32/128169_2.png) [@Hammad6264](https://forum.ionicframework.com/u/Hammad6264)\
**Post date:** [July 23, 2019, 12:11am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/10 "2019-07-23T00:11:51Z")

</div>

Hi @sauravcd02 . Look at the customeData that you are posting. use postAsync function like this:

```
` this.WooCommerce.postAsync('customers', customerData.customer).then( (data) => {})` 

```

and the customer object is something like this:

Define your customerData object like this but before the postAsync function

```auto

 let customerData = {
        customer : {}
      }
      customerData.customer = {
        "email": this.newUser.email,
        "first_name": this.newUser.first_name,
        "last_name": this.newUser.last_name,
        "password": this.newUser.password,
        "confirm_password": this.newUser.confirm_password,
        "state": this.newUser.state,
        "primary": this.newUser.primary,
      }

```

let me know if it’s work. Thanks!

---

<div class="post-metadata">

**Author:** ![Ray02](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@Ray02](https://forum.ionicframework.com/u/Ray02)\
**Post date:** [July 26, 2019, 1:27am UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/11 "2019-07-26T01:27:05Z")

</div>

Hello @Haroun could you please tell me how to use it within IONIC? ☹

---

<div class="post-metadata">

**Author:** ![Hammad6264](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/hammad6264/32/128169_2.png) [@Hammad6264](https://forum.ionicframework.com/u/Hammad6264)\
**Post date:** [July 28, 2019, 11:13pm UTC](https://forum.ionicframework.com/t/woocommerce-api-ionic-3-post-error-401/110022/12 "2019-07-28T23:13:12Z")

</div>

Yeah, you could use this code in Ionic also in angular if you want.  
Here is my whole code of register the user:

```auto
signup(){
    const run = this.loadCtrl.create({ content: 'Please wait...' });
    run.present();
    setTimeout(() => {
      if(this.newUser.password != this.newUser.confirm_password) {
        run.dismissAll();
        this.toastCtrl.create({
          message: "Passwords do not match.",
          showCloseButton: true
        }).present();
        return;
      }
      let customerData = {
        customer : {}
      }
      customerData.customer = {
        "email": this.newUser.email,
        "first_name": this.newUser.first_name,
        "last_name": this.newUser.last_name,
        "password": this.newUser.password,
        "confirm_password": this.newUser.confirm_password,
        "state": this.newUser.state,
        "primary": this.newUser.primary,
      }
      this.WooCommerce.postAsync('customers', customerData.customer).then( (data) => {
        console.log(JSON.parse(data.body));
        let response = (JSON.parse(data.body))
        if(response.id){
          run.dismissAll();
          this.alertCtrl.create({
            header: "Account Created",
            message: "Your account has been created successfully! Your Username is " + response.username + " .Please login to proceed.",
            buttons: [{
              text: "Login",
              handler: ()=> {
                this.router.navigate(['/login']);
              }
            }]
          }).then((elementEl) => {
          elementEl.present();
        });
        } else if(response.message){
          this.toastCtrl.create({
            message: response.message,
            showCloseButton: true
          }).present();
        }
      }, (err) => {
        run.dismissAll();
        this.alertCtrl.create({
          header: 'Something went wrong',
          message: 'Something went wrong. There is a problem with connecting to the server. Please check your internet connection, Thanks!',
          buttons: [{
            text: 'Okay',
            handler: () => {
              this.router.navigate(['/home']);
            }
          }]
        }).then((elementEl) => {
          elementEl.present();
        });
      })
    }, 2000)
  }

```

Hope this will help!
