酷代码 AI
菜单

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', headers:{ "content-type":"application/json;charset=utf-8" }, body:{ mode:"urlencoded", urlencoded:[ {key:"username",value:"${username}"}, {key:"password",value:"${password}"} ] } } pm.sendrequest(login, function (err, response) {

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. [2024-10-29 19:35:25 | AI写代码神器 | 419点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多