i'm in a big trouble with 415 error on http post request,
A fonctionnal guy gave me his test in postman and it works well :
{
"variables": [],
"info": {
"name": "demo",
"_postman_id": "id",
"description": "",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "1 -getToken sandbox",
"request": {
"url": "myurl",
"method": "POST",
"header": [
{
"key": "Authorization",
"value": "Basic RTlBS000T0FtN1FORFc1SUxlRVFjeXZFTEZBYTpHTlB6ZVlaWXIyN2NDbG53Y1VvZ0ZiOGJWd2dh",
"description": ""
}
],
"body": {
"mode": "urlencoded",
"urlencoded": [
{
"key": "grant_type",
"value": "password",
"description": "",
"type": "text"
},
{
"key": "username",
"value": "[user]",
"description": "",
"type": "text"
},
{
"key": "password",
"value": "[password]",
"description": "",
"type": "text"
}
]
},
"description": ""
},
"response": []
},
with the sent json, i received the bearer i wanted to receive.
In my java code, i have this :
String postUrl = "myurl";
String dataTokenJson = "{\"grant_type\":\"password\", \"username\":\"myuser\", \"password\":\"mypassword\"}";
Gson gson = new Gson();
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(postUrl);
StringEntity postingString = new StringEntity(gson.toJson(dataTokenJson));
post.setEntity(postingString);
post.addHeader("Content-Type","application/json");
post.addHeader("Authorization","Basic RTlBS000T0FtN1FORFc1SUxlRVFjeXZFTEZBYTpHTlB6ZVlaWXIyN2NDbG53Y1VvZ0ZiOGJWd2dh");
HttpResponse responseToken = httpClient.execute(post);
I tried to not using gson lib and set the StringEntity directly with the jsonString. I tried different methods to set the Header. Nothing works better.
so, I changed the content-type to "application/x-www-form-urlencoded" dont have 415 error anymore but 400.. I also tried to remove the content-type but it gave me a 415 again.
In fact, the server didnt except json but he is supposed to answer with a json.. The specifications wasnt clear
HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Fri, 18 Feb 2022 11:57:13 GMT, Server: Apache, Strict-Transport-Security: max-age=63072000; includeSubdomains;, X-Frame-Options: SAMEORIGIN, X-Frame-Options: DENY, X-Frame-Options: SAMEORIGIN, Strict-Transport-Security: max-age=63072000; includeSubdomains;, X-Content-Type-Options: nosniff, X-XSS-Protection: 1; mode=block, Content-Type: application/json, Connection: close, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/json,Chunked: true]}}