添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

I’m developing a pre-request script. This script does a POST to get a JWT that I’ll use for my main request. The POST returns with a http 200 so all good… if I comment out the attempted parse, it all completes with no issue. I have a result that passes JSONlint … so I didn’t expect any issues.

Its a simple response and I simply want to get the “val” attribute but the JSONParse fails.
The Postman console says “JSONError: Unexpected token u in JSON at position 0” and as you can see there’s no “u” attribute in the response. Yes, I see that inside the value for “val” the letter ‘u’ turns up… that can’t be the issue.

How can I sort out what’s happening so I can make this work?

“tt”: “JWT”,
“val”: “eyJraWQiOiJJYW1Kd2tLZXkiLCJhbGciOiJFUzI1NiJ9.eyJqdGkiOiJKMFZjazlsaDc2QlVQdnBrenJVU3hnIiwiaWF0IjoxNjEyMzk0NjUwLCJpc3MiOiJJQU0iLCJhdWQiOiIxMjduamNsZW14OSIsInN1YiI6IjEyN25qY2xlbXg5IiwiZXhwIjoxNjEyMzk1NTUwLCJ0a25mbyI6IntcImFjY2Vzc190b2tlblwiOlwiYTFhY2E2YjctMmZhZS00MDExLTg0OTEtM2M3ODJmMTZhYmI0XCIsXCJzY29wZVwiOltcInNlcnZpY2VJbnN0YW5jZTovY29tLmZpY28uZG1wLnMzL3Jlc3Qvc2VydmljZS9zb2x1dGlvbmluc3RhbmNlcy8xMjdubzB4c3kxaC9hcGlcIixcIlBVVDpjb25zb2xlLmRtcy5wcmV2aWV3LnVzdzIuZmljb2FuYWx5dGljY2xvdWQuY29tXCIsXCJzb2x1dGlvbklkOjEyN25qY2xlbXg5XCIsXCJvPUZJQ08tRklFTEQtVEVOQU5UXCIsXCJzZXJ2aWNlSW5zdGFuY2U6L2NvbS5maWNvLmRtcC5zMy9yZXN0L3NlcnZpY2Uvc29sdXRpb25pbnN0YW5jZXMvMTI3bm84dTk1NGIvYXBpXCIsXCJjbGllbnRfaWQ6MTI3bmpjbGVteDlcIixcIlBPU1Q6Y29uc29sZS5kbXMucHJldmlldy51c3cyLmZpY29hbmFseXRpY2N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LFwibWF4Y29tcG9uZW50c1wiOlwiNVwiLFwiZW5hYmxlZmFzdFwiOlwidHJ1ZVwifSxcInJlYWxtXCI6XCIvRmljb0FuYWx5dGljQ2xvdWRcIixcInRva2VuX3R5cGVcIjpcIkJlYXJlclwiLFwiZXhwaXJlc19pblwiOjM1OTksXCJvXCI6XCJGSUNPLUZJRUxELVRFTkFOVFwifSIsImtpZCI6IkZpY29BbmFseXRpY0Nsb3VkIn0.EuD6sBbUfFYSuJ7EgZdz7s5j4bksLl9nlfu1-i0mZSnREnXf_GWTCiu0FA34AZnSKzTjW_Lu82lRnx6taoQ6Vw”,
“v”: “1.0”,
“exp”: 1612395550

I think I see what’s happening here… but I don’t know what to do about it. If I manually create a request that’s a POST as above (same content as in my pre-request script) I get a response body… I can see it in the UI (same as above) … but if I make a POST in the pre-request script I don’t have a body… I have status and status-code and the response headers, stream… but there’s no body like I would have if I made a GET.

I can’t use GET, I have to use POST (its not my interface) …

Is there anything I can do?

Can you try to do log of response before parsing to JSON.
console.log(response.body);
And check how data is coming in response.
If there is no data for it try to do console.log(response);
Then we can try to extract the data

You don’t need to parse that response like that, it’s not the same as the main response.

You would just need to use this in the sendRequest() function:

pm.environment.set('jwt-token', response.json().val)
              

Thanks, I tried that even before posting - console.info( response.body );
I get “undefined” in the logs… which is why I’m seeing the “u” parse error that complains about an unexpected token “u” …

I DO have a response object.

{id: “344e2763-ca69-46e4-a10c-f8527b5bc2d9”, status: “OK”, code: 200…}

  • id: “344e2763-ca69-46e4-a10c-f8527b5bc2d9”
  • status: “OK”
  • code: 200
  • :arrow_forward:header: [5] :arrow_forward:stream: {…}
  • cookie: [0]
  • responseTime: 3960
  • responseSize: 2229
  • danny-dainton:

    You don’t need to parse that response like that, it’s not the same as the main response.

    You would just need to use this in the sendRequest() function:

    pm.environment.set('jwt-token', response.json().val)

    Thanks, I’ll review and maybe there’s another trick in there for me to learn.