Resteasy #QueryParam parsing - java

I have a rest endpoint as below. pid is UUID which I'm parsing using UUID.fromString(pid);
#GET
#Path("/")
public Response process(#Context HttpServletRequest req,
#QueryParam("p") String pid,
#DefaultValue("3") #QueryParam("a") String active,
#DefaultValue("3") #QueryParam("c") String closed,
#CookieParam("X") String cookie) {
//my stuff
}
This is not setting 'p' and 'X' when I run jetty and curl using
curl localhost:9090/rest/accounts?p=<uuid>&c=4&a=5 -b "X=1212;"
response is -b: command not found
it works when I move cookie part to front
curl -b "X=1212;" localhost:9090/rest/accounts?p=&c=4&a=5
but 'c' and 'a' are always 3 (default).
Is there something wrong with the way I'm using it.

Your shell probably interpreted &, try:
curl -b "X=1212;" "localhost:9090/rest/accounts?p=&c=4&a=5"
If you want to see detail information about request and response use:
curl -v -b "X=1212;" "localhost:9090/rest/accounts?p=&c=4&a=5"

Related

how can i call web services with curl with json param?

I'm programming in Java.
I developed a test web service in which I pass a random json and the service returns me hello
Here the web service code:
#Path("test")
#GET
#Consumes(MediaType.APPLICATION_JSON)
public String test(#HeaderParam("token") String token, #QueryParam("array")String array)
{
return "hello";
}
I call the service with curl
curl -v -H 'token:aaaa' 'http://140.145.1.2/clayapi/restservices/test?array=[{"name":"george","lastname":"ronney"}]';
message of error:
curl: (3) [globbing] illegal character in range specification at pos 60
I tried to add -g but it doesn't work .. how should I do?
Use -G along with --data-urlencode:
curl -v -G 'http://example.org' \
-H 'header:value' \
--data-urlencode 'array=[{"name":"george","lastname":"ronney"}]'
From the documentation:
-G, --get
When used, this option will make all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a ? separator. [...]
--data-urlencode <data>
(HTTP) This posts data, similar to the other -d, --data options with the exception that this performs URL-encoding. [...]

To accept -d #json , How put method should be written in rest api

In PUT rest call , Using curl I would like to use -d option , what should be method signature . I am using java.
curl -X PUT -u testUser 'http://hostname:port/method' -d #test.json -H "Content-Type: application/json"
This should be my curl when I fire .
I have written a method signature which accepts string as input
#PUT #Path ("/test")
#Produces("application/json")
public Response method(String input) throws Exception {...}
When I run above command , Having above method signature is throwing error .
Error processing command: java.lang.NullPointerException
String input needs data in json format .
{
"name" : "test",
"host" : "10.xx.xx.xx",
"port" : 22
.
.
} //around 15 values
having these in a single url like this
"http://hostname:port/method?name=test&host=10.xx.xx.xx&port=22........."
makes url grow pretty long but its working though.
I would like to use -d option to send json input through a file.
What should be the method signature for curl to work with -d #test.json in java ?
--Thanks in Advance.

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).

#Requestparam: Can not get the 2nd inputted request parameters

I'm trying to create simple REST request handler like this:
#RequestMapping(value = "test", method = RequestMethod.GET)
public String test(#RequestParam(name = "test", required = false) String test,
#RequestParam(name ="number", required = false) Long number) {
return new StringBuilder(test).append(" ").append(number).toString();
}
But when I test this by CURL:
curl http://localhost:8080/test?test=abc&number=2016
The output become like this:
abc null
What's wrong here? Is this my mistake or Spring bug?
I'm using Spring Boot 1.4.0 (latest version may be).
curl version is 7.43.0 (64 bit).
Hopefully this is the solution and not a false assumption:
If you really type:
curl http://localhost:8080/test?test=abc&number=2016
Your curl process
curl http://localhost:8080/test?test=abc
will be executed and send in the background because of the &.
Try:
curl 'http://localhost:8080/test?test=abc&number=2016'.
Used curl from cmder on Windows 8.1. Got same issue with multiple Spring #RequestParam annotations. Wrapping url in single quotes did't fix it for me, but double quotes worked as expected.
No quotes approach:
curl http://localhost:8080/jobs/test%20name/find?jobTime=jobtime&jobDate=jobdate
Will produce 400 Bad request error, because jobDate parameter marked as mandatory in Spring controller.
Single quotes approach:
curl 'http://localhost:8080/jobs/test%20name/find?jobTime=jobtime&jobDate=jobdate'
Will produce 'jobDate' is not recognized as an internal or external command, operable program or batch file. error
Double quotes will behave as expected:
curl "http://localhost:8080/jobs/test%20name/find?jobDate=jobdate&jobTime=jobtime"
Produced correct output from controller class.
Same exact GET request working fine without any quotes if executed via browser address bar.

RestEasy: #PathParam returns first word if string has spaces

My API endpoint looks like
#GET
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
#Path("groups/{groupName}")
public Response getCategoriesByGroupName(#PathParam("groupName") #Nonnull final String groupName) {
return Response.ok(getCategoryTO().getCategoriesByGroupName(categoryManager, groupName)).build();
}
When I try to hit the endpoint as
curl -H"Content-Type: application/json" -H"BEARER:792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Utilities"
I get the correct JSON back, meaning groupName is correctly substituted with Utilities
But when I do
curl -H"Content-Type: application/json" -H"BEARER:792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Food & Drink"
I see server error as
Caused by: javax.ejb.EJBException: javax.persistence.NoResultException: no categories exists with groupName: Food
The problem is that groupName was substituted as Food instead of Food & Drink
I tried with other groupNames and realized that whenever my groupName has spaces, only the first word is substituted as groupName
How can I fix this issue so that entire string passed in URL becomes groupName?
My server is deployed on Wildfly 8
That has nothing to do with my endpoint.
As mentioned by #Sotirios, I needed to encode my URL before sending it to server.
When I did that thing started to work
curl -H"Content-Type: application/json" -H"BEARER: 792345452:78f7f8a4-a8c9-454a-93a8-6633a1076781:169000000" "https://myapp.com/rest/categories/groups/Food%20%26%20Drink"
[{"id":"9b1e97f2-ac7d-4caf-85fc-476cd97dd6cb","name":"Alcohol & Bars","groupName":"Food & Drink"}]

Categories

Resources