function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jStarkjStark 

Trouble updating via REST API with Ruby

Hello everyone, I've been working on a little product catalog application using the REST api. I've got my authentication going with OAuth2, and can query my custom object just fine. I'm working on the update method. I've gotten to the point where I get an HTTP 415 error, which is unsupported media type. I have an update_hash that I'm trying to pass. I've tried these various forms of the hash:

 

update_hash

  = speed3.25 mph

update_hash.to_json

  = {"speed":"455 mph"}

update_hash.dump

 = "{\"speed\":\"455 mph\"}"

 

 

Now this last one looks like how it should be sent. When using the OAuth2 Gem, and this:

 

resp = @access_token.post("#{@resturl}/sobjects/StarkT1__Terminator__c/#{id}?_HttpMethod=PATCH", update_hash, {"ContentType" => "application/json"})

 

I receive this error:

 

undefined method `merge' for "\"{\\\"speed\\\":\\\"455 mph\\\"}\"":String

 

So it looks like the @access_token.post() is doing it's own conversion. Just passing in the hash gives me the 415 error though. So I thought I'd try a RestClient.post and see what happens:

 

resp = RestClient.post("#{@resturl}/sobjects/StarkT1__Terminator__c/#{id}?_HttpMethod=PATCH", update_hash, :content_type => :json, :authorization => "OAuth #{@saccess_token}")

 

This gives me an http 400, bad request. Trying different versions of the update_hash seems to always give me an http 400 error, so something else is wrong with my RestClient post.

 

Any of you gurus out there can tell at a glance what I'm doing wrong?

 

Thanks,

Jed

Best Answer chosen by Admin (Salesforce Developers) 
jStarkjStark

Hi Simon,

 

Thanks for the reply. After sitting back and looking at why my query was working and my update wasn't, I finally figured it out. I could update the name of one of my accounts just fine, but was having trouble with my custom object. Well, I was trying to update a custom field on that custom object. So "speed" wasn't going to cut it, but when I tried "namespace__speed__c" it worked fine.

 

Now I need to come up with an efficient way to parse my update hash and fix any custom fields, which shouldn't be an issue.

 

So lesson learned: Don't forget your namespaces and __c's!

 

It's always the little things that count. ;)

 

Thanks!

Jed

All Answers

jStarkjStark

I just noticed one typo:

 

{"ContentType" => "application/json"}

 

should be:

 

{"Content-Type" => "application/json"}

 

Fixing that now gives me http 400's instead of 415's when using the access_token.

SuperfellSuperfell

Can you post what your actual HTTP request looks like. (headers + body)

jStarkjStark

Hi Simon,

 

Thanks for the reply. After sitting back and looking at why my query was working and my update wasn't, I finally figured it out. I could update the name of one of my accounts just fine, but was having trouble with my custom object. Well, I was trying to update a custom field on that custom object. So "speed" wasn't going to cut it, but when I tried "namespace__speed__c" it worked fine.

 

Now I need to come up with an efficient way to parse my update hash and fix any custom fields, which shouldn't be an issue.

 

So lesson learned: Don't forget your namespaces and __c's!

 

It's always the little things that count. ;)

 

Thanks!

Jed

This was selected as the best answer
naveenkumarbvnaveenkumarbv

Hi Jed,

       Good to see that you have got started with REST api. 

And, I thing you might have authenticated against salesforce using OAuth.

I am a bit stuck up with connecting to salesforce itself.

Can you provide some information or any links you have which helped you accomplish this.

 

Regards,

Naveen Kumar B.V

jStarkjStark

Hello Naveen,

 

This was probably the best resource for OAuth2 and Salesforce for me:

 

http://wiki.developerforce.com/index.php/Digging_Deeper_into_OAuth_2.0_at_Salesforce.com#The_Salesforce.com_Identity_Service

 

I would suggest not using the OAuth2 Ruby gem, as I don't think it's up-to-date with the current state of OAuth2. I was using RestClient to make my calls. The above article very clearly states which parameters you need, depending on which flow you decide to use.

 

Good luck!

-Jed

naveenkumarbvnaveenkumarbv

Hello Jed,

 

    Thank you for the link, that would definitely give me some insight on how to start off.

 

If not OAuth2.0, what is the next best alternative to authenticate or connect to salesforce.  Can you read through this brief description referring to what I have understood till now, and help me take this forward.

 

Please excuse me if it is a bit lengthy, just wanted to keep things clear and explain the entire scenario. Please ignore if anything mentioned below seems inappropriate. Also, would you point out, if I am going wrong somewhere.

 

My application environment uses Ruby 1.9.2 and Rails 3.0.3 versions, which is giving me a tough time to zero upon any gem available. 

 

 

  • activesalesforce, activerecord-activesalesforce-adapter, asf-rest-adapter, asf-soap-adapter, are some of the gems I have tried to use initially.
  • asf-soap-adapter was a blocker because Ruby 1.9.2 and Rails 3.0.3 versions do not support SOAP.
  • And now using the adapter gems seemed to look a bit promising because the implementation and querying, etc would be almost similar to the way we interact with a local database since these gems internally use the activerecord pattern.
  • Later, I found in some of the forums that connecting or interacting with salesforce would now follow(and support) more of a RESTful pattern leaving behind all other approaches.

 

Based on the description of most of your posts also RESTful services looked promising.

 

QUERIES: 

 

  1. What is the other approach to authenticate to salesforce if we are not using OAuth?
  2. After this, performing CRUD operations using RESTful services would be easy.
  3. How do we add custom columns to entities and new custom entities to salesforce? 
  4. Can we use the Bulk API without any issues if we have to deal with large amounts of data, if we are initially using REST

 

Thanks Jed. Thank you very much. Awaitng your reply.

 

Regards,

Naveen Kumar B.V

 

 

jStarkjStark

Hello Naveen,

 

I'm using different versions than you, and this is my first experince with Ruby so I can't provide much input about different gems. All I know was that I was having issues POSTing requests using the OAuth2 gem. Here are my answers to a couple of your questions.

 

"What is the other approach to authenticate to salesforce if we are not using OAuth?" - Your other option is to login via SOAP. I don't believe there are any other ways, but I'm sure there are some experts here that know all of the different ways.

 

As for #3, you can do that through the declarative interface when logged into your org in the browser, or through the metadata API. There's a ton of information on both of those. I'd suggest "http://wiki.developerforce.com/index.php/Documentation".

 

For #4, I would assume yes, since the Bulk API uses REST, but I haven't used it so I can't say for sure. I am only doing authentication and basic CRUD, so anything beyond that you'll need to research elsewhere.

 

Good luck,

-Jed

naveenkumarbvnaveenkumarbv

Hello Jed,

 

          Thank you very much for your help. This would be sufficient for me to start off, and your replies helped me a lot.

Also I have started working on connecting to salesforce using OmniAuth + HttParty in Rails 3 by Quinton Wall.

 

I will get back to you if there are any updates to share with you.

Thanks once again.

 

Regards,

Naveen Kumar B.V

cloudcodercloudcoder

Omniauth is certainly a good way to go. And, the official Gem now supports Force.com :)

 

LMK if you have any questions about getting things working under Rails 3

Bramha1Bramha1

Hi,

 

  can any one help me in choosing a Ruby gem. I am developing a Bulk Loader in Ruby. I was looking at different gems and coudn't figure it out. We already have a Bulk Loader in C# where we use SOAPClient and HTTPCalls to get jobId and batchId.

 

 It would be greatful if there is any sort of perfect gem which aloows to do bulk loads. and if you have any links or useful docs abt bulk loading using ruby please post it here.

 

 

Thanks in advance.

 

Bramha