I'm trying to send a request, I do the following in curl:
curl -v --header "location: 60.004:8.456" --cookie "sessionToken=~session" -i -X PUT -H 'Content-Type: application/json' -d '{"data":"{"FCT":"Welcome", "Uni":"Welcome to DI"}"}' localhost:8080/tester/apps/e39/data
and for some reason it matches the class but no this method:
#PUT
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response createDocumentRoot(JSONObject inputJsonObj,
#Context UriInfo ui, #Context HttpHeaders hh) {
}
Edit:
The class is defined with #Path("{appId}/data")
The problem isn't the paths, as I've debugged it and seen it identifies the class right, it just throws the bad request after going inside the class without entering any method.
Here is the curl verbose:
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1... connected
> PUT /tester/apps/e39/data HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8080
> Accept: */*
> Cookie: sessionToken=~session
> location: 60.004:8.456
> Content-Type: application/json
> Content-Length: 60
>
* upload completely sent off: 60out of 60 bytes
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
Content-Type: text/html;charset=utf-8
< Content-Length: 990
Content-Length: 990
< Date: Tue, 02 Jul 2013 21:46:56 GMT
Date: Tue, 02 Jul 2013 21:46:56 GMT
< Connection: close
Connection: close
The problem was in the json syntax that was incorrect, I had
'{"data":"{"FCT":"Welcome", "Uni":"Welcome to DI"}"}'
after changing to this, it worked fine:
'{"data":
{"FCT":"Welcome",
"Uni":"Welcome to DI"}}'
I used a JSON online parser to check the json syntax, here is the link incase someone needs it:
http://json.parser.online.fr/
Related
To be more specific, I mean specifically to just consume the HTTP headers over the network and stop the communication before the client receives the response body.
Example
Client makes a request
GET / HTTP/1.1
Host: example.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (Java/1.8.0_262)
Accept-Encoding: gzip,deflate
Then the response over the network is just
HTTP/1.1 200 OK
Date: Wed, 23 Sep 2020 22:41:21 GMT
Server: Apache
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Cache-Control: public, max-age=10800
Content-Language: en
Vary: Cookie,Accept-Encoding
Content-Encoding: gzip
Content-Type: text/html; charset=utf-8
Age: 1865
grace: none
Content-Length: 9213
Connection: keep-alive
Accept-Ranges: bytes
Http protocol has six method, one of the methods is 'HEAD'. You can try use HEAD method instead of GET method.
And another stupid way : declare a web interface, and return null string.Like this:
// a web interface
String result = "";
return result;
I am building a basic Spring Boot app, using the built is resource handler.
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/public/")
.setCachePeriod(60 * 60 * 24 * 365)
.resourceChain(true)
.addResolver(versionResourceResolver);
The file structure is as follows:
src
main
java
..
resources
public
js
app.js
Making a request to localhost:8080/js/app.js works as expected, returning the contents of the file with the correct MIME type.
However, making a request to the container directory also returns 200 OK and an empty response!
$ curl -v http://localhost:8080/js/
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /js/ HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.49.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Mon, 28 Nov 2016 21:13:26 GMT
< X-Application-Context: application
< Last-Modified: Mon, 28 Nov 2016 21:06:28 GMT
< Cache-Control: max-age=31536000
< Accept-Ranges: bytes
< Content-Type: application/octet-stream
< Content-Length: 0
<
* Connection #0 to host localhost left intact
How can I get this to return 404 Not Found instead? It's not a huge issue, but definitely not what I'd expected.
In my pet project I have a simple jersey (v 2.13) resource that performs some crud operations, and the input is validated by hibernate-validator (v 5.0.1.Final).
The problem is that while the junit tests works as expected, when I deploy the webapp all the ExceptionMapper implementations are ignored.
Here some details:
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public JsonResult<User> addUser(#Valid Userbean user) {
//do stuff...
}
The validation is made by hibernate-validator.
the Userbean is annotate with:
#NotNull
private String password;
I've created an ExceptionMapper implementation:
#Provider
public class ValidationExceptionMapper implements ExceptionMapper<ValidationException> {
#Override
public Response toResponse(ValidationException e) {
return Response.status(422).build();
}
}
Leaving aside that on validation the proper status code should be 400, my problem is that while in my junit-test everything goes fine...
Userbean entity = new Userbean();
entity.setUsername(username);
Response response = target.path("users").request(MediaType.APPLICATION_JSON).post(Entity.json(entity));
int status = response.getStatus();
assertEquals(422 status);
...I cannot reproduce the same behaviour when I run it on tomcat (I've tried tomcat7 / tomcat 8 and also glassfish 4.1)
I always get a 400 status code instead of 422 and ValidationExceptionMapper is being skipped.
$ curl -v -X POST -H "Content-Type: application/json" --data "{\"username\":\"MrFoo\"}" http://localhost:8080/playground-webapp/jaxrs/jersey/users/
* About to connect() to localhost port 8080 (#0)
* Trying ::1... connected
> POST /playground-webapp/jaxrs/jersey/users/ HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8080
> Accept: */*
> Content-Type: application/json
> Content-Length: 20
>
* upload completely sent off: 20out of 20 bytes
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< Content-Type: text/html;charset=utf-8
< Content-Language: en
< Content-Length: 990
< Date: Sun, 28 Dec 2014 15:07:34 GMT
< Connection: close
<
* Closing connection #0
<html><head><title>Apache Tomcat/7.0.52 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - Bad Request</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>Bad Request</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.52</h3></body></html>
The same call made on the test server works as expected:
curl -v -X POST -H "Content-Type: application/json" --data "{\"username\":\"MrFoo\"}" http://localhost:8080/users/
* About to connect() to localhost port 8080 (#0)
* Trying ::1... Connessione rifiutata
* Trying 127.0.0.1... connected
> POST /users/ HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: localhost:8080
> Accept: */*
> Content-Type: application/json
> Content-Length: 20
>
* upload completely sent off: 20out of 20 bytes
< HTTP/1.1 422
< Content-Type: application/json
< Date: Sun, 28 Dec 2014 15:05:35 GMT
< Content-Length: 94
<
* Connection #0 to host localhost left intact
the code of the server I use for junit is:
#Before
public void setUpClient() throws Exception {
ResourceConfig rc = new UserResourceConfig();
rc.property("contextConfig", appContext);
server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc);
server.start();
....
Thanks in advance for your help.
I'm trying to upload a presentation, using the google api drive, but the presentation goes incomplete some slides are blank, watching the logs this is what shows
com.google.api.client.http.HttpRequest execute: -------------- REQUEST --------------
PUT https://www.googleapis.com/upload/drive/v2/files? uploadType=resumable&upload_id=AEnB2UrDPJgLIehcqH--aWgwl- R_atDhqdvbXnJiWMXKE0V0euJGOvULbM4y5YmvUePWaHSrYyFdOgsmTASJGe-Dtvg09NCkzQ
Accept-Encoding: gzip
Authorization: Bearer ya29.AHES6ZTfSaK77NGCcZO1bK_aTbT8zVX3eslOAb8BkrvpeXARK94XsXY
Content-Range: bytes 0-1048575/6774302
User-Agent: Google-HTTP-Java-Client/1.11.0-beta (gzip)
Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation
Content-Length: 1048576
com.google.api.client.http.HttpRequest execute: curl -v --compressed -X PUT -H 'Accept- Encoding: gzip' -H 'Authorization: Bearer ya29.AHES6ZTfSaK77NGCcZO1bK_aTbT8zVX3eslOAb8BkrvpeXARK94XsXY' -H 'Content-Range: bytes 0- 1048575/6774302' -H 'User-Agent: Google-HTTP-Java-Client/1.11.0-beta (gzip)' -H 'Content-Type: application/vnd.openxmlformats-officedocument.presentationml.presentation' -d '#-' -- https://www.googleapis.com/upload/drive/v2/files? uploadType=resumable&upload_id=AEnB2UrDPJgLIehcqH--aWgwl- R_atDhqdvbXnJiWMXKE0V0euJGOvULbM4y5YmvUePWaHSrYyFdOgsmTASJGe-Dtvg09NCkzQ << $$$
com.google.api.client.http.HttpResponse <init>: -------------- RESPONSE --------------
308 OK
server: HTTP Upload Server Built on Oct 3 2012 16:52:30 (1349308350)
range: bytes=0-1048575
x-range-md5: 19230c1c1a0fc493f3431f46cf30c14c
date: Mon, 05 Nov 2012 22:00:33 GMT
pragma: no-cache
expires: Fri, 01 Jan 1990 00:00:00 GMT
cache-control: no-cache, no-store, must-revalidate
content-length: 0
content-type: text/html; charset=UTF-8
x-google-cache-control: remote-fetch
via: HTTP/1.1 GWA
and this is the code that am using:
byte[] myFile = readImageData(blobKey,size);
ByteArrayContent mediaContent = new ByteArrayContent(mimeType, myFile);
Drive.Files.Insert insert = service.files().insert(body, mediaContent);
insert.getMediaHttpUploader().setChunkSize(1024 * 1024);
File file = insert.execute();
UPDATE
doing some testing, it seems the error only occurs when upload presentations with extension .pptx
is there any problem with the mimeType of that extension?
I hope someone can help me, thx in advance.
It seems like the resumable upload protocol is not being implemented correctly. The 308 response code indicates that only a chunk of the request has been uploaded and you should proceed with the next chunk.
Does it work correctly if you remove the following line from your code?
insert.getMediaHttpUploader().setChunkSize(1024 * 1024);
Below is the url using in my code
URL url = new URL("https://8.7.177.4/ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001");
but iam getting exception as
java.io.FileNotFoundException: https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at AuthenticateCurl.authenticatenewPostUrl(AuthenticateCurl.java:311)
at AuthenticateCurl.main(AuthenticateCurl.java:341)
in the exception we can find url where /?object=answerrule is getting appended before starting query string.
How can i resolve this.
When you access the url without the extra '/', the web server forwards you to the version that has the extra '/'. You can see this when attempting to curl the URL at the command line:
$ curl --insecure -v 'https://8.7.177.4/ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001'
> GET /ns-api?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 8.7.177.4
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Date: Thu, 12 Jul 2012 18:45:28 GMT
< Server: Apache/2.2.11 (Fedora)
< Location: https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001
< Content-Length: 392
< Connection: close
< Content-Type: text/html; charset=iso-8859-1
<
This is fine, and HttpURLConnection automatically follows the redirect to the new URL. This is normal behaviour.
Following the new URL, we get a different result:
$ curl --insecure -v 'https://8.7.177.4/ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001'
> GET /ns-api/?object=answerrule&action=read&domain=amj.nms.mixnetworks.net&user=9001 HTTP/1.1
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5
> Host: 8.7.177.4
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Date: Thu, 12 Jul 2012 18:46:46 GMT
< Server: Apache/2.2.11 (Fedora)
< X-Powered-By: PHP/5.2.9
< Content-Length: 0
< Connection: close
< Content-Type: text/html; charset=UTF-8
<
...And we get a 404, which is why you get a FileNotFoundException!
If you were not expecting a redirect and you are running the server as well, maybe there is a configuration problem on the server.