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;
}
Related
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);
I am trying to build a Rest API automation based on Java, RestAssured, and Cucumber. I ' trying to hit an endpoint via POST. The problem is when I am converting the response as string and when I print the response, it is printing the XML file contents but not the response. I also see the status code as 200. I'm not sure what is going wrong in here. Below is the sample snippet from my codebase.
I am trying to hit a WebService (SOAP WSDL).
// required imports
public class Service_Steps {
Response xmlResponse;
#When("I create a POST request for RCP for endpoint using XML")
public void i_create_a_post_request_for_endpoint_using_xml() {
// xml Request body
String path = "pah_to_xml_file";
File file = new File(path);
xmlResponse = RestAssured.given().when().relaxedHTTPSValidation().accept(ContentType.XML).header(<headers>)
.body(file)
.post(url);
String xmlResponseAsString = xmlResponse.then().extract().body().asString();
}
Not sure why I am seeing this ambiguity. Sometimes it is printing the response, and sometimes it is printing the XML file (request body) contents.
After checking with developers I came to know that the SOAP EndPoint is sending out the responses in two different ways, randomly!
Try this one:
xmlResponse = RestAssured.given().log().all().when().relaxedHTTPSValidation().accept(ContentType.XML).header(<headers>)
.body(file)
.post(url)
.then()
.log().all()
.extract()
.response();
This will print out all the request & response stuff
I need to send the json object in get request. I installed chrome Postman extension but i am not getting how can i send json object in GET request ?
Postman provides the way to send json data in Post request by adding the header as application/json and then add the json data under raw form.
How to send the json data in GET request ? Do i need to append it in URL ?
It's bad solution to send any objects using get request. But you can send it as a url parameter using url encoding:
String url = "http://example.com/query?json=" + URLEncoder.encode(json, "UTF-8");
In POSTMAN you can send body data in GET request. If you try to append in the URL using url encoding you will get error.
Try to convert json object into string and send it in the URL parameters and see if it works.
Also if your backend server allows only to send data as URL parameters and your URL is long (i.e approx 2048 characters) then I am not sure whether this will work.
If above solution doesn't work then I think you can achieve this using curl. CURL is a tool for doing all sorts of URL manipulations and transfers. You can generate cURL code using Postman. Here is the reference
You can use google chrome Postman Extension
it allows you to send and see any type of data.
I'm new to playframework and to REST API.
I want to send a POST request to REST API in a controller.
What is the best way to do it? Does play have a support for it or do I have to use a plugin?
Basically I want it to look like this:
User submits a form.
I load a form data in a controller.
I send form data as a POST request
Get response, do something with it and display result
So far I'm stuck at point 3, I have no idea how to do this.
Code to visualize what I have in mind:
public static Result processForm() {
Form<FormData> myForm = Form.form(FormData.class).bindFromRequest();
String text = myForm.get().text;
//Send 'text' in a post request and get response
text = doSomethingWithResponse(response);
return ok(resultpage.render(text));
}
I don't think it matters but this is the API I want to use:
http://open.xerox.com/Services/fst-nlp-tools/Pages/API%20Docs
The following line of code sends a request and waits for a reponse:
WS.url(feedUrl).setHeader("Content-Type", "application/x-www-form-urlencoded").post("arg1=val1&arg2=val2").get().asJson();
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.