REST Price Rule: required parameter missing or invalid

Solved
needmoredata
Tourist
11 0 2

Apologies for posting something so common but the error (required parameter missing or invalid) is completely unhelpful and I've run out of permutations to try.

 

Here's my args dict:

 

pr_args = {
"title": "FNF",
"target_type": "line_item",
"target_selection": "all",
"allocation_method": "each",
"value_type": "percentage",
"value": "-50",
"once_per_customer": "false",
"customer_selection": "all",
"usage_limit": "1",
"starts_at": "2019-10-14T23:01:57Z"
}

This is being called with:

def post_price_rule(payload, type='price_rules.json'):
	req = requests.post(url=f'https://{KEY}:{PWD}@{SHOP_NAME}.myshopify.com/admin/api/{API_VERSION}/{type}',
                        json=payload, headers={'Content-Type': 'application/json'})
	print(req.content)
	return req.status_code

print(post_price_rule(pr_args, 'price_rules.json'))

... which produces the error above.

 

Any idea which part of the incantation I'm missing / malforming?

 

Thanks in advance.

 

0 Likes
Visely-Team
Shopify Partner
1837 210 421

This is an accepted solution.

Looks like the JSON is incorrect in your payload. You have to have all your attributes under "price_rule" attribute, and example would be:

{
  "price_rule": {
    "title": "SUMMERSALE10OFF",
    "target_type": "line_item",
    "target_selection": "all",
    "allocation_method": "across",
    "value_type": "fixed_amount",
    "value": "-10.0",
    "customer_selection": "all",
    "starts_at": "2017-01-19T17:59:10Z"
  }
}
Sergiu Svinarciuc | CTO @ visely.io
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution!
- To learn more about the awesome stuff we do head over to visely.io or our blog
0 Likes
needmoredata
Tourist
11 0 2

giphy

 

Thank you sir!! Just absolutely missed the forest for the trees.

0 Likes
itsanolive
Shopify Partner
19 0 7

I'm having the same issue through Python, but I have the attributes nested in "price_rule" - when i paste my printed json payload and send the post request through Postman, it works fine, just can't seem to get it to work through the Python script. I've actually had this issue with some other put/post requests through python. Any ideas?

 

def createDiscount(title, amount):
    sc_url = 'https://udans.myshopify.com/admin/api/2020-07/price_rules.json'
    sc_auth = ls_auth.shopify_auth
    sc_data = json.dumps({
        "price_rule": {
            "title": str(title),
            "target_type": "line_item",
            "target_selection": "all",
            "allocation_method": "across",
            "value_type": "fixed_amount",
            "value": str(amount),
            "customer_selection": "all",
            "starts_at": datetime.now().isoformat(timespec='seconds') + "Z"
        }
    })
    print(sc_data)
    r = requests.request("POST", sc_url, data=sc_data, auth=sc_auth)
    print(r.json())

createDiscount('231622895668', '-25')

 

Response using the script:

{'errors': {'price_rule': 'Required parameter missing or invalid'}}

Response through Postman is success. Pulling my hair out a bit trying to understand what the difference is. Get requests are totally fine through the script. Whyyy

0 Likes