酷代码 AI
菜单
服务商

"Fixing Postman Script: Correcting Login POST Request with JSON Data"

It looks like you're trying to send a POST request using Postman's scripting capabilities to log in to an application. However, there are a few issues with the script that need to be addressed: 1. Body Mode: The `body` should be set to `raw` and the content type should be `application/json` if you are sending JSON data. 2. Variable Interpolation: In Postman scripts, variable interpolation is done using `{{variableName}}` within strings, not `${variableName}`. 3. Function Name: The correct function to send a request in Postman is `pm.sendRequest`, not `pm.sendrequest`. Here is the corrected version of your script: ```javascript const username = pm.environment.get("username"); const password = pm.environment.get("password"); const url = `http://1.92.154.61:8800/app/common/login`; const login = { url: url, method: 'POST', header: { 'Content-Type': 'application/json' }, body: { mode: 'raw', raw: JSON.stringify({ username: username, password: password }) } }; pm.sendRequest(login, function (err, response) { if (err) { console.error(err); } else { console.log(response.json()); } }); ``` Explanation: 1. Header: Changed `headers` to `header` and set the `Content-Type` to `application/json`. 2. Body: Set the `mode` to `raw` and used `JSON.stringify` to convert the object to a JSON string. 3. Variable Interpolation: Used `{{username}}` and `{{password}}` directly in the JSON object. 4. Function Call: Corrected the function name to `pm.sendRequest`. This script will send a POST request to the specified URL with the username and password from the environment variables, and it will log the response to the console. ######[AI写代码神器 | 419点数解答 | 2024-10-29 19:35:25]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]