Dtrav
Dtrav

Reputation: 417

Converting Curl request to jQuery AJAX

I'm having trouble getting thing everything after -d except for the website. Curl command:

curl -H “Authorization: Token #{auth_token}” -X GET -d ‘basket_id=#{basket_id}&price=#{price}&title=#{title}&merchant_url=#{merchant_url}&comment=#{comment}&product_url=#{product_url}&merchant_name=#{merchant_name}&color=#{color}&size=#{size}&product_image_url=#{product_image_url}’ http://localhost:3000/api/v1/baskets/add

This is what I have so far:

$.ajax({
        url: "http://localhost:3000/api/v1/baskets/add",
        type: 'GET',
        processData: false,
        headers: { 'Authorization' : token_string },
         data: "'basket_id=1&price=22800&title=Tory%20Burch&merchant_url=https://www.bloomingdales.com&product_url=https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492&merchant_name=Bloomingdales&color=Black/Gold&size=5&product_image_url=https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg'",
        success: function (data) {
          window.response = JSON.stringify(data);
          console.log(response);
          console.log(data);
        },
        error: function(){
         console.log("Cannot get data");
        }
    });

And this is the response I'm getting in the browser: {"response":"Missing attributes: Basket ID, Merchant Name"}

I have other curl commands like: curl -H “Authorization: Token #{auth_token}” -X GET http://localhost:3000/api/v1/baskets/ work fine and I'm getting a response from the server so it definitely doesn't have anything to with the authorization token or the link. Any help would be appreciated.

I should also mention that the string for data that's in the ajax request minus the double quotes, works perfectly in Terminal for me and the request goes through.

Upvotes: 1

Views: 525

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

The issue is because you've wrapped the data in double and single quotes - it should be one or the other:

data: 'basket_id=1&price=22800&title=Tory%20Burch&merchant_url=https://www.bloomingdales.com&product_url=https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492&merchant_name=Bloomingdales&color=Black/Gold&size=5&product_image_url=https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg',

Although you can also provide an object which allows jQuery to URL encode the values for you, as you may encounter some problems with the ampersand (&) characters in the URLs you send. You'll also need to remove processData: false. Try this:

data: {
  basket_id: 1,
  price: 22800,
  title: 'Tory%20Burch',
  merchant_url: 'https://www.bloomingdales.com',
  product_url: 'https://www.bloomingdales.com/shop/product/tory-burch-minnie-travel-ballet-flats?ID=1830976&CategoryID=16963#fn=ppp%3D%26spp%3D2%26sp%3D1%26rid%3D121%7CBOOST%20SAVED%20SET%26spc%3D492%26rsid%3Dundefined%26pn%3D1%7C6%7C2%7C492',
  merchant_name: 'Bloomingdales',
  color: 'Black/Gold',
  size: '5',
  product_image_url: 'https://images.bloomingdales.com/is/image/BLM/products/2/optimized/9262012_fpx.tif?wid=800&qlt=90,0&layer=comp&op_sharpen=0&resMode=sharp2&op_usm=0.7,1.0,0.5,0&fmt=jpeg'
}

Upvotes: 1

Related Questions