2017-11-15 3 views
0

Basierend auf this Tutorial, habe ich den folgenden Code versucht. Ich versuche, der Webseite ein neues Skript hinzuzufügen.Erstellen Sie ein neues Skript-Tag (Shopify) Fehler: Ungültige URI "/"

request.post(accessTokenRequestUrl, { 
     json: accessTokenPayload 
    }) 
    .then((accessTokenResponse) => { 
      const accessToken = accessTokenResponse.access_token; 
      // DONE: Use access token to make API call to 'shop' endpoint 
      const shopRequestUrl = 'https://' + shop + '/admin/shop.json'; 
      const shopRequestHeaders = { 
       'X-Shopify-Access-Token': accessToken, 
       'Content-Type': 'application/json' 
      }; 


      const createScriptTagUrl = 'https://' + shop + '/admin/script_tags.json'; 
      const scriptTagBody = { 
       "script_tag": { 
        "event": "onload", 
        "src": "https:\/\/djavaskripped.org\/fancy.js" 
       } 
      } 

      request.get(shopRequestUrl, { 
        headers: shopRequestHeaders 
       }) 
       .then((shopResponse) => { 
        res.status(200).end(shopResponse); 
       }) 
       .catch((error) => { 
        res.status(error.statusCode).send(error.error.error_description); 
       }); 
      request.post(createScriptTagUrl, { 
        json: scriptTagBody 
       }, { 
        headers: shopRequestHeaders 
       }) 
       .then((scriptResponse) => { 
        res.status(200).end(scriptResponse); 
       }) 
       .catch((error) => { 
        res.status(error.statusCode).send(error.error.error_description); 
       }); 

Allerdings bekomme ich RequestError: Error: Invalid URI "/"

Bin ich etwas fehlt? Oder hat der Src-Wert ein Problem?

Antwort

0

Ich denke, Sie verwenden Get-Methode, um das Skript-Tag anstelle von Post zu erstellen. Bitte benutzen Sie die post-Methode und entfernen Sie \ aus dem src.

Dank

+0

ich mit 'request.post versucht (createScriptTagUrl, {json: scriptTagBody}, {Header: shopRequestHeaders})' jedoch weiterhin besteht das Problem weiterhin. Mit oder ohne \ –

0

Fixed mit dem folgenden Code. Grundsätzlich sollte der Anfragetext als JSON gesendet werden.

request.post({ 
    url: createScriptTagUrl, 
    body: scriptTagBody, 
    headers: shopRequestHeaders, 
    json: true 
}, function(error, response, body) { 
    if (!error) { 
     console.log(body) 
    } 
}); 
Verwandte Themen