1

I am trying to run this curl script in my ruby rails application:

%x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F overwriteBehavior=normal 
-F content=record -F type=flat -F format=json 
-F data='[{"record_id":"123","seat_id_seq":"bbb","address":"bbb","price":"bbb","email":"bbb","tickets1_complete":"2"}]'  
'https://cri-datacap.org/api/' }

and its working fine. Now I want don't want to hardcode the values so i am trying to give this values by the variable.

These variable contains value from the text field on my rails application:

%x{ curl -F token='08F14AE57696E458BA6FC6A203F57E69' -F overwriteBehavior=normal 
-F content=record -F type=flat -F format=json 
-F data='[{"record_id":"#{params[:record_id]}","seat_id_seq":"bbb","address":"bbb","price":"bbb","email":"bbb","tickets1_complete":"2"}]'  
'https://cri-datacap.org/api/' }

so I have tried for one variable record_id, but its not working.. This script is written in my controller.

3
  • 3
    Why not use an HTTP library instead of curl? Commented Jan 18, 2017 at 19:00
  • I am not sure how to use that. Commented Jan 18, 2017 at 19:44
  • Here are some simple examples to get you started; it's a Ruby std library. ruby-doc.org/stdlib-2.1.3/libdoc/net/http/rdoc/Net/HTTP.html Commented Jan 18, 2017 at 20:05

1 Answer 1

3

Ignoring the fact that using curl externally when libraries like curb exist, using %x{...} for this is extremely messy. What you want to do is call system:

data = [
  {
    record_id: params[:record_id],
    seat_id_seq: "bbb",
    address: "bbb",
    price: "bbb",
    email: "bbb",
    tickets1_complete: 2
  }
]

system(
  "curl",
  "-F", "overwriteBehavior=normal",
  "-F", "content=record",
  "-F", "type=flat",
  "-F", "format=json",
  "-F", "data=#{JSON.dump(data)}",
  'https://cri-datacap.org/api/'
)

When you're writing JSON data, do try and use JSON.dump or .to_json to ensure your document is 100% valid.

Sign up to request clarification or add additional context in comments.

8 Comments

I replaced payload with data, but it is still not inserting data .
It looks like , it is not getting data in params[:record_id]
I'd switch this over to curb code at the very least, if not Faraday before attempting to get everything working properly. That can make debugging a lot less messy.
I actually do not know how to use curb.. I found this solution on net, and thought to try
That's why they have documentation, it helps you understand. curb isn't hard to use, it's essentially 1:1 mapped against curl command-line arguments, just in Ruby code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.