0

This is my api request body.

{
  "product": {
    "name": "LG TV",
    "subcategory_id": 1,
    "price": 15.40,
    "quantity": 50,
    "company_name": "LG",
    "product_images_attributes": {
      "0": {
             "image": #<ActionDispatch::Http::UploadedFile:0x007ff480760350 @tempfile=#<Tempfile:/tmp/RackMultipart20180414-5482-q352rm.pdf>, @original_filename="Bill.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][image]\"; filename=\"Bill.pdf\"\r\nContent-Type: application/pdf\r\n">
           }
    }
}

I am setting content-type: 'multipart/form-data' when i try to send form then i am receiving json params.Here the params which i am receiving.

<ActionController::Parameters {"{\"product\": {\"name\": \"LG TV\",\"subcategory_id\": 1,\"price\": 15.40,\"quantity\": 50,\"company_name\": \"LG\",\"product_images_attributes\": {\"0\": {\"image\": #<ActionDispatch::Http::UploadedFile:0x007ff480760350 @tempfile"=>"#<Tempfile:/tmp/RackMultipart20180414-5482-q352rm.pdf>, @original_filename=\"Bill.pdf\", @content_type=\"application/pdf\", @headers=\"Content-Disposition: form-data; name=\\\"product[product_images_attributes][0][image]\\\"; filename=\\\"Bill.pdf\\\"\\r\\nContent-Type: application/pdf\\r\\n\">}}}}", "auth_token"=>"b864eb626eb05da3d688", "format"=>:json, "controller"=>"api/v1/products", "action"=>"create"} permitted: false>

How can I parse params and get product json?

Rohit Lingayat
  • 716
  • 6
  • 23
Raza Aslam
  • 13
  • 3
  • And you didn't consider using `JSON.parse` because...? – Myst Apr 18 '18 at 00:26
  • first, you missed closing `}` bracket on end. And to parse the response you can use JSON.parse – Rohit Lingayat Apr 18 '18 at 04:32
  • `{ "image": #, @original_filename="Bill.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][image]\"; filename=\"Bill.pdf\"\r\nContent-Type: application/pdf\r\n"> }` – Raza Aslam Apr 19 '18 at 19:54
  • I tried this and received `"#, @original_filename=\"Bill.pdf\", @content_type=\"application/pdf\", @headers=\"Content-Disposition: form-data; name=\\\"product[product_images_attributes][0][image]\\\"; filename=\\\"Bill.pdf\\\"\\r\\nContent-Type: application/pdf\\r\\n\">\n}", "auth_token"=>"b864eb626eb05da3d688", "format"=>:json, "controller"=>"api/v1/products", "action"=>"create"} permitted: false>` – Raza Aslam Apr 19 '18 at 19:56
  • when i try parse JSON.parse(params) then it will get this error. `*** TypeError Exception: no implicit conversion of ActionController::Parameters into String `@rohit @Myst – Raza Aslam Apr 19 '18 at 19:57
  • I take it all back... it seems your data might be parsed by the framework and can be accessed as is (i.e. `params["image"].data`)... – Myst Apr 19 '18 at 21:06
  • it is returning nil. – Raza Aslam Apr 28 '18 at 16:58

0 Answers0