/admin/api/2023-07/price_rules.json returning an array instead of the new price rule object

admchrs1
Tourist
9 0 3

Hey, thanks for your time.

The problem is, I call the price rules api endpoint using POST method and it returns a list of all the existing prices rules instead of the price rule I just created. Basically, it's returning what would be the response for the GET method using that same endpoint, except, I'm using POST. Any help is appreciated, I'm quite pressed for time. Thanks.

I'm using POST method, my body: 

const requestBody = {
  price_rule: {
    title: 'TEST10',
    target_type: 'line_item',
    target_selection: 'entitled',
    entitled_product_ids: [54196394067498167846412514],
    allocation_method: 'across',
    value_type: 'percentage',
    value: -10.0,
    usage_limit: 1,
    starts_at: isoStartDate,
    ends_at: isoEndDate,
    customer_selection: 'all'
  }
};
the request response is far to big for me to send it here but it's an array of existing price rules (and my price rule isnt in it), other than that, it's just typical price rules.
Calling from server, no from client.
endpoint: 
X-Request-Id: e54bf548-5fdb-4259-8f53-cdb1e7db694f
 
Access scopes:
- read/write price rules
- read/write discount codes
- read analytics
 
(Leave a like if you have the same problem, so that it brings attention to this)
Tell me if any more info is needed, I'll provide it.
Thanks.
Replies 9 (9)

Liam
Shopify Staff
2731 298 769

Hi Admchrs1,,

This does seem unusual that your request is not being correctly processed as a POST request, but instead as a GET request. This could be due to a number of reasons. Here are some steps you can take to troubleshoot:

  1. Check your Headers: Make sure you are setting the correct 'Content-Type' header to 'application/json'. This lets the server know that you're sending JSON data.

  2. Check your Data format: Ensure that your request body is correctly formatted as JSON. You can use a tool like Postman to help with this.

  3. Check your Access Token: Ensure that you have the necessary permissions to create a price rule. You mentioned the access scopes you have, make sure they are correct and have the necessary permissions

Try the above and let us know if you're still seeing issues. Hope this helps!

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

admchrs1
Tourist
9 0 3

Firstly, thank you so much for your response.

 

Secondly, unfortunately, I already did all the above x).

Here's the code: 

const currentDate = new Date();
const isoStartDate = currentDate.toISOString();
const endTimeInMilliseconds = currentDate.getTime() + 30 * 60 * 1000; // Add 30 minutes
const isoEndDate = new Date(endTimeInMilliseconds).toISOString();

const requestBody = {
  price_rule: {
    title: 'TESTKIPP1234567891',
    target_type: 'line_item',
    target_selection: 'entitled',
    entitled_product_ids: [7673522094306],
    allocation_method: 'across',
    value_type: 'percentage',
    value: -5.0,
    usage_limit: 1,
    starts_at: isoStartDate,
    ends_at: isoEndDate,
    customer_selection: 'all'
  }
};

fetch(url, {
  method: 'POST',
  headers: {
    'X-Shopify-Access-Token': 'accessToken',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(requestBody),
}).then(async response => {
  if (response.ok) {
    const status = response.status;
    const statusText = response.statusText;
    console.log(`Status: ${status}`);
    console.log(`StatusText: ${statusText}`);
    const location = response.headers.get('Location');
    console.log(`Price rule created at ${location}`);
    const xRequestId = response.headers.get('X-Request-ID');
    console.log(`X-Request-ID: ${xRequestId}`);
    return response.json();
  } else {
    const text = await response.text();
    console.log(`Response text: ${text}`);
    throw new Error(`Request failed with status: ${response.status}`);
  }
})
.then(data => {
  console.log(data);
})
.catch(error => {
  console.error(error);
});
 
I didn't include the url and the access token.
For the access token I even double checked by calling the endpoint to know if I had write access, and I do.
 
But the weirdest thing is that I tried this with my own store (with the same parameters and same access scopes) and it works fine, so I'm completely confused.
 
I thought it was probably because my client had too much price rules already, so I delete 2 and tried it again... no change. I then read somewhere that the limit was 20 000 000 price rules, so it's unlikely that my client reached that.
 
Do you have anymore ideas with this added info?
Thanks.
Adam.
admchrs1
Tourist
9 0 3

Firstly, thank you so much for your response.

 

Secondly, unfortunately, I already did all the above x).

 

Here's the code:

const currentDate = new Date();
const isoStartDate = currentDate.toISOString();
const endTimeInMilliseconds = currentDate.getTime() + 30 * 60 * 1000; // Add 30 minutes
const isoEndDate = new Date(endTimeInMilliseconds).toISOString();

const requestBody = {
  price_rule: {
    title: 'TESTKIPP1234567891',
    target_type: 'line_item',
    target_selection: 'entitled',
    entitled_product_ids: [7673522094306],
    allocation_method: 'across',
    value_type: 'percentage',
    value: -5.0,
    usage_limit: 1,
    starts_at: isoStartDate,
    ends_at: isoEndDate,
    customer_selection: 'all'
  }
};

fetch(url, {
  method: 'POST',
  headers: {
    'X-Shopify-Access-Token': 'accessToken',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(requestBody),
}).then(async response => {
  if (response.ok) {
    const status = response.status;
    const statusText = response.statusText;
    console.log(`Status: ${status}`);
    console.log(`StatusText: ${statusText}`);
    const location = response.headers.get('Location');
    console.log(`Price rule created at ${location}`);
    const xRequestId = response.headers.get('X-Request-ID');
    console.log(`X-Request-ID: ${xRequestId}`);
    return response.json();
  } else {
    const text = await response.text();
    console.log(`Response text: ${text}`);
    throw new Error(`Request failed with status: ${response.status}`);
  }
})
.then(data => {
  console.log(data);
})
.catch(error => {
  console.error(error);
});
 
Access token: I double checked by calling the endpoint and I have the permissions needed.
 
I even tried with my own store and it works fine with the same parameters and same permissions for the key.
 
I even deleted 2 price rules to make sure that my client hadn't reached the limit. Moreover I read the limit was 20 000 000, so it's unlikely.
 
I don't really know what else I can try, if you have any more ideas given this added info, I'd appreciate it immensely.
Thanks.
Adam.
admchrs1
Tourist
9 0 3

(duplicate reply)

admchrs1
Tourist
9 0 3

Hey Liam, any new ideas or solutions for this? Thanks

bailang
Shopify Partner
1 0 2

I'm getting the same issue.
Did you resolve that?

admchrs1
Tourist
9 0 3

No, I tried so many things, but to no avail.

Completely ran out of ideas.

You should like the original post so that it brings attention to this ;).

 

@Liam do you have any new ideas for this issue?

amir-club
Shopify Partner
4 0 2

Hi team,

I'm having the same issue, anyone was able to fix it ?

Thank you

amir-club
Shopify Partner
4 0 2

For anyone facing the same issue, it turn out it only happen when we call the main domain but it doesn't happen when we call myshopify domain. so probably an bug on the redirection where the POST requests end up turning to a GET requests