How to use Curl::Easy to post a metafield to Shopify API
I'm trying to write a Ruby program using Curl::Easy to add a metafield to a smart collection. What's wrong with my code? What parameter name to use for the json? I've tried 'post', 'request', 'body' and also tried just posting the json string without a parameter name. I've tried using PostField and query strings, multipart and not. I've even tried using straight curl from a shell. Same "422 Unprocessable Entity" response. What am I missing?

    curl -X POST -d 'my_metafield.json' https://1234:1234@mysite.myshopify.com/admin/smart_collections/1234/metafields.json

Ruby program:

    require 'curb'
    
    url = 'https://mysite.myshopify.com/admin/smart_collections/1234/metafields.json'
    
    # json = File.read('my_metafield.json')
    
    json = '{"metafield":{"namespace":"layout", "key":"products_per_row", "value":7, "value_type":"integer"}}'
    
    # post_field = Curl::PostField.content('post', json)
    # post_field = Curl::PostField.file('file', 'my_metafield.json')
    
    curl = Curl::Easy.http_post(url, json) do |c|
      c.username = '1234'
      c.password = '1234'
      # c.multipart_form_post = true
    end
    
    puts curl.body_str

Response:

    Connection #0 to host mysite.myshopify.com left intact
    {"errors":{"namespace":["can't be blank","is too short (minimum is 3 characters)"], "key":["can't be blank","is too short (minimum is 3 characters)"],"value":["can't be blank"],"value_type":["can't be blank","is not included in the list"]}}
How to use Curl::Easy to post a metafield to Shopify API
===

I'm trying to write a Ruby program using Curl::Easy to add a metafield to a smart collection. What's wrong with my code? What parameter name to use for the json? I've tried 'post', 'request', 'body' and also tried just posting the json string without a parameter name. I've tried using PostField and query strings, multipart and not. I've even tried using straight curl from a shell. Same "422 Unprocessable Entity" response. What am I missing?

    curl -X POST -d 'my_metafield.json' https://1234:1234@mysite.myshopify.com/admin/smart_collections/1234/metafields.json

Ruby program:

    require 'curb'
    
    url = 'https://mysite.myshopify.com/admin/smart_collections/1234/metafields.json'
    
    # json = File.read('my_metafield.json')
    
    json = '{"metafield":{"namespace":"layout", "key":"products_per_row", "value":7, "value_type":"integer"}}'
    
    # post_field = Curl::PostField.content('post', json)
    # post_field = Curl::PostField.file('file', 'my_metafield.json')
    
    curl = Curl::Easy.http_post(url, json) do |c|
      c.username = '1234'
      c.password = '1234'
      # c.multipart_form_post = true
    end
    
    puts curl.body_str

Response:

    Connection #0 to host mysite.myshopify.com left intact
    {"errors":{"namespace":["can't be blank","is too short (minimum is 3 characters)"], "key":["can't be blank","is too short (minimum is 3 characters)"],"value":["can't be blank"],"value_type":["can't be blank","is not included in the list"]}}