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:

HTTParty.get("http://dataineed.com/data.json")
JSON.parse(response.body)

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 https://www.dntly.com/api/v1/sessions.json \
-d "email=test@example.com" \
-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 https://www.dntly.com/api/v1/accounts.json \
-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:

2 Likes

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("https://www.dntly.com/api/v1/accounts.json", options)
3 Likes

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:

post_data = YOUR_POST_JSON_OBJECT
    app_id = YOUR_APP_ID
    private_key = YOUR_PRIVATE_API_KEY
    url = "https://push.ionic.io/api/v1/push"
    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" https://push.ionic.io/api/v1/push -d '{"tokens":["DEVICE_TOKEN"],"notification":{"alert":"I come from planet Ion."}}'