Send JSON request body without identifiers SPRING Boot - java

I am consuming a legacy rest service that receives xml text in the body but is sent as json as shown in the following image
Postman source request
I have done the process to transform the previous request to a request that receives a JSON in normal format
New JSON request
And then I transform it into the format that asks me for the source request, my problem is that I do not know how to send my string request because I get the following error when I try to send it
Request error new http request
I get the same error when I sent in my source request in plain text format
Plain text error legacy http request
however in code I already transformed this text into JSON format but probably incorrectly,in the following sample code the http entity is the xml string object required by the legacy service
#Value("${client.medExpInsuranceQuotation.uri}")
private String clientUri;
#Autowired
#Qualifier("restTemplategetPolicyWs")
private RestTemplate restTemplate;
#Override
public Object callMedicalExpenseInsuranceQuotation(MedicalExpenseInsuranceQuotationRequestClient requestClient) {
Gson gson = new Gson();
Object json = gson.toJson(requestClient.getXml(), String.class);
System.out.println("Object: "+json);
HttpEntity<Object> entity = new HttpEntity<>(json);
log.info(requestClient.getXml());
ResponseEntity<ResponseBean<Object>> responseEntity = restTemplate.exchange(clientUri, HttpMethod.POST, entity, new ParameterizedTypeReference<ResponseBean<Object>>() {
});
return responseEntity.getBody().getPayload();
}
Notes:
The legacy service only receives the body in the format indicated in image 1 (Postman source request)
if I try to send the body in traditional json format with an attribute identifier
attemp send normal json format
I get the following error without information
{
"Message": "An error has occurred."
}
I hope you can help me greetings

That rest service is consuming json format. Note the double quotes at the start and the end of the payload - this makes it json string. Not json object, or json array, but json string. You need to transform the xml payload into json string and send the correct content type header - application/json.
Edit: To get json string first you need the xml as a string(it's already a string, i know that sounds strange, but can't explain it better). Then you call toJson() on that xml. Something similar to this:
String xml = "<xml><tag></tag></xml>";
String jsonString = gson.toJson(xml);
System.out.println(jsonString);

Related

Turn HttpExchange POST JSON request body to string

There are some other longer solutions I found, but I was wondering if the following works as well:
I have HttpExchange message, that is a POST request that is passed in, and write String jsonPayload = new String(message.getRequestBody().readAllBytes());. Would this turn the request body's json to a string?

how to send the data in request body as json to resttemplate.exchange

Assume I am getting the values from one API, need to send that data to other API through resttemplate.exchange
I am iterating the values like below
String Data="";
for(String value: receivedvalues){
Data=Data.concat(","+val)
}
List<map> result=resttemplate.exchange(url+Data,HttpMethod.GET,ArrayList.class).getBody);
The above problem is I'm adding data to request url, but i need to send through the request body but in this case how we can send data through the resttemplate.exchange.Can any one have any idea on this.
Thanks in advance.
Map your JSON object to POJO class and then use RestTemplate.exchange(...) method.
Usage example:
ReceivedValuesClass receivedValuesPojo = mapJson(receivedValues);
RequestEntity request = RequestEntity
.get(new URI(url))
.accept(MediaType.APPLICATION_JSON)
.body(receivedValuesPojo);
ResponseClass result=resttemplate.exchange(url,HttpMethod.GET,ResponseClass.class).getBody;
More information here: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html#exchange-org.springframework.http.RequestEntity-java.lang.Class-

Rest Assured Automation

How to post request JSON Data and attach file to upload
actually there is a form which contains attachment so how to send request to fill form and attach image file
Response response = given().config(config).header(key, value).header(contentType, "multipart/mixed")
.multiPart("attachment[]", upleadFile).body(jsonBody).when().post(apiURL).then().statusCode(200).extract().response();
I have got solution use this.
In first we are passing exact key like attachment[]
and for other objects
{
Response response =
given().header(key, value).
multiPart("attachment[]", new File("E:/Lightshot/Screenshot_2.png")).
multiPart("issueTypeId", json.get("issueTypeId").toString()).
multiPart("relationJson",json.get("relationJson").toString()).
multiPart("url",json.get("url").toString())
.when().post(apiURL).then().statusCode(200).extract().response();
return response;
}

Jersey: How to log HTTP POST data?

I have a code block like
final Invocation.Builder builder = webTarget.request();
final Entity<IFSRequestPresentation> entity = Entity.entity(ifsRequestPresentation, MediaType.APPLICATION_JSON);
final Response response = builder.post(entity);
The server (which is external to me and I can't see logs) is not returning me the data I expect. I believe that the json payload is not good
The IFSRequestPresentation is quite complex and I would like to see how it is represented as json String.
I am using Spring(4.0.3) and Jersey(2.8).
Is there a way I can log builder.post(entity) method? or at least see what entity looks like as json String?
I wrote it to file like
mapper.writeValue(new File("my.json"), IFSRequestPresentation);
as per Jackson documentation and got the json written to that file

how to access Salesforce Attachment Body (base64 binary data) using java?

I am working on java application for getting attachments from salesforce.
I have to show the salesforce attachments in my java application for particular object like Leads,Contacts etc. For that i am using Rest Api and got response body. But in response body there is url but i want binary data of attachment body.
I get response in body in following format:
{
Body = "/services/data/v23.0/sobjects/Attachment/00P90000004SRFlEAO/Body";
ContentType = "application/video";
Name = "Video.MOV";
attributes = {
type = Attachment;
url = "/services/data/v23.0/sobjects/Attachment/00P90000004SRFlEAO";
};
}
You get the actual attachment by performing a GET request to the Url returned in the Body field.

Categories

Resources