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"
Related
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).
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.
I try to communicate between javascript and java. My script javascript send a message to java and java send a response.
javascript part:
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4)
{
alert(xmlhttp.responseText);
}
}
var s = "LIGNE \n 2 \n il fait beau \nEND\n";
xmlhttp.open("POST","http://localhost:6020",true);
xmlhttp.send(s);
java part:
try {
serverSocket = new ServerSocket(6020);
} catch (IOException e) {
System.err.println("Could not listen on port: 6020.");
System.exit(-1);
}
serverSocket.accept()
BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
String ligne = "";
while(!(ligne = plec.readLine()).equals("END")){
System.out.println(ligne);
}
bw.write("Il fait beau\n");
bw.flush();
bw.close();
plec.close();
socket.close();
output java :
POST / HTTP/1.1
Host: localhost:6020
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost:8080/test.html
Content-Length: 30
Content-Type: text/plain; charset=UTF-8
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache
LIGNE
2
il fait beau
So, I receive correctly the message send by javascript but the alert his always empty. How to response at this message?
I try a lot of possiblity but they don't work. And I don't want to use the servlet, it's to heavy to do that.
Thanks.
Edit:
I did this :
bw.write("HTTP/1.1 200 OK\r\n"+
"Content-Type: text/html; charset=utf-8\r\n"+
"Content-Length: 13\r\n\r\n" +
"il fait beau\n");
and this:
String data = "il fait beau \n";
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK\r\n");
builder.append("Content-Type: text/html; charset=utf-8\r\n");
builder.append("Content-Length:" + data.length() + "\r\n\r\n");
builder.append(data);
bw.write(builder.toString());
But the alert remain empty. Maybe it's a problem in the javascript.
The javascript needs to see a full HTTP response. Merely sending back characters to it, makes it discard the reply as it is an invalid HTTP response.
In your java code, send back something like this
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: <length of data>
---data here---
Reference
Something like:
StringBuilder builder = new StringBuilder();
builder.append("HTTP/1.1 200 OK\r\n");
builder.append("Content-Type: text/plain; charset=utf-8\r\n");
builder.append("Content-Length:" + data.length() + "\r\n\r\n);
builder.append(data);
bw.write(builder.toString());
Try:
bw.write("HTTP/1.1 200 OK\r\n"+
"Content-Type: text/html; charset=utf-8\r\n"+
"Content-Length: 13\r\n\r\n" +
"il fait beau\n");
HTTP-Headers are separated by \r\n (CRLF). Headers and body is spearated by \r\n\r\n.
Note that you set the length to 13 because you also have to count the \n at the end of your string.
EDIT: It does not work because of the cross-domain-policy. http://localhost:6020 is not the same port as the website which executes your JavaScript and so the xmlhttprequest might not be delivered.
I am trying to send a GET request to a REST server using Java.
This is the code with some debugging.
URL url = new URL(request);
System.out.println("request url: " + url.toString());
System.out.println("method: " + httpMethod);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod(httpMethod);
connection.setRequestProperty("Content-Type", "text/plain");
connection.setRequestProperty("charset", "utf-8");
OutputStream os = connection.getOutputStream();
os.flush();
String response = os.toString();
System.out.println("response: " + response);
if (response.length() == 0)
{
throw new MyException("the response is empty");
}
This is the output:
request url: http://www.example.com/api.php/getToken/?api_ver=1&token=&api_key=bf8de053d9b6c540fb12195b4ac1602b0a71788c&sig=e00a59747afc7232207d40087e3765a5
method: GET
response:
com.example.api.client.MyException: the response is empty
As you can see, the response is empty.
But if I try and copy and paste the URL in Firefox I get this output
{"error":220}
and this header:
HTTP/1.1 200 OK
Date: Mon, 15 Nov 2010 11:55:29 GMT
Server: Apache
Cache-Control: max-age=0
Expires: Mon, 15 Nov 2010 11:55:29 GMT
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 33
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Can you see what it is wrong? How could I debug this code further?
Any help would be very much appreciated.
I think you do not use HttpURLConnection properly (there is no connect()).
Maybe study this example.
I found a URL that httpclient doesn't seem to be handling redirects on:
http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml
should 302 to:
http://www.mtv.com/news/articles/1647243/20100907/story.jhtml
when I look at the headers in the browser everything looks good:
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html; charset=UTF-8
Location: http://www.mtv.com/news/articles/1647243/20100907/story.jhtml
Content-Length: 258
Date: Wed, 08 Sep 2010 18:40:21 GMT
Expires: Wed, 08 Sep 2010 18:40:21 GMT
Cache-Control: private, max-age=0
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Xss-Protection: 1; mode=block
Server: GSE
Set-Cookie: PREF=ID=024209255b405b06:TM=1283971221:LM=1283971221:S=AG-13_7Cjg_EqlRY; expires=Fri, 07-Sep-2012 18:40:21 GMT; path=/; domain=.google.com
Connection: close
However httpclient doesn't seem to give me the final URL. Here is the code I was using
HttpHead httpget = null;
HttpHost target = null;
HttpUriRequest req = null;
String startURL = "http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGrJk-F7Dmshmtze2yhifxRsv8sRg&url=http://www.mtv.com/news/articles/1647243/20100907/story.jhtml";
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE,HttpClientFetcher.emptyCookieStore);
httpget = new HttpHead(startURL);
HttpResponse response = httpClient.execute(httpget, localContext);
Header[] test = response.getAllHeaders();
for(Header h: test) {
logger.info(h.getName()+ ": "+h.getValue());
}
target = (HttpHost) localContext.getAttribute( ExecutionContext.HTTP_TARGET_HOST );
req = (HttpUriRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST );
// STILL PRINTS OUT THE GOOGLE NEWS LINK
finalURL = target+""+req.getURI();
Am I doing something wrong? thanks
Found the answer from the httpclient mailing list...
Google doesn't treat a HEAD and GET the same, so the GET redirects with 302 and the HEAD request gives a 200 OK