curl --data-urlencode UTF-8 - java

I would like to send a HTTP GET request that contains a json as a parameter. The server expects the parameter to be encoded with UTF-8.
curl -G -v "http://localhost:8000/job" --data-urlencode "request={"clientId"="13","message"="I want apples"}"
I have used --data-urlencode but it doesn't seam to encode in UTF-8 because the server cannot decode the request.
I have a java client that uses
String charset = StandardCharsets.UTF_8.name();
String requestParameter = request={"clientId":"13","message"="I want apples"}
URLEncoder.encode(requestParameter, charset)
to encode the parameter and then send the HTTP GET request and the server accepts the request and is able to decode correctly.
The correct curl command is:
curl -G -v "http://localhost:8000/job" --data-urlencode 'request={"clientId":"13","messave":"I want apples"}'

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. [...]

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

Converting a Authenticated curl request to Apache Camel Request

curl -s -S -u user123:321pass https://12.15.13.12:3216 --data
'<?xml version="1.0" encoding="UTF-8"?><data>Hello Man</data>' -H 'Content-Type: text/xml' -k
I need to change this to a Apache Camel route call like :-
private ProducerTemplate producer;// there are setters for this.
producer.requestBodyAndHeaders(endpointUri, requestBody,addHeaders, String.class);
Main question is how do I pass username and password with this requestBodyAndHeaders.I tried passing through headers(header being map, key value pairs)

Spring REST controller post request

I have this controller in spring
#RestController
public class GreetingController {
#RequestMapping(value = "/greeting", method = RequestMethod.POST)
public String greeting(#RequestParam("uouo") String uouo) {
return uouo;
}
}
and when I testing it
curl -k -i -X POST -H "Content-Type:application/json" -d uouo=test http://192.168.1.104:8080/api/greeting
the result of the testing
HTTP Status 400 - Required String parameter 'uouo' is not present
I tried may thing, but I think #RequestParam can't use for POST it always passed the parameter in URL using GET, I use post only if I had object JSON as parameter using #RequestBody, is there any way to make string parameter send using POST?
The Servlet container will only provide parameters from the body for POST requests if the content type is application/x-www-form-urlencoded. It will ignore the body if the content type is anything else. This is specified in the Servlet Specification Chapter 3.1.1 When Parameters Are Available
The following are the conditions that must be met before post form
data will be populated to the parameter set:
The request is an HTTP or HTTPS request.
The HTTP method is POST.
The content type is application/x-www-form-urlencoded.
The servlet has made an initial call of any of the getParameter family of methods on the request object.
If the conditions are not met and the post form data is not included
in the parameter set, the post data must still be available to the
servlet via the request object’s input stream. If the conditions are
met, post form data will no longer be available for reading directly
from the request object’s input stream.
Since you aren't sending any JSON, just set the appropriate content type
curl -k -i -X POST -H "Content-Type:application/x-www-form-urlencoded" -d uouo=test http://192.168.1.104:8080/api/greeting
or let curl infer it
curl -k -i -X POST -d uouo=test http://192.168.1.104:8080/api/greeting?uouo=test
Note that you can still pass query parameters in the URL
curl -k -i -X POST -H "Content-Type:application/json" http://192.168.1.104:8080/api/greeting?uouo=test

Resteasy #QueryParam parsing

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"

Categories

Resources