get a pagingobject in http response requested over curl - java

Hello people i have a question about how to get JSON pagingObject in my response with a http curl get (curl running on jokto-linux-system).
i wanted to get a special page over the parameter $offset=0 and $limit=0.
But i dont know where to put the parameter to get on the right page of the serviceressource. I get all the ressources back but i need to see how much pages are used also i didnt see this too.
So my basic problem is the visibility and changing of the parameter $offset and $limit of the pagingObject of an Service/Ressource with amount of Ressourceelements.
Thank you very much for evry answer
This my basic "GET" i try: curl -i http://127.0.0.1:80/service/ressource/Elementuri
Tried to get an the object over header: curl -X GET -H "$offset" -H "$limit" http://127.0.0.1:80/service/ressource/elementuri
I tried it to set after the uri: curl -X GET http://127.0.0.1:80/service/ressource/elementuri?$offset=0&$limit=1

Dollar sign is probably not necessary.
curl http://127.0.0.1:80/service/ressource/elementuri?limit=2&offset=2

Related

Can Jackson/Jax-rs have an optional data?

I am making a curl request to endpoint with following option curl --data {"foo" : "bar"} for POST request
However I want to implement a default behavior for missing data, ie curl without --data should also work for POST request.
Currently if data is missing it returns: "error":400,"errorCode":"INVALID_JSON". I do not want that.
Is it possible ?
Does it have any annotation to support optional data.?

Convert Python request with image to cURL

I found this example of a API request. Unfortunately I didn't find any other example how to upload an image to the API.
As I'm not familiar with Python I'm trying to understand how to do the same in a cURL command.
import requests
auth_headers = {
'app_id': 'your_app_id',
'app_key': 'your_app_key'
}
url = 'https://XXXXXXX'
files = {
'source': open('media/test.jpg')
}
data = {
'timeout': 60
}
response = requests.post(url, files=files, data=data, headers=auth_headers)
I tried to convert it by trying out a cURL to python converter, but I don't know how to build it with the files.
In the end I want to do the request in JAVA, but I think if I would know the request in cURL I can figure it out.
Hope anyone can help me with that.
This will do it:
#!/bin/bash
args=(
-H 'app_id: your_app_id'
-H 'app_key: your_app_key'
-F 'source=#/path/to/file'
-F 'timeout=60'
'http://httpbin.org/post'
)
curl "${args[#]}"
or, as a one-liner:
curl -H 'app_id: your_app_id' -H 'app_key: your_app_key' -F 'source=#/path/to/file' -F 'timeout=60' 'http://httpbin.org/post'
Use -H to specify header fields (repeat for every field) and -F to specify form fields - either as key=value pairs, or filename=#path pairs. When -F is used, POST method is the default, and Content-Type is multipart/form-data (but that too can be overridden).

Using curl to output http status code, total time, and the request body

