Search code examples
rubyhttparty

Syntax error in body using HTTParty (Ruby/HTTParty)


I'm just get a error and really don't know how to solve this.

What I'm trying:

class Addresses
include HTTParty
base_uri "https://reservedurl.com/"

def self.api_search
    search = get('members/api/default/member/addresses')
end

def self.api_change_address(street, new_zip, n)
    change_address = patch('members/api/default/member', {
        :body => {
          :'option_id' => '1'
          :'address' => {
            :'address_id' => '3',
            :'street_name' => '#{street}',
            :'alias' => 'Address',
            :'address_number' => '#{n}',
            :'additional_information' => 'New',
            :'reference_point' => 'New',
            :'city' => 'NY',
            :'country_code' => 55,
            :'district' => 'New',
            :'postal_code' => '#{new_zip}',
            :'state' => 'NY',
            :'default' => true,
            :'address_name' => 'string',
          }
        }.to_json
        :headers => {
          :'Content-Type' => 'application/x-www-form-urlencoded',
          :'charset' => 'utf-8',
          :'Host' => 'reservedurl.com',
          :'Accept-Encoding' => 'gzip, deflate',
          :'Accept' => '*/*',
          :'Postman-Token' => 'token'
        }
      })
end

API body:

{
  "option_id": "1",
    "address": {
    "address_id": "token",
    "street_name": "Jack, St",
    "alias": "Address",
    "address_number": "213",
    "additional_information": "New",
    "reference_point": "New",
    "city": "DC",
    "country_code": 55,
    "district": "WS",
    "postal_code": "0453401",
    "state": "WS",
    "default": true,
    "address_name": "string"
  }
}

And I got this error:

    *** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
c:/features/support/this_page.rb:13: syntax error, unexpected tSYMBEG, expecting '}'
              :'address' => {
              ^~
c:/features/support/this_page.rb:13: syntax error, unexpected =>, expecting end     
              :'address' => {
                         ^~
c:/features/support/this_page.rb:28: syntax error, unexpected '}', expecting end    
            }.to_json
            ^
c:/features/support/this_page.rb:29: syntax error, unexpected =>, expecting end
            :headers => {
                     ^~
c:/features/support/this_page.rb:37: syntax error, unexpected '}', expecting end
          })
          ^ (SyntaxError)
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/glue/registry_and_more.rb:107:in `load'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/glue/registry_and_more.rb:107:in `load_code_file'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime/support_code.rb:144:in `load_file'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime/support_code.rb:85:in `block in load_files!'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime/support_code.rb:84:in `each'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime/support_code.rb:84:in `load_files!'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime.rb:272:in `load_step_definitions'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/runtime.rb:68:in `run!'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/lib/cucumber/cli/main.rb:34:in `execute!'
C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/cucumber-3.1.2/bin/cucumber:9:in `<top (required)>'
C:/Ruby26-x64/bin/cucumber:23:in `load'
C:/Ruby26-x64/bin/cucumber:23:in `<main>'

I really don't see why HTTParty don't allow me to use this body. What I understand? The error is the bracket itself, but I'm trying to pass exactly how the API was. If I remove the bracket in address line the body don't will be read for my API.


Solution

  • You're missing commas after :'option_id' => '1' and after }.to_json,:

    {
      :body => {
        :'option_id' => '1',
        :'address' => {
          :'address_id' => '3',
          :'street_name' => '#{street}',
          :'alias' => 'Address',
          :'address_number' => '#{n}',
          :'additional_information' => 'New',
          :'reference_point' => 'New',
          :'city' => 'NY',
          :'country_code' => 55,
          :'district' => 'New',
          :'postal_code' => '#{new_zip}',
          :'state' => 'NY',
          :'default' => true,
          :'address_name' => 'string',
        }
      }.to_json,
      :headers => {
        :'Content-Type' => 'application/x-www-form-urlencoded',
        :'charset' => 'utf-8',
        :'Host' => 'reservedurl.com',
        :'Accept-Encoding' => 'gzip, deflate',
        :'Accept' => '*/*',
        :'Postman-Token' => 'token'
      }
    }