POST method works in Postman, but returns the login page when using axios

POST method works in Postman, but returns the login page when using axios

aliyar
Visitor
2 0 0

When I make a post request to the following URL in Postman, and pass the raw data below, I get a 201 status code and the price rule is created successfully. But, when I try the same thing with axios, the api returns a 200 status code and the response data is the html of the login page.

https://<apikey>:<password>@xxx.myshopify.com/admin/api/2020-07/price_rules.json

{
  "price_rule": {
    "title": "TESTPRICERULE2",
    "target_type": "line_item",
    "target_selection": "entitled",
    "allocation_method": "across",
    "value_type": "percentage",
    "value": "-100.0",
    "customer_selection": "all",
    "usage_limit": "1",
    "entitled_product_ids": ["5591610753175"],
    "starts_at": "2020-09-02T17:59:10Z"
  }
}

 
Here's how I'm making the post request with axios:

    const data = {
      price_rule: {
        title: "TESTPRICERULE2",
        value_type: "percentage",
        value: "-100.0",
        entitled_product_ids: ["5591610753175"],
        allocation_method: "across",
        once_per_customer: "false",
        usage_limit: "1",
        customer_selection: "all",
        target_type: "line_item",
        target_selection: "entitled",      
        starts_at: new Date()  
      }
    };

   const url = "https://<apikey>:<password>@xxx.myshopify.com/admin/api/2020-07/price_rules.json";
    axios.post(url, data).then(res => {
        console.log(res.data); // res.data is html
    });

 

Replies 3 (3)

Jason
Shopify Partner
11207 226 2319

Is this fair to assume you’re making an API call via the browser or is this server side code?

You’ll be exposing the key details to the public if client side so that’s pretty scary to begin with. As for the block, If sending via the browser it’s going to include cookies so will be blocked for good security reasons. 

If it is server side and you’re sure the details are protection, there’s an alternative approach but let’s cover off the assumptions above first.

 

★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★
aliyar
Visitor
2 0 0

Thank you! I already figured it out. I had to pass the password as a content header.

Barenziah
Shopify Partner
3 0 0

Did you just say in Postman in the headers section "password": pass ? Or did you frame it as a specific header?