简体   繁体   English

POST 请求在 Java 客户端中不起作用,但在 postman 中起作用

[英]POST request does not work in Java client but works in postman

I am trying to execute a POST request to a REST endpoint and it fails when executing under Java code.我正在尝试对 REST 端点执行 POST 请求,但在 Java 代码下执行时失败。 The response I am getting back has a status 403 forbidden.我得到的响应状态为 403 禁止。

I am using apache HttpClient.我正在使用 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.标头设置正确并且具有与 Postman 中完全相同的值。从我在类似帖子中看到的没有“用户代理”header 可能会导致此问题。 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.事实上,我手动设置了 Postman 的所有标头,但仍然没有成功。

When executing a GET to the same URL context from Java it works.当从 Java 对相同的 URL 上下文执行 GET 时,它起作用了。 Also some JS client accessing exactly the same endpoint with the same HTTP headers also works.此外,一些 JS 客户端使用相同的 HTTP 标头访问完全相同的端点也有效。 here is the JS code:这是 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);
    ....

Thank you in advance for your inputs.提前感谢您的投入。

Try checking whether your code allows CORS requests?尝试检查您的代码是否允许 CORS 请求? Because in POSTMAN, even if backend does not allow CORS requests, It still executes the api.因为在 POSTMAN 中,即使后端不允许 CORS 请求,它仍然会执行 api。 But that is not the case with any programming languages.但任何编程语言都不是这样。

The URL I was trying to post to was poorly configured in AWS.我试图发布到的 URL 在 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.我没有注意到 URL 端点以“/”结尾,但在我的 Java 代码中,我试图在没有正斜杠的情况下访问相同的 URL。

// 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.将 Route 53 配置为使用和不使用正斜杠映射两条路径解决了该问题。 Publishing this as an answer so others can check it out if getting the same.将此作为答案发布,以便其他人可以查看是否得到相同的答案。

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.就我而言,我浏览了 Postman 自动应用的标头,并将它们与我添加的标头进行了比较,在 Postman 中,我单击标头以查找需要哪些标头才能获得 200 响应,并发现它需要header "Accept":"*/*" - 我将其添加到我的请求标头中并且有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PostMan上发布数据有效,而在Java上不起作用 - Post data on PostMan works, whereas on java doesn't work REST POST请求不适用于java但适用于REST客户端 - REST POST request not working on java but works on REST client REST请求在RestClient中有效,但在PostMan / Java中无效 - REST request works in RestClient but not PostMan/Java Java中Postman和POST请求之间的区别 - the difference between Postman and POST request in Java 如何使用java复制邮递员POST请求 - How to replicate Postman POST request using java 使用Firefox无法与Java-Client一起使用,Chrome运行良好 - Using Firefox does not work with the Java-Client, Chrome works well 为什么要通过 Map<string,string> 在 postman 作为 JSON 工作,但通过 map 在 ZD52387880E1EA22817A72D37592 工作?</string,string> - Why passing Map<String,String> in postman as JSON works but passing the map in Java RestTemplate does not work? 使用Jersey到API的POST请求出错,适用于POSTMAN - Error on POST request with Jersey to API, works fine with POSTMAN 将 postman 表单数据发布请求转换为 java 请求 - Convert a postman form-data post request to a java request 为什么无法将此Python POST请求转换为Java? - Why does converting this Python POST request to Java not work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM