Rest api assets bug! - is not a valid file error

Highlighted
New Member
4 0 0

There is a bug with rest api. When I try to upload js file that is more than 9kb, it shows "422{"errors":{"asset":["\/ajax\/libs\/d3js\/5.15.1\/d3.min.js is not a valid file."]}}" error. When file is less than 9kb it works fine, just adding one more comment line shows error so it must be something about file size limit. File is a valid js file, I even tried direct jquery file from google host:

PUT /admin/api/2020-04/themes/THEME_ID/assets.json
{
"asset": {
"key": "assets/test.js",
"src": "https://ajax.googleapis.com/ajax/libs/d3js/5.15.1/d3.min.js"
}
}

There is no information about file size limit in rest api documentation and 10kb is very low size. Is there any solution to this bug?

0 Likes
Highlighted
Shopify Partner
520 38 109

I just tested this out, pulling a ~28 KB image file and putting it into my theme assets. It worked fine. Weird...

 

PUT https://{my_shop}.myshopify.com/admin/api/2020-04/themes/{my_theme}/assets.json HTTP/1.1
Content-Type: application/json
Authorization: Basic {my_creds}
User-Agent: PostmanRuntime/7.26.1
Accept: */*
Host: {my_shop}.myshopify.com
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 114
Cookie: _master_udr=eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaEpJaWszWVdWalpqTTVOUzFtTUdVekxUUTJNVFF0T0dGalppMDROR016TmpRNU5qVTJZVGdHT2daRlJnPT0iLCJleHAiOiIyMDIyLTA2LTI0VDE2OjEwOjE0LjY2NloiLCJwdXIiOiJjb29raWUuX21hc3Rlcl91ZHIifX0%3D--4068fe267f2de487051bbd18551f94b673f829f2; _secure_admin_session_id_csrf=6450872a722a54ddd55819f3c0d70127; _secure_admin_session_id=6450872a722a54ddd55819f3c0d70127; __cfduid=d474d283195031f2c442a1f3fcd4abe0d1591975172; _orig_referrer=https%3A%2F%2F238037e5e0fad0896680083eef3fee7d%3A813501fbe190445351977b5f9d995f42%40{my_shop}.myshopify.com%2Fadmin%2Fapi%2F2020-04%2Fcustomer%2F305519113; _shopify_y=cafb78a9-ebc4-44ad-a337-92cea8377f0f; _y=cafb78a9-ebc4-44ad-a337-92cea8377f0f; _landing_page=%2Fadmin%2Fauth%2Flogin

{
  "asset": {
    "key": "assets/4.sm.jpg",
    "src": "https://www.gstatic.com/webp/gallery/4.sm.jpg"
  }
}
HTTP/1.1 200 OK
Date: Mon, 29 Jun 2020 15:42:52 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
X-Sorting-Hat-PodId: 161
X-Sorting-Hat-ShopId: 3036253
Vary: Accept-Encoding
Referrer-Policy: origin-when-cross-origin
X-Frame-Options: DENY
X-ShopId: 3036253
X-ShardId: 161
X-Stats-UserId: 
X-Stats-ApiClientId: 309925
X-Stats-ApiPermissionId: 8304915
X-Shopify-API-Terms: By accessing or using the Shopify API you agree to the Shopify API License and Terms of Use at https://www.shopify.com/legal/api-terms
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT: 1/40
X-Shopify-Shop-Api-Call-Limit: 1/40
X-Shopify-API-Version: 2020-04
Location: /admin/themes/{my_theme}/assets?asset%5Bkey%5D=assets%2F4.sm.jpg
Strict-Transport-Security: max-age=7889238
X-Request-Id: f6e6cbc8-c36f-44e5-86b5-0022877b890d
X-Shopify-Stage: production
Content-Security-Policy: default-src 'self' data: blob: 'unsafe-inline' 'unsafe-eval' https://* shopify-pos://*; block-all-mixed-content; child-src 'self' https://* shopify-pos://*; connect-src 'self' wss://* https://*; frame-ancestors 'none'; img-src 'self' data: blob: https:; script-src https://cdn.shopify.com https://cdn.shopifycdn.net https://checkout.shopifycs.com https://js-agent.newrelic.com https://bam.nr-data.net https://api.stripe.com https://mpsnare.iesnare.com https://appcenter.intuit.com https://www.paypal.com https://js.braintreegateway.com https://c.paypal.com https://maps.googleapis.com https://www.google-analytics.com https://v.shopify.com https://widget.intercom.io https://js.intercomcdn.com 'self' 'unsafe-inline' 'unsafe-eval'; upgrade-insecure-requests; report-uri /csp-report?source%5Baction%5D=update&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fassets&source%5Bsection%5D=admin_api&source%5Buuid%5D=f6e6cbc8-c36f-44e5-86b5-0022877b890d
X-Content-Type-Options: nosniff
X-Download-Options: noopen
X-Permitted-Cross-Domain-Policies: none
X-XSS-Protection: 1; mode=block; report=/xss-report?source%5Baction%5D=update&source%5Bapp%5D=Shopify&source%5Bcontroller%5D=admin%2Fassets&source%5Bsection%5D=admin_api&source%5Buuid%5D=f6e6cbc8-c36f-44e5-86b5-0022877b890d
X-Dc: gcp-us-central1,gcp-us-central1
NEL: {"report_to":"network-errors","max_age":2592000,"failure_fraction":0.01,"success_fraction":0.0001}
Report-To: {"group":"network-errors","max_age":2592000,"endpoints":[{"url":"https://monorail-edge.shopifycloud.com/v1/reports/nel/20190325/shopify"}]}
CF-Cache-Status: DYNAMIC
cf-request-id: 03a257e37f0000fdd12c144200000001
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 5ab0c27f3c6afdd1-ORD
alt-svc: h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400
Content-Length: 277

{"asset":{"key":"assets\/4.sm.jpg","public_url":"https:\/\/cdn.shopify.com\/s\/files\/1\/0303\/6253\/t\/41\/assets\/4.sm.jpg?v=1593445371","created_at":"2020-06-29T15:42:51Z","updated_at":"2020-06-29T15:42:51Z","content_type":"image\/jpeg","size":24906,"theme_id":{my_theme}}}
1 Like
Highlighted
New Member
4 0 0

I apologize for my mistake, it seems to be server side problem on my side. I am generating js file with php. I tested now response with websniffer.cc and it is weird that when the content excess some size it will automatically add 1f4d line on top which makes javascript code invalidated. Anyway this is not Shopify problem so this question can be closed.

0 Likes
Highlighted
Shopify Partner
47 0 6

It is because of Transfer-Encoding: chunked header that chunks the content when js is genereting with PHP. To resolve the problem just use ob_start() with header("Content-Length: ".ob_get_length()). When header("Content-Length: ".ob_get_length()) is used, it won't use chunked Transfer-Encoding.

0 Likes