Making a HTTP POST SOAP request with JSON - java

I am making a SOAP request to a web method using HTTP Post. The request and response are both JSON.
But while making the POST request i am getting an error :
The server cannot service the request because the media type is
unsupported.
This is my code
String SOAP_ACTION = "Method name";
String URL = "service url";
HttpPost httpPost = null;
httpPost = new HttpPost(URL);
httpPost.addHeader("Accept-Encoding", "gzip,deflate");
httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
httpPost.addHeader("SOAPAction", SOAP_ACTION);
HttpEntity postEntity = new StringEntity(requestContent);
httpPost.setEntity(postEntity);
I have tried giving Accept-Encoding as application/json , text. But i still get the same error.

SOAP is an XML-based protocol. See https://en.wikipedia.org/wiki/SOAP and the W3C SOAP Primer.
If you want to communicate using JSON, that's fine - but it isn't SOAP.

Related

Http POST failing with status code: 406 and with status: Not Acceptable

I was trying to post some data to AWS EC2 rest end-point, from an AWS Lambda. I was getting response "with status code: 406 and with status: Not Acceptable". I tried fixing this, with adding accept headers as below
HttpPost httpPost = new HttpPost(serviceURL);
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/xml");
httpPost.addHeader(HttpHeaders.ACCEPT, "*/*");
httpPost.addHeader(HttpHeaders.ACCEPT_CHARSET, "*");
httpPost.addHeader(HttpHeaders.ACCEPT_ENCODING, "*");
httpPost.addHeader(HttpHeaders.ACCEPT_LANGUAGE, "*");
httpPost.setEntity( new InputStreamEntity(content, ContentType.APPLICATION_XML) );
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
CloseableHttpResponse response = httpClient.execute(httpPost);
Am i missing something? Is there anything else that i should be looking into?
Attach exception log please. Are u trying to send this method though any kind of front

Grant_type missing in request

I'm trying to get token access (protocol oauth2) to dynamics 365.
That's the code that build and execute the http post request:
URI uri = new URIBuilder()
.setScheme("https")
.setHost("login.microsoftonline.com")
.setPath("/"+PropertyUtils.getInstance().getProperty("AD_TENANT_ID")+"/oauth2/token")
.setParameter("grant_type", "client_credentials")
.setParameter("client_id", PropertyUtils.getInstance().getProperty("CLIENT_ID"))
.setParameter("resource", PropertyUtils.getInstance().getProperty("RESOURCE"))
.setParameter("client_secret", PropertyUtils.getInstance().getProperty("CLIENT_SECRET"))
.build();
HttpPost post = new HttpPost(uri);
HttpResponse response = client.execute(post);
the response json is:
{"error":"invalid_request","error_description":"AADSTS900144: The request body must contain the following parameter: 'grant_type'.\r ...
why response tell me that grant_type is missing when it's in the request as a parameter?
You are trying to perform the request putting those parameters in the URI as query parameters. Although those parameters needs to be put in the body of the request as form url encoded.
{"Content-Type": "application/x-www-form-urlencoded"}

Modifying Sharepoint List From External Web Application REST API

I am attempting to modify items in a Sharepoint list using the Sharepoint REST API from back-end java code. I am using NTLM authentication to access the server and Apache httpclient to perform the http requests. I am able to perform a GET request to the list URL without issue, but when doing a POST request, I am receiving an error:
The type SP.ListItemEntityCollection does not support HTTP PATCH
method
Here is the relevant code for the POST request:
JsonObject type = new JsonObject();
type.addProperty("type", "SP.Data.MyListListItem");
JsonObject listJson2 = new JsonObject();
listJson2.add("__metadata", type);
listJson2.addProperty("Title", "test");
String backToString = listJson2.toString();
CloseableHttpClient httpClient2 = HttpClients.createDefault();
HttpPost httpPost2 = new HttpPost(LIST_URL);
httpPost2.setEntity(new StringEntity(backToString));
httpPost2.addHeader("X-RequestDigest", formDigestValue);
httpPost2.addHeader("Content-Type", "application/json;odata=verbose");
httpPost2.addHeader("Accept", "application/json;odata=verbose");
httpPost2.addHeader("X-HTTP-Method", "MERGE");
httpPost2.addHeader("IF_MATCH", "*");
CloseableHttpResponse response2 = httpClient2.execute(httpPost2, context);
Am I doing something wrong here? I am following the post request from https://dev.office.com/sharepoint/docs/sp-add-ins/working-with-lists-and-list-items-with-rest.

HTTPClient's PATCH method not allowing body entity?

I am using Apache HTTPClient version 4.3.5.
I am trying to create a HTTP PATCH request.
HttpPatch request = new HttpPatch(ServerURL);
StringEntity params = new StringEntity(requestBody, ContentType.APPLICATION_JSON);
request.setEntity(params);
client.execute(request);
On checking the actual request received, it didn't have a body associated with it.
Similar code is working fine for HTTP POST requests.

Handling HTTP request redirect in java

I'm writing a network android application that uses http requests to get data. The data is HTML format. I use Apache HttpClient and JSoup.
When I'm out of traffic with my mobile internet provider, I am always redirected to the providers' page saying that I should pay some money. Of course, it is a bad idea to parse this page.
How to detect occured page substitution?
This code will help you to know with is the final target of your request, if isn't the page that you asked for, is the provider page.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet("http://www.google.com/");
HttpResponse response = httpclient.execute(httpget, localContext);
HttpHost target = (HttpHost) localContext.getAttribute(
ExecutionContext.HTTP_TARGET_HOST);// this is the final page of the request
System.out.println("Final target: " + target);
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
Thanks
If your provider is lying to you by immediately returning a 200 OK but not giving you the resource you've requested, your best option is probably to set a custom HTTP response header that your client can check before continuing.

Categories