I need to download and save file. I'm trying to use HTTPBuilder because it has simple API and supports cookies. I have written following code:
//create new httpBuilder and set cookies
def httpBuilder = ...
def file = ...
def inputStream = httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY)
FileUtils.copyInputStreamToFile(inputStream)
How can I check that file is correctly downloaded (not only the part of the file)?
For large files exception java.lang.OutOfMemoryError: Java heap space occurs on line def inputStream = httpBuilder.get... How can I solve it?
May be it's not best choise to download files by HTTPBuilder. What is the best way to download file with cookies support?
Have you tried HttpBuilder GET request with custom response-handling logic:
httpBuilder.get(uri: urlData.url, contentType: ContentType.BINARY) {
resp, inputStream ->
FileUtils.copyInputStreamToFile(inputStream)
}
If HttpBuilder has problems which is strange then you can always use the tried and true Apache HttpClient API which has full cookie support.
HttpGet req = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(req);
// validate response code, etc.
InputStream inputStream = response.getEntity().getContent();
You can add a localContext when executing the request to manage cookies.
Related
I'm a beginner using ScriptRunner and Groovy.
I have a post request that returns an array of string. I thought it was working fine until I compared it with my Javascript post request response. (same endpoint with same param data)
How do I confirm I'm getting cache response? or how do I make sure I dont' get cache response? (if it is server-side cache, shouldn't I get the same response through Javascript call as well?)
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setInstanceFollowRedirects( false );
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
conn.setUseCaches( false );
DataOutputStream wr = new DataOutputStream( conn.getOutputStream());
wr.write( postData );
def statusArrayString = new StringBuffer();
def rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
def line;
while((line=rd.readLine()) !=null) {
statusArrayString.append(line);
}
According to the HTTP RFC the POST requests are not cached if you do not specifically set specific headers. And usually you don't.
But if you want to be sure, just add some dummy query string (e.g. timestamp) to the end of URL.
I am using the following code to encode the SAMLRequest value to the endpoint, i.e. the XYZ when calling https://login.microsoftonline.com/common/saml2?SAMLRequest=XYZ.
Is this the correct way to encode it?
private static string DeflateEncode(string val)
{
var memoryStream = new MemoryStream();
using (var writer = new StreamWriter(new DeflateStream(memoryStream, CompressionMode.Compress, true), new UTF8Encoding(false)))
{
writer.Write(val);
writer.Close();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Base64FormattingOptions.None);
}
}
If you just want to convert string to a base64 encoded string, then you can use the following way:
var encoded = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(val));
Console.WriteLine(encoded);
return encoded;
Yes, that looks correct for the Http Redirect binding.
But don't do this yourself unless you really know what you are doing. Sending the AuthnRequest is the simple part. Correctly validating the received response, including guarding for Xml signature wrapping attacks is hard. Use an existing library, there are both commercial and open source libraries available.
I need to add json as multipart body part to "multipart/form-data" request using groovy.
I can do this using file attachments:
testRunner.testCase
.testSteps["/job/result"]
.getHttpRequest()
.attachBinaryData(json.toString().getBytes(), "application/json").contentID = "info"
My problem is "attachBinaryData" creates a temp file per request. It is not good for load tests :)
Is there other possibility to add body parts, without file attachments?
something like :
testRunner.testCase
.testSteps["/job/result"]
.getHttpRequest()
.addBodyPart("application/json", json.toString())
P.S. it must be the "add", because request has also one static attachment.
If you want to add a json to your request as a multipart/form-data using groovy script testStep you can use the follow code:
def jsonStr = "{'id':'test','someValue':'3'}"
def testStep = context.testCase.testSteps["/job/result"]
// set the content for the request
testStep.getHttpRequest().setRequestContent(jsonStr)
// and set the media type
testStep.testRequest.setMediaType('application/json')
// if you want to send as a multipart/form-data then use follow line instead
// testStep.testRequest.setMediaType('multipart/form-data')
This results in a request configured as follows:
Hope it helps,
Have found a solution using https://github.com/jgritman/httpbuilder
def http = new HTTPBuilder(serviceEndPoint)
def scanResultFile = new File(testRunner.testCase.getPropertyValue("ScanResultFile"))
http.request( POST ){ req ->
headers.'Connection' = 'Keep-Alive'
headers.'User-Agent' = 'SoapUI 4.5.1'
requestContentType = 'multipart/form-data'
ByteArrayBody bin = new ByteArrayBody(scanResultFile.readBytes(), "application/octet-stream", "jobResult");
StringBody info = new StringBody(testRunner.testCase.getPropertyValue("JsonScanResult"), "application/json", java.nio.charset.StandardCharsets.UTF_8);
MultipartEntity entity = new MultipartEntity()
entity.addPart("info", info);
entity.addPart("jobResult", bin)
req.entity = entity
}
I would like to send a string (JSON formatted) to my webservice instead using a DTO.
var client = new JsonServiceClient(absoluteUrl);
client.Post<T>(absoluteUrl, data);
But, after to do change my data (DTO object) to a JSON string, I'm getting a ServiceStack Exception: Internal Server Error. Looks like the ServiceStack Post/Send method changes my JSON request.
Is it possible or is there any way to avoid it? Someone else had the same issue?
UPDATE 1: I'm using this approach because of the OAuth authentication. I'm generating a HMAC by request.
Thanks.
You can use HTTP Utils to send raw JSON, e.g:
var response = absoluteUrl.PostJsonToUrl(data)
.FromJson<T>();
ServiceStack's .NET Service Clients provide Typed API's to send Typed Request DTO's, it's not meant for POST'ing raw strings but I've just added support for sending raw string, byte[] and Stream in this commit so now you can send raw data with:
var requestPath = request.ToPostUrl();
string json = request.ToJson();
var response = client.Post<GetCustomerResponse>(requestPath, json);
byte[] bytes = json.ToUtf8Bytes();
response = client.Post<GetCustomerResponse>(requestPath, bytes);
Stream ms = new MemoryStream(bytes);
response = client.Post<GetCustomerResponse>(requestPath, ms);
This change is available from v4.0.43+ that's now available on MyGet.
Sharing Cookies with HttpWebRequest
To have different HttpWebRequests share the same "Session" you just need to share the clients Cookies, e.g. after authenticating with a JsonServiceClient you can share the cookies with HTTP Utils by assigning it's CookieContainer, e.g:
var response = absoluteUrl.PostJsonToUrl(data,
requestFilter: req => req.CookieContainer = client.CookieContainer)
.FromJson<T>();
I've followed instructions on how creating a ServiceStack here at:
https://github.com/ServiceStack/ServiceStack/wiki/Create-your-first-webservice
I'm sure I have followed it to the letter, but as soon as I run the web application. I get a 'Snapshot' view of my response. I understand this happens when I don't have a default view/webpage. I set up the project as a ASP.net website, not a ASP.net MVC website. Could that be the problem?
I also wrote a test console application with the following C# code. It got the response as a HTML webpage rather than as a plain string e.g. "Hello, John".
static void sendHello()
{
string contents = "john";
string url = "http://localhost:51450/hello/";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentLength = contents.Length;
request.ContentType = "application/x-www-form-urlencoded";
// SEND TO WEBSERVICE
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(contents);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
Console.WriteLine(result);
}
How can I switch off the 'snapshot' view? What am I doing wrong?
The browser is requesting html so ServiceStack is returning the html snapshot.
There are a couple of ways to stop the snapshot view:
First is to use the ServiceClient classes provided by servicestack. These also have the advantage of doing automatic routing and strongly typing the response DTOs.
Next way would be to set the Accept header of the request to something like application/json or application/xml which would serialize the response into json or xml respectively. This is what the ServiceClients do internally
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Accept = "application/json";
...
Another method would be to add a query string parameter called format and set it to json or xml
string url = "http://localhost:51450/hello/?format=json";
Putting the specific format requesting is the practical way to do this
string url = "http://localhost:51450/hello/?format=json";
I suggest simply deleting this feature.
public override void Configure(Container container)
{
//...
this.Plugins.RemoveAll(p => p is ServiceStack.Formats.HtmlFormat);
//...
}
Now all requests with the Content-Type=text/html will be ignored.