I would like to use curl to capture the following parameters in a single POST request:
http status code
total time of the operation
body of the results
Based on the manual (http://curl.haxx.se/docs/manpage.html), here are the options I tried.
1) The -w parameter captures the http_code and the time_total.
curl "http://stackoverflow.com/" -w "\' %{http_code}\',\'%{time_total}\'"
But it appears it only returns these two parameters and I am no longer able to capture the body.
2) The -i parameter returns the header and the requested body but I will no longer be able to return the total time.
curl -i "http://localhost:8080/"
The end goal is place the three results into a JSON object which can then be parsed as needed.
{"httpVar: "200", "runtimeVar" : "18.58", "outputVar" : "ID:grinvriar080n23nvn"}
Currently, I'm using groovy script (similar to JAVA) so I don't think I could use bash or PHP. I could total time it took the groovy to run the .execute() but that won't be the true time it took to POST to send/return data. I'm even open to using another program besides curl.
I looked for a few days now with no results so any suggestions are appreciated.
What you tried should give, what you desired to get.
I mean the following:
curl "http://stackoverflow.com/" -w "\' %{http_code}\',\'%{time_total}\'"

How to send parameters in a GET call with restlet?

Confirmed the following in Curl:
This is working fine-
curl -X GET 'http://remoteUrl.com:8080/public/private/request/data?begin=12&end=20'
This is not working-
curl -X GET http://remoteUrl.com:8080/public/private/request/data?begin=12&end=20
So the whole purpose is to use the way first curl command in a java code with restlet to send the paramters in a GET request but unfortunately my code is not working.
Code:
ClientResource clientResource = new
ClientResource("http://remoteUrl.com:8080/public/private/request/
data?begin=12&end=20");
Representation clientText = clientResource.get();
Error Log:
INFO: Unable to read a header
java.io.IOException: Parameter or extension has no name. Please check your value
at org.restlet.engine.header.HeaderReader.readNamedValue(HeaderReader.java:499)
at org.restlet.engine.header.CacheDirectiveReader.readValue(CacheDirectiveReader.java:65)
at org.restlet.engine.header.CacheDirectiveReader.readValue(CacheDirectiveReader.java:38)
at org.restlet.engine.header.HeaderReader.addValues(HeaderReader.java:282)
at org.restlet.engine.header.CacheDirectiveReader.addValues(CacheDirectiveReader.java:50)
at org.restlet.engine.header.HeaderUtils.copyResponseTransportHeaders(HeaderUtils.java:776)
at org.restlet.engine.adapter.ClientAdapter.readResponseHeaders(ClientAdapter.java:129)
at org.restlet.engine.adapter.ClientAdapter.updateResponse(ClientAdapter.java:191)
at org.restlet.engine.adapter.ClientAdapter.commit(ClientAdapter.java:105)
at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:119)
at org.restlet.Client.handle(Client.java:153)
From the exception you give in your question, it seems that the value of the header Cache-Control isn't correct within the response. Restlet isn't able to parse it.
I guess that the value of this header is an empty string in the response...
Could you give us in your question the exact content of the response (mainly headers)?
Hope it helps you,
Thierry

RESTful web service in Java - 400 Bad request - Wrong method invocation? [duplicate]

This question already has answers here:
HTTP GET with request body
(23 answers)
Closed 7 years ago.
I don't understand why this curl invocation gives me a 400 bad request.
curl -v -XGET -H "Content-Type:application/json" -d '{"match":{"first":"james"}}' http://localhost:8080/geocon/search/
Considered that this is the code part who should handle everything
#Path(Paths.SEARCH)
public class SearchService {
#GET
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public String search(#DefaultValue(NO_TYPE) #QueryParam("type") String type, String query) {
// do interesting stuff with the query
System.out.println(type);
System.out.println(query);
return //the result of the query
}
Obviously, I don't see in the console the two print lines and the answer from the web service is 400 Bad request
If I change the verb in curl and in the code (POST instead of GET), the results are OK and everything works as it should.
Why?
Can't I use GET passing data? If you think about it, I am not modifying stuff, I'd just receive data so GET should be the most RESTful translation for it.
You are allowed to send a body with a GET request, but it must not have any semantics; it should essentially be ignored. Not doing so violates the HTTP/1.1 specification. I suggest avoiding this practice because no one expects a request body with a GET request.
Now I'm not sure if the MVC framework you're using is stricter in the sense that it automatically rejects any GET request sent with a body, because in general a server can accept a GET request with a body, but is just supposed to ignore it.
Hence, what you're doing is not RESTful. The only way you can pass in data through a GET is through query parameters.
UPDATE
This is in response to your comment.
You could expose an explicit resource called searchResult or search. To create a searchResult or search resource instance, you pass in a body that is essentially a query. If you are using semantic media types (as you should for proper RESTfulness), your search-result resource could have the media type application/vnd.myservice.search-result+json and the query can have the media type application/vnd.myservice.search-query+json. Then, you can POST a request to /searchResults that has a request body that contains the complex query. The media-type of the response would be application/vnd.myservice.search-result+json and it can contain the search results.
So how does this play out? A search query comes in and you parse out the body and run the query. Then you should persist the results (not for ever; give it some sane TTL value). Once you have done that, you return a 303 See Other with a Location header that has a link to the search results (maybe something like /searchResults/4334, where 4334 is the id of this particular result). The client can then access this URI to retrieve the search results. If the client requests a search result that has expired (i.e., the server cleaned it up because the TTL expired), then the server should respond with a 410 Gone.
Also read this for more pointers.
It seems that syntax you are using has a typo.Try using the below ones for json and xml format. -XGET is wrong. it should be -X GET.
For xml
curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource
For JSON
curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

Categories

Resources