运行测试时出现Postman错误: JSONError: 在JSON的位置0处发现意外的u标记。

4

重现错误的步骤: https://blog.getpostman.com/2016/11/09/generate-spotify-playlists-using-a-postman-collection/

我一直在按照上面的教程向Spotify发送API请求。我成功地获取了授权代码,但是尽管正确地按照教程操作,Postman在运行Spotify集合时出现了错误。 2 Successful, 2 failed

从控制台中观察,可以看到在下面的行出现了错误: error at line shown 错误信息如下 XPath runner/search already exists 运行脚本时出错:JSONError | Unexpected token u in JSON at position 0 Error executing pre-request scripts for Get Artist Top Tracks: JSONError: Unexpected token u in JSON at position 0 see bottom right corner 还有两个人遇到了相同的问题,但他们没有得到回复。有什么办法可以解决这个问题吗?我是初次使用API请求,希望能得到帮助! Two others experience the issue, unanswered question

授权的预请求脚本:

var client_credentials = (environment.client_id + ':' + environment.client_secret).encodeBase64();
postman.setEnvironmentVariable("client_credentials", client_credentials);

environment.cloned_environment && (delete environment.cloned_environment);
var environmentObjClone = _.cloneDeep(environment);
postman.setEnvironmentVariable("cloned_environment", JSON.stringify(environmentObjClone));

为艺术家编写的预请求脚本:

 try {
    var artist_id = _.sample(JSON.parse(environment.artists));
    postman.setEnvironmentVariable("id", artist_id);
}
catch (e) {
    console.log(e);
    throw false;
}

JSON:

  {
        "id": "b2b45667-7ec9-4d5d-9e38-c976d181c2bd",
        "name": "SpotifyGenV1.template1",
        "values": [
            {
                "key": "artists",
                "value": "[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]",
                "enabled": true
            },
            {
                "key": "N",
                "value": "5",
                "enabled": true
            },
            {
                "key": "user_id",
                "value": "marcz2007",
                "enabled": true
            },
            {
                "key": "country_code",
                "value": "US",
                "enabled": true
            },
            {
                "key": "client_id",
                "value": "744a1316f9374a27b7ec9bc7eabb0f92",
                "enabled": true
            },
            {
                "key": "client_secret",
                "value": "---secret--",
                "enabled": true
            },
            {
                "key": "refresh_token",
                "value": "AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w",
                "enabled": true
            },
            {
                "key": "client_credentials",
                "value": "NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=",
                "enabled": true
            },
            {
                "key": "cloned_environment",
                "value": "{\"artists\":\"[“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]\",\"N\":\"5\",\"user_id\":\"marcz2007\",\"country_code\":\"US\",\"client_id\":\"744a1316f9374a27b7ec9bc7eabb0f92\",\"client_secret\":\"3fb5c600d84a45588fe3bec81297a83f\",\"refresh_token\":\"AQAf0rvuNBo23wiIiyWQRfavFvx7JdTjvGYLufOBAM1ABtvXWvfqqo_I0IfhFAelTPIh8w2cscS4C1k8QLKOc1rqn18CjKJbmczfu1X-EfEFaWjcDbIPYGCIGZmbNZv5DgiJ9w\",\"client_credentials\":\"NzQ0YTEzMTZmOTM3NGEyN2I3ZWM5YmM3ZWFiYjBmOTI6M2ZiNWM2MDBkODRhNDU1ODhmZTNiZWM4MTI5N2E4M2Y=\",\"authorization\":\"BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0\",\"playlist_id\":\"0cfptAtDW6jHMeWauwQ13w\",\"artist_name\":\"Oasis\",\"artistID\":\"2DaxqgrOhkeH0fpeiQq2f4\",\"trackID\":\"4AKUOaCRcoKTFnVI9LtsrN\"}",
                "enabled": true
            },
            {
                "key": "authorization",
                "value": "BQD6I914g3iIt3zs1lpZl7nriwNgkFNMuIjIPjEa6ElTZw_PEbx3ewZkb38zlhypR5BfuyXLUxdJ0WyZ1TWETMSPYpPwBk7Olf6E5J49Q67BhgFWp5A4HyxYHgXNO-56sbOGx1aszETJ24Q8uGfOizzsv9WhpVU2cuC8lJdA3K2aYqc0L-8JnmD0VIrQhwX0",
                "enabled": true
            },
            {
                "key": "playlist_id",
                "value": "0cfptAtDW6jHMeWauwQ13w",
                "enabled": true
            },
            {
                "key": "artist_name",
                "value": "Oasis",
                "enabled": true
            },
            {
                "key": "artist_id",
                "value": "2DaxqgrOhkeH0fpeiQq2f4",
                "enabled": true
            },

        ],
        "_postman_variable_scope": "environment",
        "_postman_exported_at": "2019-07-28T12:45:10.195Z",
        "_postman_exported_using": "Postman/7.3.4"
    }

由于您收到了400错误,很可能是代码出了问题。您能否分享一下预请求脚本中这些请求的内容图片?该博客是3年前的,因此Spotify也有可能发生了变化。从那个错误来看,我怀疑与解析请求正文有关。 - Danny Dainton
是的,可能就是这个问题。我已经在上面编辑了预请求脚本(对于 https://api.spotify.com/v1/artists/{{id}}/related-artists 这个链接之前是空的)。你有没有发现哪里有错呢? - marcz2007
我遇到了同样的问题,真的很奇怪的是这个调用以前是可以工作的。我甚至做了一个演示,自那以后代码就没有改过。而且,是的,我设置的环境和之前一样。 - Dan Csharpster
2个回答

2
我还没有详细阅读你的问题,但是当你尝试获取不存在的Postman变量时,JSONError: Unexpected token u in JSON at position 0 是典型的错误信息(因为拼写错误或其他原因)。与此密切相关的错误信息是JSONError: Unexpected token '<' at 1:1<!doctype html>^,它通常发生在你尝试请求一个不存在的URL时。(链接到相关页面)

0

缺少环境变量是其中一个原因。还要确保该值存在于“初始值”部分,而不仅仅是“当前值”部分。

另一个原因是在变量中意外地留下了回车符,这将导致以下错误:

JSON 中出现意外的标记 \n...

删除回车符(Postman UI 将其显示为回车符号而不是字符本身),即可解决此问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接