I have a JSON body as Java String. I would like to convert this String to RestAssured Response. Is this possible?
Or
Is it possible to convert apache HttpResponse to RestAssured Response
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
org.apache.http.entity.StringEntity entity = new org.apache.http.entity.StringEntity(body);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
I'd like to convert httpResponse to RestAssured Response
The Response Javadoc says "The response of a request made by REST Assured.". So, no I don't think its possible. I would make the request with RestAssurred anyway, I find it easier.
Reference: http://static.javadoc.io/com.jayway.restassured/rest-assured/1.2.3/com/jayway/restassured/response/Response.html
Related
excuse me everyone, I'm currently having problems connecting my spring boot application with an open api IoT. to send data to the open api, the request body must be encrypted and sent in json form. I have followed the available documentation, but the response says that the request parameter must be in json format.
here is my program code:
HttpPost httppost = new HttpPost(url);
httppost.addHeader("x-random-key", randomSecretKey);
httppost.addHeader("x-access-key", xAccessKey);
httppost.addHeader("Content-type", "application/json;charset=UTF-8");
httppost.addHeader("sys_code", "901");
httppost.addHeader("lang", "_en_US");
String encryptedRequestBody = encrypt(request.toString(), randomMessage);
StringEntity strEntity = new StringEntity(encryptedRequestBody);
strEntity.setContentType("application/json");
httppost.setEntity(strEntity);
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
I want to get the JSON data from solarwinds orion rest api and have to write those JSON data in excel file.
I'm assuming you need a java program to send a post request to an API endpoint. ApacheHTTP library to the rescue. You can read more from the documentation here. Even more information in the official apache website
HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("http://www.a-domain.com/foo/");
// Request parameters and other properties.
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("param-1", "12345"));
params.add(new BasicNameValuePair("param-2", "Hello!"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
//Execute and get the response.
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
try (InputStream instream = entity.getContent()) {
// do something useful
}
}
Taken from this answer
I need to post some form parameters to a server through an HTTP request (one of which is a file). So I use Apache HTTP Client like so...
HttpPost httpPost = new HttpPost(urlStr);
params = []
params.add(new BasicNameValuePair("username", "bond"));
params.add(new BasicNameValuePair("password", "vesper"));
params.add(new BasicNameValuePair("file", payload));
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpPost.setHeader("Content-type", "multipart/form-data");
CloseableHttpResponse response = httpclient.execute(httpPost);
The server returns an error, stack trace is..
the request was rejected because no multipart boundary was found
at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:954)
at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:351)
at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
at org.springframework.web.multipart.commons.CommonsMultipartResolver.parseRequest(CommonsMultipartResolver.java:156)
I understand from other posts that I need to somehow come up with a boundary, which is a string not found in the content. But how do I create this boundary in the code I have above? Should it be another parameter? Just a code sample is what I need.
As the exception says, you have not specified the "multipart boundary". This is a string that acts as a separator between the different parts in the request. But in you case it seems like you do not handle any different parts.
What you probably want to use is MultipartEntityBuilder so you don't have to worry about how it all works under the hood.
It should be Ok to do the following
HttpPost httpPost = new HttpPost(urlStr);
File payload = new File("/Users/CasinoRoyaleBank");
HttpEntity entity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("file", payload)
.addTextBody("username", "bond")
.addTextBody("password", "vesper")
.build();
httpPost.setEntity(entity);
However, here is a version that should be compatible with #AbuMariam findings below but without the use of deprecated methods/constructors.
File payload = new File("/Users/CasinoRoyaleBank");
ContentType plainAsciiContentType = ContentType.create("text/plain", Consts.ASCII);
HttpEntity entity = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addPart("file", new FileBody(payload))
.addPart("username", new StringBody("bond", plainAsciiContentType))
.addPart("password", new StringBody("vesper", plainAsciiContentType))
.build();
httpPost.setEntity(entity);
CloseableHttpResponse response = httpclient.execute(httpPost);
The UrlEncodedFormEntity is normally not used for multipart, and it defaults to content-type application/x-www-form-urlencoded
I accepted gustf's answer because it got rid of the exception I was having and so I thought I was on the right track, but it was not complete. The below is what I did to finally get it to work...
File payload = new File("/Users/CasinoRoyaleBank")
MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE );
entity.addPart( "file", new FileBody(payload))
entity.addPart( "username", new StringBody("bond"))
entity.addPart( "password", new StringBody("vesper"))
httpPost.setEntity( entity );
CloseableHttpResponse response = httpclient.execute(httpPost);
When I am using SoapUI to call this web service I am getting the correct response but when I implement this in android, I am getting the below exception,
system.web.services.protocols.soapheaderexception (some information is missing).
This is what I tried,
HttpPost httppost = new HttpPost("http://www.ocrwebservice.com/services/OCRWebService.asmx");
StringEntity se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
I tried other combinations also like,
httppost.setHeader("Accept-Charset","utf-8")
and
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8")
But nothing worked.
The error says,
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input was not a valid SOAP message because the following information is missing: action.
System.Web.Services.Protocols.SoapHeaderException: WSE012: The input
was not a valid SOAP message because the following information is
missing: action.
=> As per the above exception, I can say you forgot to set Action.
Try:
String SOAP_ACTION = "http://stockservice.contoso.com/wse/samples/2005/10/OCRWebServiceAvailablePages";
httppost.setHeader("SOAPAction", SOAP_ACTION);
I'm using HttpEntity from Apache library to download files from URLs. I.e.
String url="http://www.stackoverflow.com/question/ask/idontexist.jpg";
String user_agent=...; //I know, I can use the default value, but this is what I do actually!
HttpClient httpclient =new AutoRetryHttpClient(new DefaultServiceUnavailableRetryStrategy(5, 500));
HttpGet httpget = new HttpGet(url);
httpget.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpget.setHeader("User-Agent", user_agent);
HttpEntity entity = httpclient.execute(httpget).getEntity();
InputStream is = entity.getContent();
Now. If I save the resource from the InputStream through a FileOutputStream I get a file named idontexist.jpg, but it has no content (as expected).
How can I verify that the returned InputStream has no content or that the requested resource pointed by the URL doesn't exist?
You should first get HttpResponse object with
HttpResponse httpResponse = httpclient.execute(httpget);
Then you can get status code with
int statusCode = httpResponse.getStatusLine().getStatusCode();
and, if the resource is found, get http entity with
HttpEntity entity = httpResponse.getEntity();
Hope this helps,
Regards.