When I click a button on webpage I get following information in developer tools:
Request URL:https://example.com/path/eventAction.do
Request Method:POST
Status Code:200 OK
and Request headers like below:
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.9
Connection:keep-alive
Content-Length:1600
Content-Type:multipart/form-data; boundary=----
WebKitFormBoundaryZ9ymd7fGOwOFpELi
Cookie:JSESSIONID=C1rX...
and Request payload like:
------WebKitFormBoundaryZ9ymd7fGOwOFpELi
Content-Disposition: form-data; name="example1"
FILTER_900000
------WebKitFormBoundaryZ9ymd7fGOwOFpELi
Content-Disposition: form-data; name="example2"
------WebKitFormBoundaryZ9ymd7fGOwOFpELi
Content-Disposition: form-data; name="example3"
08/03/2018
How can I send this http post with Apache HttpClient or is there any other method to imitate button click event with java code..
Payload parameter can be submitted with HttpPost request by preparing a JSON object as string and calling HttpPost's setEntity method.
payload = "{" +"\"fsName\": \"Peel\", " + "\"name\": \"John\", " + "\"path\": \"/" + accountName + "/invoice/" + sdf2.format(todayDate.toDate()) + "\", " + "}";
strEntity = new StringEntity(payload);
HttpPost().setEntity(strPayload).
Related
I need consume a service, the file is a excel.
But when I execute the consume the response is this "returned a response status of 400 Bad Request"
String authString = name + ":" + password;
Client restClient = Client.create();
String authStringEnc = Base64.getEncoder().encodeToString(authString.getBytes());
// the file to upload, represented as FileDataBodyPart
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File(file),
MediaType.APPLICATION_OCTET_STREAM_TYPE);
// fileDataBodyPart.setContentDisposition(FormDataContentDisposition.name("file").fileName(file).build());
FormDataMultiPart multiPart = new FormDataMultiPart();
multiPart.field("spId", idServicio, MediaType.MULTIPART_FORM_DATA_TYPE).bodyPart(fileDataBodyPart);
multiPart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
WebResource webResource = restClient.resource(url);
ClientResponse resp = webResource.header("Authorization", "Basic " + authStringEnc)
.header("Content-Type", "multipart/form-data").post(ClientResponse.class, multiPart);
String output = resp.getEntity(String.class);
System.out.print(output);
return resp;
I put a proxy and I use the Rest client "insomnia" and it works for me by insomnia.
This is the request that sent it:
INSOMNIA
POST /conf/configuration/distribution-files/service HTTP/1.1
Authorization: Basic aW1wbGluZWE6SU1QTElORUE=
User-Agent: insomnia/7.0.6
Content-Type: multipart/form-data; boundary=X-INSOMNIA-BOUNDARY
Accept: */*
Content-Length: 5872
Connection: close
--X-INSOMNIA-BOUNDARY
Content-Disposition: form-data; name="file"; filename="myspprueba.xls"
Content-Type: application/vnd.ms-excel
--X-INSOMNIA-BOUNDARY
Content-Disposition: form-data; name="spId"
5823
This is the request that at the moment i am failing
POST /conf/configuration/distribution-files/service HTTP/1.1
Authorization: Basic aW1wbGluZWE6SU1QTElORUE=
Accept: */*
Content-Type: multipart/form-data; boundary=Boundary_1_2104028992_1577117786190
MIME-Version: 1.0
User-Agent: Java/1.8.0_211
Connection: close
Content-Length: 5994
--Boundary_1_2104028992_1577117786190
Content-Type: text/plain
Content-Disposition: form-data; filename="myspprueba.xls"; modification-date="Thu, 19 Dec 2019 15:52:46 GMT"; size=5632; name="file"
I'm trying to configure and send a multipart request like the following one:
------boundary
Content-Disposition: form-data; name="before"; filename="blob"
Content-Type: application/vnd...+json;type=some_type
{some JSON}
------boundary
Content-Disposition: form-data; name="after"; filename="blob"
Content-Type: application/vnd...+json;type=some_type
{another JSON}
------boundary--
So I tried to configure a request, as in the code below
RestAssuredConfig config = RestAssured.config().multiPartConfig(
new MultiPartConfig().defaultCharset(StandardCharsets.UTF_8).
defaultBoundary("--boundary--"));
MultiPartSpecification m1 new MultiPartSpecBuilder(
new ObjectMapper().writeValueAsString(some_JSON_transformed_to_HashMap)).
fileName("blob").controlName("before").
mimeType(ContentType.TEXT.getAcceptHeader()).
header("ContentType", "application/vnd...+json;type=some_type").build();
MultiPartSpecification m2 = new MultiPartSpecBuilder(
new ObjectMapper().writeValueAsString(another_JSON_transformed_to_HashMap)).
fileName("blob").controlName("after").
mimeType(ContentType.TEXT.getAcceptHeader()).
header("ContentType", "application/vnd...+json;type=some_type").build();
RequestSpecification request = RestAssured.given().multiPart(m1).multiPart(m2).
config(config).
.header("Content-Type", "multipart/form-data; boundary=" + config.getMultiPartConfig().defaultBoundary());
request.post("some_url");
But when I try to execute it, server says that resource is invalid, but I believe, that JSONs is correct, so I suppose my multipart configuration is incorrect. How should I configure a request?
You could try something similar to below code .
given().auth().preemptive()
.basic("Jirausername", "Jirapassword")
.header("X-Atlassian-Token", "nocheck")
.multiPart(new File("/home/users/cat.log"))
.when().post("http://localhost:8181/rest/api/2/issue/STS-223/attachments");
I have a java server using sockets. I have an html file which contains a form that can be used to upload a file and send it. How can the server download that file without blocking. The browser never closes the connection and it just hangs in there I have beeping using readLine() from the input stream but it blocks. is there any way around this?
I appreciate any help here.
Thanks
please clarify your requirement, it seems total chaos in your steps.
did your program run as client side or server side?
could you show your code? and show what blocked you?
It is much better if you can draw a working-flow of your program .
Thanks for your supplement, do you want to upload a files through HTTP(writing in socket way). For this requirement, you could check the link link for how HTTP works
[RFC 1867] (https://www.rfc-editor.org/rfc/rfc1867)
Below is the package of how socket way do:
Suppose the server supplies the following HTML:
<FORM ACTION="http://server.dom/cgi/handle"
ENCTYPE="multipart/form-data"
METHOD=POST>
What is your name? <INPUT TYPE=TEXT NAME=submitter>
What files are you sending? <INPUT TYPE=FILE NAME=pics>
</FORM>
and the user types "Joe Blow" in the name field, and selects a text
file "file1.txt" for the answer to 'What files are you sending?'
The client might send back the following data:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain
... contents of file1.txt ...
--AaB03x--
If the user also indicated an image file "file2.gif" for the answer
to 'What files are you sending?', the client might client might send
back the following data:
Content-type: multipart/form-data, boundary=AaB03x
--AaB03x
content-disposition: form-data; name="field1"
Joe Blow
--AaB03x
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y
--BbC04y
Content-disposition: attachment; filename="file1.txt"
Nebel & Masinter Experimental [Page 9]
RFC 1867 Form-based File Upload in HTML November 1995
Content-Type: text/plain
... contents of file1.txt ...
--BbC04y
Content-disposition: attachment; filename="file2.gif"
Content-type: image/gif
Content-Transfer-Encoding: binary
...contents of file2.gif...
--BbC04y--
--AaB03x--
I have server running on port. I want it to gather the data sent by a POST request which carries a file (Not just text but png image).
for example here is a server:
public void run() {
try {
InputStream is = insocket.getInputStream();
PrintWriter out = new PrintWriter(insocket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line;
line = in.readLine();
String request_method = line;
System.out.println("HTTP-HEADER: " + line);
line = "";
// looks for post data
int postDataI = -1;
while ((line = in.readLine()) != null && (line.length() != 0)) {
System.out.println("HTTP-HEADER: " + line);
if (line.indexOf("Content-Length:") > -1) {
postDataI = new Integer(
line.substring(
line.indexOf("Content-Length:") + 16,
line.length())).intValue();
}
}
String postData = "";
// read the post data
if (postDataI > 0) {
char[] charArray = new char[postDataI];
in.read(charArray, 0, postDataI);
postData = new String(charArray);
}
out.println("HTTP/1.0 200 OK");
out.println("Content-Type: text/html; charset=utf-8");
out.println("Server: MINISERVER");
// this blank line signals the end of the headers
out.println("");
// Send the HTML page
out.println("<H1>Welcome to the Mini Server</H1>");
out.println("<H2>Request Method->" + request_method + "</H2>");
out.println("<H2>Post->" + postData + "</H2>");
out.println("<form name=\"input\" action=\"form_submited\" method=\"post\">");
out.println("Username: <input type=\"text\" name=\"user\"><input type=\"submit\" value=\"Submit\"></form>");
out.close();
insocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
How can it be changed to safe a file (Uploaded through HTTP/1.1 POST REQUEST) form) to disk using this server.
Thank you for your time
i have problems with my http-headers.
I need to send a post-request with an empty body and the header "Content-Length:0". But this seems impossible, becauce i get this exception:
--org.apache.http.ProtocolException: Content-Length header already present--
But when i do not include the header, there is no Content-Length header in the request and therefore i get 400: Bad Request.
I build this in Python too and there it works fine, but i can not make it work in java. Can you help me, please?
Here is my Code:
HttpClient client = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(header..);
httpPost.addHeader(another header);
httpPost.addHeader("Content-Type","application/xml");
httpPost.addHeader("Content-Length","0");
httpPost.addHeader(another header);
HttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine().getStatusCode());
EDIT:
Solved the problem with the answer from #Beno Arakelyan.
I tried to send the Content-Length with http-request which built on apache http client. It works fine. Example:
private static final HttpRequest<?> HTTP_REQUEST =
HttpRequestBuilder.createGet(YOUR_URI)
.addDefaultHeader(HttpHeaders.CONTENT_LENGTH, "0")
.build();
public void test(){
System.out.println(HTTP_REQUEST.execute().getStatisCode()); // is 200
}
You don't need to calculate the content length, the client will handle that for you. Try removing the header and set a body instead, for example: httpPost.setEntity(new StringEntity("")).
This Link directs you to the example below
URL url = new URL("https://reqbin.com/echo/post/json");
HttpURLConnection http = (HttpURLConnection)url.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setRequestProperty("Content-Type", "application/json");
String data = "{\"widget\": {\n \"window\": {\n \"title\": \"Sample Widget\",\n \"name\": \"sample_widget\",\n \"width\": 600,\n \"height\": 400\n },\n \"image\": { \n \"src\": \"images/test.png\",\n \"name\": \"sample_test\",\n \"hOffset\": 150,\n \"vOffset\": 150,\n \"alignment\": \"center\"\n },\n \"text\": {\n \"data\": \"Click Here\",\n \"size\": 36,\n \"style\": \"bold\",\n \"name\": \"sample_click\",\n \"alignment\": \"center\"\n }\n}} ";
byte[] out = data.getBytes(StandardCharsets.UTF_8);
OutputStream stream = http.getOutputStream();
stream.write(out);
System.out.println(http.getResponseCode() + " " + http.getResponseMessage());
http.disconnect();
Note: Content-Length will be added automatically.
I'm trying to upload an image to the PushBullet API with retrofit.
After the upload-request I fire the multipart upload.
With retrofit I get this error:
{"error":{"code":"invalid_request","type":"invalid_request","message":"Invalid multipart body.","cat":"o(^・x・^)o"},"error_code":"invalid_request"}
The problem only occurs in my java code and not with the PAW HTTP-Client.
# PAW generated Request
POST /upload-legacy/bcSWXnBjNIwpkej7CxfIHFz0ugXO6yhf HTTP/1.1
Content-Type: multipart/form-data; charset=utf-8; boundary=__X_PAW_BOUNDARY__
Host: upload.pushbullet.com
Connection: close
User-Agent: Paw/3.0.12 (Macintosh; OS X/10.11.6) GCDHTTPRequest
Content-Length: 34508
--__X_PAW_BOUNDARY__
Content-Disposition: form-data; name="file"; filename="cat.jpg"
Content-Type: image/jpeg
...
# Retrofit generated Request
POST https://upload.pushbullet.com/upload-legacy/ZZ4fLcqt2WFQmlbKTDlgcYXtB3KiCs3M http/1.1
Content-Type: multipart/form-data; charset=utf-8
Content-Length: 2012
Content-Disposition: form-data; name="file"; filename="1475501429665_motion_detected.jpg"
Content-Type: image/jpeg; charset=utf-8
Content-Length: 1772
...
The important difference I think is the Content-Length in the Part.
I found this issue, but that would mean the PushBullet API is non-compliant with the HTTP specification?
Any help would be appreciated.
I was experiencing this same issue in Google Apps Script, which is JavaScript based, but I'm hoping my solution could help anyone else experiencing this issue. I used TANAIKE's method of building the multipart request here: https://gist.github.com/tanaikech/d595d30a592979bbf0c692d1193d260c
My successful end result looked like this for successfully uploading a JPEG:
// https://docs.pushbullet.com/v8/#upload-request
// Assuming var picResponseJSON is your JSON results from successful upload-request
var uploadJSON = {
awsaccesskeyid: picResponseJSON.data.awsaccesskeyid,
acl: picResponseJSON.data.acl,
key: picResponseJSON.data.key,
signature: picResponseJSON.data.signature,
policy: picResponseJSON.data.policy,
"content-type": picResponseJSON.data["content-type"],
};
// https://gist.github.com/tanaikech/d595d30a592979bbf0c692d1193d260c
var boundary = "xxxxxxxxxx";
var data = "";
for (var i in uploadJSON) {
data += "--" + boundary + "\r\n";
data +=
'Content-Disposition: form-data; name="' +
i +
'"; \r\n\r\n' +
uploadJSON[i] +
"\r\n";
}
data += "--" + boundary + "\r\n";
data +=
'Content-Disposition: form-data; name="file"; filename="' +
fileTitle +
'"\r\n';
data += "Content-Type:" + mimeType + "\r\n\r\n";
var payload = Utilities.newBlob(data)
.getBytes()
.concat(DriveApp.getFileById(fileID).getBlob().getBytes())
.concat(Utilities.newBlob("\r\n--" + boundary + "--").getBytes());
var options3 = {
method: "post",
contentType: "multipart/form-data; boundary=" + boundary,
payload: payload,
muteHttpExceptions: true,
};
// Send request
var uploadResponse = UrlFetchApp.fetch(picResponseJSON.upload_url, options3);
// Confirm it's successful
if (uploadResponse.getResponseCode() == 204) {
console.log("Success! File: " + picResponseJSON.file_url);
}
Please note the Blob functions in the payload are part of Google Apps Script so modify accordingly for your language.