1

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.

1 Answer 1

1

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'
}
Sign up to request clarification or add additional context in comments.

6 Comments

I did try that, but it still gave me this error: {"response":"Missing attributes: Merchant Name"}. I figured since it was in single quotes for it to work in the terminal, I'd need to make sure those read in the request.
I updated the answer for you - the problem is with the un-encoded & in the URLs
I tried this yesterday (and again now) and this is the response. {"response":"Missing attributes: Basket ID, Title, Merchant Name, Merchant URL, Product URL"}
Try removing processData: false too
Removing that with your second response worked! Thank you so much. I was using that for all my other requests since they didn't have data and I didn't think to remove it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.