0

I need to POST a script by using BigCommerce API. Here is the body

{
  "name": "string",
  "description": "string",
  "html": "string",
  "src": "(function (sCDN, sCDNProject, sCDNWorkspace, sCDNVers) {
    if (
      window.localStorage !== null &&
      typeof window.localStorage === 'object' &&
      typeof window.localStorage.getItem === 'function' &&
      window.sessionStorage !== null &&
      typeof window.sessionStorage === 'object' &&
      typeof window.sessionStorage.getItem === 'function'
    ) {
      sCDNVers =
        window.sessionStorage.getItem('wx_preview_version') ||
        window.localStorage.getItem('wx_preview_version') ||
        sCDNVers;
    }
    window.x= window.x|| {};
    window.x.config = window.x.config || {};
    window.x.config.frontend = window.x.config.frontend || {};
    window.x.config.frontend.cdnhost =
      sCDN + '/get/' + sCDNWorkspace + '/web/' + sCDNVers + '/';
    window.x.config.frontend.vers = sCDNVers;
    window.x.config.frontend.env = sCDNWorkspace;
    window.x.config.frontend.project = sCDNProject;
    window._wx = window._wx || [];
    var f = document.getElementsByTagName('script')[0];
    var j = document.createElement('script');
    j.async = true;
    j.src = window.x.config.frontend.cdnhost + 'sss.js';
    f.parentNode.insertBefore(j, f);
  })(
    'https://xxxx.x-x-x.mybigcommerce.com',
    'x-test-env',
    'live',
    '_'
  );",
  "auto_uninstall": true,
  "load_method": "default",
  "location": "head",
  "visibility": "storefront",
  "kind": "src",
  "api_client_id": "string",
  "consent_category": "essential",
  "enabled": true,
  "channel_id": 1
}

but it returned

{ "status": 400, "title": "Input is invalid", "type": "https://developer.bigcommerce.com/api-docs/getting-started/api-status-codes", "errors": {} }

Seems like it occurred with '{' enter image description here

Any method that I can POST this script to head?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
nethsrk
  • 78
  • 8

2 Answers2

1

Make the script one lined. JSON is broken because of new line.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Puwka
  • 640
  • 5
  • 13
  • Can you please clarify whether you are recommending modifying the JavaScript to no longer have line breaks at all (which I do not suggest, but that's what people typically mean by having a script be "one-lined"), or if you are recommending encoding the text of the JavaScript properly into JSON value format by representing newline characters as `\n` as required by JSON? i.e. like `"src": "function() {\n}"` – Wyck Nov 17 '22 at 14:36
  • "src": "should be one lined here" -> this will work. "src": "if i put linebreak here it will be broken". In "src" value you should use one line. On attached screenshot author has line break inside of JSON body. It is not valid syntax for JSON. Only way to transfer this script is to make it one lined. – Puwka Nov 17 '22 at 14:38
  • I'm saying "one-lined" is not a good description of the solution. Replacing line breaks with `\n` is a better description of how to fix the problem. – Wyck Nov 17 '22 at 14:40
1

I believe src should be a URL to a script file.

If you want to embed JavaScript then place it in the HTML field and wrap a tag around it.

Tony McCreath
  • 2,882
  • 1
  • 14
  • 21