curl 'http://localhost:8080/userlogin'
-H 'Accept-Encoding: gzip, deflate, br'
-H 'GICAR: UNITAT_MAJOR=PRESIDÈNCIA'
As you can see, browser is sending a header with a content with È character.
I need to deal with it in to service:
LOG.debug("Default Cahrset: {}", Charset.defaultCharset().displayName());
String headerValue = request.getHeader(EspaiDocConstants.Headers.GICAR_HEADER);
LOG.debug("Header value: {}", headerValue);
The output is:
Default Cahrset: UTF-8
UNITAT_MAJOR=PRESIDÃNCIA
As you can see, È is chaged to Ã.
Any ideas?
Try to add this to the curl command :
-H "charset=utf-8"
Related
I'm using Curl to call json API with following as:
curl -i -k -X POST -H "Content-Type: application/json" -d "{\"niceSessionKey\": \"S202320220722145915340287\",\"fiCode\": \"B100000022\",\"taskCode\": \"KYC_VC2_RSLT\",\"appNumber\": \"APPL000000000000999\",\"customerNumber\": \"12345678\",\"mobilePhoneNumber\": \"0966688526\"}" https://100.113.125.128:8080/api/KYT_VC7_RSLT
But when i run it return result from server following as:
"status":"UNSUPPORTED_MEDIA_TYPE","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","errors":["application/x-www-form-urlencoded;charset=UTF-8 media type is not supported. Supported media types are application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, application/json, application/*+json, */*"]}
I have set Content-Type is application/json, but i don't why it still return get result error. How to fix the problem ?
try this :
#PostMapping(
path = "/web/feedback",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
I'm trying to make a multipart request using restassured. The request fails with the tested API telling me that the "information" part of the request is not an object. Here is my code:
RequestSpecification request;
Options options = Options.builder()
.printMultiliner()
.build();
RestAssuredConfig config = CurlLoggingRestAssuredConfigFactory.createConfig(options).multiPartConfig(MultiPartConfig.multiPartConfig().defaultCharset(StandardCharsets.UTF_8));
request = given()
.relaxedHTTPSValidation()
.config(config)
.header("Authorization", "Bearer " + LoginSteps.accessToken)
.queryParam("memberId", memberId)
.queryParam("policyId", policyId)
.contentType(String.valueOf(ContentType.MULTIPART_FORM_DATA))
.multiPart("information", "{\"description\":\"info\"}")
.multiPart("documents", new File(filePath), contentType)
.log().everything();
return request.post(baseUrl + endpoint);
and now here is the weird part. The standard restassured log looks like this:
Request method: POST
Request URI: myUri/?param1=149479812¶m2=19281999
Proxy: <none>
Request params: <none>
Query params: param1=149479812
param2=19281999
Form params: <none>
Path params: <none>
Headers: Authorization=Bearer aToken
Accept=*/*
Content-Type=multipart/form-data; charset=ISO-8859-1
Cookies: <none>
Multiparts: ------------
Content-Disposition: form-data; charset=ISO-8859-1; name = information; filename = file
Content-Type: text/plain
{"description":"info"}
------------
Content-Disposition: form-data; charset=ISO-8859-1; name = documents; filename = testFile.jpg
Content-Type: image/jpg
src\test\resources\testData\testFile.jpg
Body: <none>
Being a bit desperate to determine the cause of the issue, I also turned on curl logging and here's what I saw:
13:58:05.312 [main] DEBUG curl - curl "myUri/?param1=149479812¶m2=19281999" ^
--request POST ^
--header "Authorization: Bearer aToken ^
--header "Accept: */*" ^
--header "Host: theHost" ^
--header "Connection: Keep-Alive" ^
--header "User-Agent: Apache-HttpClient/4.5.13 (Java/11)" ^
--form "information={""description"":""info""};type=text/plain; charset=US-ASCII" ^
--form "documents=#testFile.jpg;type=image/jpg" ^
--compressed ^
--insecure ^
--verbose
As you can see, the content of "information" here has duplicated quotation marks. Which seems to be the cause of why this is failing. I also copy-pasted everything into Postman and it passes, and returns a 201 as expected. Log and screenshot below:
POST myUri/?param1=149479812¶m2=19281999 {
"Request Headers": {
"authorization": "Bearer aToken"
"user-agent": "PostmanRuntime/7.29.0",
"accept": "*/*",
"postman-token": "bb25e9db-87c0-4fdd-959a-e4229ed36e7a",
"host": "host",
"accept-encoding": "gzip, deflate, br",
"connection": "keep-alive",
"content-type": "multipart/form-data; boundary=--------------------------115811667537577795637808",
"content-length": "28196"
},
"Request Body": {
"information": "{\"description\":\"info\"}",
"documents": ""
}
I have honestly no idea what I'm doing wrong here. When I remove the quotation marks, I get no quotation marks. when I insert one I get two in the curl log. I don't think the API is at fault, as the same request works using postman. Can anyone help, please?
After configuring the service to show logs of what requests are arriving, I found out that the quotation marks are not duplicated. It turns out the curl logger was responsible for that. And the request did not pas due to a bug in the API
Spring Boot is decoding a String from a #RequestHeader annotation with ISO-8859-1 instead of UTF-8.
abstract of application.properties
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.http.encoding.force-request=true
spring.http.encoding.force-response=true
spring.messages.encoding=UTF-8
spring.banner.charset=UTF-8
spring.mandatory-file-encoding=UTF-8
server.tomcat.uri-encoding=UTF-8
spring.freemarker.charset=UTF-8
Controller
#Controller
#RequestMapping(value = "/mapping/", produces = "text/plain;charset=UTF-8")
public class MyController {}
curl command
curl 'https://example.com/mymapping/' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Upgrade-Insecure-Requests: 1' -H 'User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' -H 'Referer: https://example.com/mymapping/' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.9,de;q=0.8' -H 'Cookie: cookiesession1=<*>; mellon-cookie=<*>; JSESSIONID=<*>' --compressed
Some special characters are still wrong decoded for example a ü will be ü afterwards.
With the following workaround I get the correct string.
private static final Charset UTF_8 = Charset.forName("UTF-8");
private static final Charset ISO = Charset.forName("ISO-8859-1");
String textnew = new String(textold.getBytes(ISO), UTF_8);
I want to declare my application to decode all data in UTF-8 not ISO or something similiar. Any suggestions?
Somewhere i read Java expects that *.properties files are encoded in ISO-8859-1 and that's why Spring treats application.properties as if it's in ISO-8859-1.Try using YAML instead of properties files.
Can you please verify the response headers what is the value for Content-Type:?
for ex :application/json;charset=UTF-8
I have a RequestMapping setup in a spring #Controller and I am unable to correctly send a CURL request to it. I keep getting a HTTP 400 return code when trying to send various ways. I am not sure what is setup incorrectly.
Here is the request
curl -v -X POST localhost:8082/api/registerDevice -d '{"serial":"FA541YJ05065"}' -H "Content-Type:application/json;charset=UTF-8"
Here is the output
Note: Unnecessary use of -X or --request, POST is already inferred.
* Trying ::1...
* Connected to localhost (::1) port 8082 (#0)
> POST /api/registerDevice HTTP/1.1
> Host: localhost:8082
> User-Agent: curl/7.49.0
> Accept: */*
> Content-Type:application/json;charset=UTF-8
> Content-Length: 23
>
* upload completely sent off: 23 out of 23 bytes
< HTTP/1.1 400 Bad Request
< Server: Apache-Coyote/1.1
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 16 Jun 2016 02:48:50 GMT
< Connection: close
<
{"timestamp":1466045330556,"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read document: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream#3bae0839; line: 1, column: 2]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')\n at [Source: java.io.PushbackInputStream#3bae0839; line: 1, column: 2]","path":"/api/registerDevice"}* Closing connection 0
Spring RequestMapping
#RequestMapping(value = "registerDevice", method = RequestMethod.POST)
public ResponseEntity<String> registerDevice(#RequestBody Map<String, Object> payload) throws Exception {
Update
As mentioned below it was an issue with escaping the quotes and related to windows. The same CURL command worked fine on centos.
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{"""serial""":"""FA541YJ05065"""}" http://localhost:8082/api/deregisterDevice
On Centos
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"serial":"FA541YJ05065"}' http://localhost:8082/api/registerDevice
I suspect that you end up sending ' at the start of the json. You can try using double quotes around the json and escaping them in it.
See also
curl -v -X POST localhost:8082/api/registerDevice -d "{\"serial\":\"FA541YJ05065\"}" -H "Content-Type:application/json;charset=UTF-8"
The only reason behind this is that fact that your request is not formatted correctly
Check out this Spring 4.x/3.x (Web MVC) REST API and JSON2 Post requests, how to get it right once for all? for more information.
I do not know Java and I haven't had success with Google or trial and error testing...
How would I write this with just using CURL on the command line for a RESTful API authentication? (a php or perl solution would also be okay)
This code is from documentation but I don't plan on using Java and need to translate it.
URL url = new URL("http://www.thingsandstuff.com/resfulness/post");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// i don't know if this is relevant to my question or not...
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
// this is what i think might be the problem...
String userNamePassword = "myusername:mypassword";
userNamePassword =
new String(org.apache.commons.codec.binary.Base64.encodeBase64(userNamePassword
.getBytes()));
conn.setRequestProperty("Authorization", userNamePassword);
I always get a 401 unauthorized error.
I've been just playing with the header argument -H. I don't know if what I am doing is not working because I am truly not doing the authentication right or if it is something else. The data is xml and I am testing with something very simple/straightforward. I am assuming that even if my data/xml/post is incorrect, it would be returning an error instead of an unauthorized.
# bXl1c2VybmFtZTpteXBhc3N3b3Jk == myusername:mypassword base64 encoded....
curl -H "bXl1c2VybmFtZTpteXBhc3N3b3Jk" -d "<datums>" -X POST http://www.thingsandstuff/restfulness/post
curl -H "bXl1c2VybmFtZTpteXBhc3N3b3Jk" -d "<datums>" -X POST http://www.thingsandstuff/restfulness/post
# and like this... with encoded things and not encoded things...
curl -d "<datums>" -X POST http://user:password#www.thingsandstuff.com/restfulness/post
curl -u user:password -d "<datums>" -X POST http://www.thingsandstuff.com/restfulness/post
And for all three, I've tried encoding just the password, both together, both separately, neither... And I'm sure it doesn't help that my username does have a \ and the password does end in an ! but I think, when I'm not encoding them, I am escaping properly.
And the curl man page says that -d will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded, so that means I don't need to pass an additional header saying the content-type, right? Basically... I only partially know what I'm doing and it doesn't work and there are too many things I don't know about enough to isolate my issue or probably even correct it (yes, just enough to be dangerous).
The reason you are getting the 401 is the Authorization header needs a scheme prefix, in your case Basic
So currently you are sending
Authorization: <base64(username:password)>
But the web server expects
Authorization: Basic <base64(username:password)>
as described here
this change should work:
conn.setRequestProperty("Authorization", "Basic " + userNamePassword);
Okay, so this is what I ended up with...
curl -H "Authorization:dXNlclxuYW1lOnBhc3N3b3JkIQ==" -X POST -d 'data=%3C%3Fxml+version....' http://www.stuffandthings.com:8080/restfulness/post
So... username:password is base 64 encoded together. The data is encoded by ISO/IEC 8859-1 and prefixed with "data=" this particular set restful service.
I ended up putting it in PHP just because it was easier to test continuously that way. And this is what ended up working for that...
$user = 'user\\name';
$pass = 'password!';
$userpass = base64_encode($user.':'.$pass);
$url = 'http://www.thingsandstuff.com:8080/restfulness/post';
if (($handle = fopen("foo.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
list($thing1,$thing2) = $data;
$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<entry xmlns=\"http://purl.org/atom/ns#\">
<xml data goes here>
<ns:thing1>$param1</ns:thing1>
<ns:thing2>$param2</ns:thing2>
</xml data goes here>
</entry>";
$datums = 'data='.utf8_decode($xml);
$result = do_curl($url,$datums);
// not actually using the response at this
// point... just running through everything
var_dump($result);
}
fclose($handle);
}
function do_curl($url, $data) {
global $userpass, $user, $pass;
$ch = curl_init();
$header = array('Authorization:'.$userpass,
'Content-Type:application/x-www-form-urlencoded',
'Content-Length:'.strlen($data));
// this line was the last thing to be added before it worked
// i didn't try it again, removing the authentication in $header
// to see if this was the only thing actually needed...
curl_setopt($ch, CURLOPT_USERPWD, $user.':'.$pass);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$hout = curl_getinfo($ch, CURLINFO_HEADER_OUT);
//echo "\n"; var_dump($hout); echo "\n";
curl_close($ch);
return array('status'=>$status, 'result'=>$result);
}
And If I var_dump($hout) from the do_curl function... the header actually sent was:
POST /resfulness/post HTTP/1.1
Authorization: Basic dXNlclxuYW1lOnBhc3N3b3JkIQ==
Host: www.thingsandstuff.com:8080
Accept: */*
Content-Length: 409
Content-Type: application/x-www-form-urlencoded
<?xml version="1.0" encoding="UTF-8" ?>
<entry xmlns="http://purl.org/atom/ns#">
<xml data goes here>
<ns:thing1>value1</ns:thing1>
<ns:thing2>value2</ns:thing2>
</xml data goes here>
</entry>