Calling rails api through curl - ruby-on-rails

I have a Rails application that my mobile app is trying to call. I tried testing the API through curl with the following command and it works.
curl -d "user[name]=bob&user[email]=bob0814#example.com&user[password]=password&user[company_name]=hydra" linktomyapi
Now, I tried running the following command because when I send parameters through mobile app, I send them as a JSON format since I use AFNetworking for all my Networking calls, see below and this doesn't work.
curl -v -d '{"user[name]": "bob","user[email]": "bob#gmail.com","user[password]": "password","user[company_name]": "rs"}' linktoMyApi
I am quite confused as to why this difference is? Could anyone suggest any ideas?

You have to pass an application/json Content-Type so rails can understand you are speaking json.
Add to your curl options : -H 'Content-Type:application/json'
Additionnaly, if you want to access your params with params[:user], you have to use a json object "user" to hold all the values :
{"user" : {"name": "bob", "email": "bob#gmail.com", "password": "password", "company_name": "rs"}}

Related

How to emulate curl requests in rails app?

I want to test that a PUT to an endpoint (products/:id) works, but when I try
curl -X PUT -d listing_id_created=True localhost:3000/products/27
it gives ActionController::InvalidAuthenticityToken, which I now realise is the expected result (since there's no authenticity token provided since the PUT is coming from curl and curl doesn't know anything about it).
So my question is how do I run some simple curl PUTs (or any other verbs) to check that endpoints work correctly? Is the only solution to simply disable/skip the authenticity token?

Create repo on Bitbucket programmatically

I used to do
curl -k -X POST --user john#outlook.com:doe13 "https://api.bitbucket.org/1.0/repositories" -d "name=logoApp"
and success.
now I got : error
{"type": "error", "error": {"message": "Resource removed", "detail": "This API is no longer supported.\n\nFor information about its removal, please refer to the deprecation notice at: https://developer.atlassian.com/cloud/bitbucket/deprecation-notice-v1-apis/"}}
Does anyone know a know way to do this ?
There's a difference between a success from curl (OK:200) and an error from the service you're trying to use. The error, however, mentions that you're trying to use the Cloud Rest API version 1, which is deprecated effective 30 June 2018.
Read this for more information.
I don't use Bitbucket Server (a local option), and I think that has more features for this sort of thing.
For the public Bitbucket, you can still do it but it isn't documented.
The v1.0 API has been removed, and the new v2.0 API doesn't document a POST to a /repositories. Instead, you have to hit an endpoint that includes the repo that doesn't yet exist: /repositories/workspace/repo_slug
The JSON payload needs to know the project for the repo: look in the slug for a project that already exists. Fill in the user/team and repo name in the URL. And, you can make an application password so you aren't using your account password. This app password can limit the scope of what that access can do.
% curl -X POST --user 'user:app_pass' \
-H "Content-type: application/json" \
-d '{"project":{"key":"PROJ"}}' \
"https://api.bitbucket.org/2.0/repositories/USER/REPO"

Multi files upload using curl to ruby on rails app

I'm struggling about how I can send an unknow number of files to my rails app using curl.
This is my curl request to POST with one file :
curl -H 'Authorization: Token token=your_token' -X POST -F job[webapp_id]=2 -F job[file]=#test.txt localhost:3000/api/v0/jobs
It works.
I would like to allow the user to send as much as file as he wants with something like :
-F job[files][]=#test1.txt -F job[files][]=#test2.txt
-F job[files[]]=#test1.txt -F job[files[]]=#test2.txt
But it's not working.
I also tried with :
-F job[files[0]]=#test.txt -F job[files[1]]=#test2.txt
Still not working. I think it's because I don't know how to tweak my permit parameters. I get an empty array.
Any idea on how to do it with one request ?
RestClient may be easier to work with.

restify example TODO server not sending responses

I'm new at this and developing my first API server. I wanted to see an example of a POST request so I installed restify 3.0.3 and tried to run the TODO server example. I see the requests logged at the server but no response is sent. I'm using the sample curl requests provided and the server is running on Cloud9. Curl is running on windows 7.
For example, I've tried:
curl -isS http://test-atk9.c9.io | json
curl -isS http://test-atk9.c9.io/todo -X POST -d name=demo -d
task="buy milk"
Can anyone help?
I saw the same behavior when using PostMan to exercise the example.
Setting the PostMan Header Accept:text/plain or Accept:application/json worked for me.
BTW: if you set Accept:text/html, you should receive a helpful response:
{
"code": "NotAcceptableError",
"message": "Server accepts: application/todo,application/json,text/plain,application/octet-stream,application/javascript"
}
Hope this helps.

Passing command line arguments through the Remote API

I am using the remote API to create and start containers, but I am not sure how to pass in the command line arguments I normally would when creating from the local machine. Specifically, I am using this image, which requires a bunch of arguments I would normally do when running 'docker run [arguments][image]'. Any ideas?
For argument passing, you can like this
curl -X POST localhost:2375/containers/create -H "Content-Type: application/json" -d '{"Cmd":["ping", "8.8.8.8"], "Image": "ubuntu"}'
Also see: http://blog.flux7.com/blogs/docker/docker-tutorial-series-part-8-docker-remote-api
This depends on which arguments you want to set. Up to port bindings,here you can find how to do that. In general, you have to use the JSON objects passed as body in your create and in your start request.

Resources