Curl and HttParty Authentication

Hello Upcase experts. I have a question about curl and httparty.

In my model, I was able to get json information through httparty like this:


This particular API didn’t need any authentication, and I was able to parse the data.

The challenges came when I needed to get API calls that required authentication. The API manual gave me this instructions to get the token like this:

curl -X POST \
-d "" \
-d "password=password"

Then I get this response back from terminal:

  success: true,
  token: "dt_SAMPLETOKENd88ffc342dde5c195df4019f90d6ed"

Then, pull request with the token I get:

curl -X GET \
-u dt_SAMPLETOKENd88ffc342dde5c195df4019f90d6ed:

I am able to do this in Terminal, but how can I request this with Httparty? Or how do I call json data that requires authentication through rails model?

Any best practices or resources you guys know will be greatly appreciated too!

I would probably not do that in the AR model, how do you feel about creating a class that knows how to pass the correct params for httpParty to build the request? I dont know what these are specifically to looking at the repo it shouldn’t be hard to find. As an additional step, I’d consider also creating a parser to handle the response.

I do realise that this isnt very helpful info, and I’ll have a read of the httpParty docs to help some more when I get the chance :slight_smile:


In curl, the -u flag indicates http authentication, but the lack of anything after the colon indicates that the password is blank, which seems a little odd.

This example from the docs shows how to make an HTTP auth request. For your case, it should look something like:

auth = {
  username: "dt_SAMPLETOKENd88ffc342dde5c195df4019f90d6ed",
  password: ""
options = { basic_auth: auth }
result = HTTParty.get("", options)

Thanks guys for replying!

That helps! I am going to try these solutions and see if it works. :smile:

@andyw8 your solution worked perfectly.

I know this is off topic but my question is also curl related:

I would like to make a post request to Ionic’s push notifications api from inside a Rails App.
The sample code provided in in Python and I have trouble converting it to Ruby.
I would appreciate any help:

Here is the python code:

    app_id = YOUR_APP_ID
    private_key = YOUR_PRIVATE_API_KEY
    url = ""
    req = urllib2.Request(url, data=post_data)
    req.add_header("Content-Type", "application/json")
    req.add_header("X-Ionic-Application-Id", app_id)
    b64 = base64.encodestring('%s:' % private_key).replace('\n', '')
    req.add_header("Authorization", "Basic %s" % b64)
    resp = urllib2.urlopen(req)

This is sort of the same call in curl:

 curl -u PRIVATE_API_KEY: -H "Content-Type: application/json" -H "X-Ionic-Application-Id: APP_ID" -d '{"tokens":["DEVICE_TOKEN"],"notification":{"alert":"I come from planet Ion."}}'