Locust POST 请求以一种格式工作,而不是另一种格式

发布于 2025-01-11 18:20:24 字数 1099 浏览 0 评论 0原文

在我们的 Locust 脚本中,我们采用了 self.client.post 遵循的标准化格式。在这种格式中,我无法让这个 post 请求工作:

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
with self.client.post("/apis/authentication/login", name=f'login-{self.client.userrole} || /apis/authentication/login', data=string, headers=auth_headers, catch_response=True) as r:

我尝试使用不同的格式,它确实适用于相同的请求:

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
requests.post("https://xyz.xyz.com//apis/authentication/login", data=string, headers=auth_headers)

在第一种格式的情况下,完整的 URL 是从标头传递的,并且我可以看到它正在尝试访问正确的地址。在第二种格式中,它无法从标题中获取它,我添加了它。但这似乎不是问题所在。

我收到的错误是: 错误请求

您的浏览器发送了该服务器无法理解的请求。

我认为这与 loginCreds 数据的传递方式有关。我们应该在 self.client.post 格式中做哪些不同的事情才能使其正确通过?

In our Locust scripts, we have a standardized format that we follow with self.client.post. In this format, I'm unable to get this post request to work:

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
with self.client.post("/apis/authentication/login", name=f'login-{self.client.userrole} || /apis/authentication/login', data=string, headers=auth_headers, catch_response=True) as r:

I tried using a different format, which does work for the same request:

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
requests.post("https://xyz.xyz.com//apis/authentication/login", data=string, headers=auth_headers)

In the case of the first format, the full URL is being passed from the headers, and I can see it is trying to hit the correct address. In the second format, it wasn't able to pick it up from the headers and I added it. But this doesn't seem to be where the issue lies.

The error I'm receiving is:
Bad Request

Your browser sent a request that this server could not understand.

I think it has to do with how the loginCreds data is being passed. Is there anything we should be doing differently in the self.client.post format to get it to pass correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2025-01-18 18:20:24

我首先尝试的是将完整的 URL 放入 self.client.post 中,而不仅仅是路由。就像您对 requests.post 所做的那样。

您还可以获取最终请求信息 来自响应对象。用它来比较实际发送的内容。

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
r = self.client.post("/apis/authentication/login", name=f'login-{self.client.userrole} || /apis/authentication/login', data=string, headers=auth_headers)
print(r.request.url)
print(r.request.headers)
print(r.request.body)

First thing I'd try is put the full URL in the self.client.post instead just the route. Do it just like you did for the requests.post.

You can also get the final request info from the response object. Use that to compare what was actually sent.

loginCreds = {"data":{"email":"[email protected]","password":"password"}}
string=json.dumps(loginCreds)
r = self.client.post("/apis/authentication/login", name=f'login-{self.client.userrole} || /apis/authentication/login', data=string, headers=auth_headers)
print(r.request.url)
print(r.request.headers)
print(r.request.body)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文