发布请求在Java客户端不起作用,但在Postman中工作

发布于 2025-01-31 11:10:34 字数 798 浏览 3 评论 0原文

我正在尝试执行邮政请求到REST端点,并且在Java代码下执行时会失败。我回来的回应具有403禁止的状态。

我正在使用apache httpclient。 这是我使用的代码:

var postRequest = new HttpPost(myUrl);
postRequest.addHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN_VALUE);
postRequest.addHeader("x-env", environment);
postRequest.addHeader("apikey", myApiKey);

标题设置正确,并且具有与Postman中完全相同的值。从我在没有“用户代理”标头的类似帖子上看到的内容可能会导致此功能。设置无法解决我的问题,而且我的想法已经用完了。实际上,我手动从Postman设置了所有标题,但仍然没有运气。

当执行从Java的同一URL上下文时,它可以正常工作。 另外,一些JS客户端与相同的HTTP标头完全相同的端点也可以使用。这是JS代码:

const cnf = {
  headers: {
    'x-env': 'dev',
    apikey: this.myApyKey,
    'Content-Type': 'text/plain',
  },
};
const data = myRequestBody;
try {
  const res = await axios.post(this.apiPath, data, cnf as AxiosRequestConfig);
    ....

预先感谢您的输入。

I am trying to execute a POST request to a REST endpoint and it fails when executing under Java code. The response I am getting back has a status 403 forbidden.

I am using apache HttpClient.
Here is the code I am using:

var postRequest = new HttpPost(myUrl);
postRequest.addHeader(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN_VALUE);
postRequest.addHeader("x-env", environment);
postRequest.addHeader("apikey", myApiKey);

The headers are set properly and have exactly the same values as in Postman.From what I seen on similar posts not having the "User-Agent" header could cause this. Setting that did not solve my issue and I am running out of ideas. In fact I manually set all the headers from Postman and still no luck.

When executing a GET to the same URL context from Java it works.
Also some JS client accessing exactly the same endpoint with the same HTTP headers also works. here is the JS code:

const cnf = {
  headers: {
    'x-env': 'dev',
    apikey: this.myApyKey,
    'Content-Type': 'text/plain',
  },
};
const data = myRequestBody;
try {
  const res = await axios.post(this.apiPath, data, cnf as AxiosRequestConfig);
    ....

Thank you in advance for your inputs.

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

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

发布评论

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

评论(3

邮友 2025-02-07 11:10:34

尝试检查您的代码是否允许CORS请求?因为在Postman中,即使后端不允许CORS请求,它仍然执行API。但任何编程语言都不是这种情况。

Try checking whether your code allows CORS requests? Because in POSTMAN, even if backend does not allow CORS requests, It still executes the api. But that is not the case with any programming languages.

不乱于心 2025-02-07 11:10:34

我试图发布的URL在AWS中配置不佳。我没有注意到URL终点以“/”结束,但是在我的Java代码中,我试图在没有前向斜线的情况下访问相同的URL。

// So this worked:
https://my/path/to/post/resource/

// But this did not
https://my/path/top/post/resource

配置路由53以绘制带有和没有正向斜线的两条路径修复了问题。将其作为答案发布,以便其他人可以检查一下是否相同。

The URL I was trying to post to was poorly configured in AWS. I did not notice the URL end point ended up with a "/" but in my Java code I was trying to access the same URL without a forward slash.

// So this worked:
https://my/path/to/post/resource/

// But this did not
https://my/path/top/post/resource

Configuring the Route 53 to map both paths with and without a forward slash fixed the issue. Publishing this as an answer so others can check it out if getting the same.

ゞ花落谁相伴 2025-02-07 11:10:34

就我而言,我浏览了Postman自动应用的标题,并将其与我添加的标题进行了比较,在Postman中,我单击了标题以找到需要哪些响应的标题,发现它需要标题“接受”:“*/*” - 我将其添加到我的请求标题中,并且奏效了。

        String response = Request.Get(link)
                        .addHeader("Authorization", "Bearer " + accessToken)
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Accept","*/*")

In my case, I went through the headers that Postman had applied automatically and compared them with those that I had added, and in Postman, I clicked on the headers to find which ones were needed to get a 200 response, and found that it needed the header "Accept":"*/*" - I added this to my request headers and it worked.

        String response = Request.Get(link)
                        .addHeader("Authorization", "Bearer " + accessToken)
                        .addHeader("Content-Type", "application/json")
                        .addHeader("Accept","*/*")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文