3

My JSON response to be populated is as follows:

{
    "vendor":
    {
     "name": "Mozelle Luettgen MD",
     "email": "tyqmn@example.net",
     "phone_no": "9999997660",
     "addressline1": "Kulas Stravenue",
     "addressline2": "64636 Lynch Springs",
     "landmark": "Apt. 142",
     "city": "South Hannaview",
     "state": "North Dakota",
     "country": "Palau",
     "pincode": "53912-6122",
     "latitude": 50.8247548421224,
     "longitude": -81.8429583068792,
     "status": true
    } 
 }

My controller code for create vendor is

  def create 
    @vendor = Vendor.new(vendor_params)
    respond_to do |format|
    if @vendor.save
      format.html { redirect_to @vendor, notice: 'Vendor was                         
      successfully created.' }
      format.json { render :show, status: :created, location: @vendor, 
      :msg => { :status => "ok" , :result => @vendor.json, :message =>  
      "Succesfully Created" }
      }
    else
     format.html { render :new }
     format.json { render json: @vendor.errors, status: 
     :unprocessable_entity, 
     :msg =>
     { :status => "Error", :message =>  "Unprocessable Entity" }
     }
   end
  end
end


  def vendor_params
  params.require(:vendor).permit(:name, :email, :phone_no,
     :addressline1, :addressline2, :landmark, 
    :city, :state, :country, :pincode, :latitude, :longitude, :status, 
    {products_attributes: [:id, :product_name, :price]},
    {vendor_products_attributes: [:id, :vendor_product_id, :vendor_id,             
   :product_id, :copies, :_destroy]})
end

While running this link http://localhost:3000/vendors/create_vendor with post, the status shows 200 ok, but when I look for the JSON response,for the created vendor, it throws Unexpected '<' error.What am I doing wrong.Can someone please elaborate

Mahesh Mesta
  • 793
  • 1
  • 11
  • 28
  • did you check by using debugger, what params you are getting on controller? – power Sep 16 '16 at 10:20
  • I have updated the question displaying the params permitted. – Mahesh Mesta Sep 16 '16 at 10:55
  • I think @power is asking for a debugger view of what params are actually present in the controller when you run the transaction in Postman. – John Feltz Sep 16 '16 at 14:09
  • Oh right @Power ...well checking on params on binding.pry, I get the following params : {"vendor"=> {"name"=>"Mozelle Luettgen MD", "email"=>"ajczzz@example.net", "phone_no"=>"9999997660", "addressline1"=>"Kulas Stravenue", "addressline2"=>"64636 Lynch Springs", "landmark"=>"Apt. 142", "city"=>"South Hannaview", "state"=>"North Dakota", "country"=>"Palau", "pincode"=>"53912-6122", "latitude"=>50.8247548421224, "longitude"=>-81.8429583068792, "status"=>true}, "controller"=>"vendors", "action"=>"create"}. Is that what you wanted? – Mahesh Mesta Sep 17 '16 at 05:58
  • What happens if you edit your JSON return structure and change `location: @vendor` to `:location => @vendor.id`? – John Feltz Sep 17 '16 at 12:32
  • @JohnFeltz...it works same as when the value was location: vendor...So no changes occurring there – Mahesh Mesta Sep 17 '16 at 12:41
  • Well all I can suggest is that you start stripping things out of the JSON return until you find the thing that's breaking. Are you sure that the `@vendor.json` method works? – John Feltz Sep 17 '16 at 12:50
  • Well the thing is , even if I room the message part of the code, the one with @vendor.json , it will still show the same error. – Mahesh Mesta Sep 17 '16 at 12:52
  • I know this is quite old, but I've just encountered the same `Unexpected '<'` response. It turns out this is just a message from Postman's pretty-printing. Switch to the raw output to see the actual response. – pawel Aug 01 '17 at 14:41
  • This recently happened to me. For anyone seeing this now - make sure your URL is correct...forgot to add `/x` with a url scheme of `/x/y` – Michael McKenna Oct 10 '17 at 21:59

4 Answers4

0

I had the same issue and was able to get it sorted by changing the syncing link

Chamindi
  • 3
  • 1
  • 2
  • 5
0

Make sure you do not have declared twice this var User=require('../Models/user'); object of model class both in route/user.js and main server/app.js file

You declared object two times of model class

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
fazal ur Rehman
  • 330
  • 2
  • 5
0

This Setting in postman application helped me to resolve this problem

Global Proxy Configuration:ON

Proxy Type: HTTPS

Proxy Server: abc.xyz.abc.com:8080

Use System Proxy: OFF

-2

The reason is that you might have opened the session by logging into another browser or some where else while trying to connect to postman. That is the reason you are getting "unexpected '>'".

To avoid that, login to postman with the login api and try to hit your service. It will work for sure.

Shane G
  • 3,129
  • 10
  • 43
  • 85