I'm having troubles getting the correct encoding for a text file with xhr.
xhr(content.getContentUrl(), {
handleAs: "text",
headers: { 'Content-Type': 'text/plain; charset=iso-8859-1' }
}).then(function (data) {
console.log("DATA");
console.log(data); ... );
The data object is a text file that should be with ISO-8859-1 characters, but I get a ? instead of the special character, it's like the response encoding is UTF-8
Example: "PER-RW-C-MC-013,B,ABB, P�rtico 5B. Fundaciones. Memoria de
C�lculo,17/06/2011,23/06/2011,17/06/2011,01/07/2011,24/06/2011,20/07/2011,24/06/2011,19/07/2011,0,PER-RW-C-MC-013-C,PER-RW-C-MC-013-A"
Note: The content.getContentUrl() is a method from IBM filenet API that returns the text file URL in a filenet Repository.
Thanks in advance.
In response to your xhr request, you have code on your server that reads the file into a string and sends back that string as part of the response. This may very well be where the problem arises. See for example here (case of php) for a situation where this happened and a solution is suggested.
Related
I am currently doing web development with dart.
Implemented service with mockclient.
However, the following error occurs.
The implementation code below is an in memory web api service that inherits mockClient.
The code that calls client.send () and returns the result.
test_value is the result of json.encode (data).
var test_value = '{"id": 1, "type": "Appetizer", "name": "한글"}';
return Response (test_value, 200, headers: {'content-type': 'application / json'});
ERROR
Invalid argument(s): String contains invalid characters.
dart:convert Latin1Codec.encode
package:http/src/response.dart 36:49 new Response
package:basil/common/mock_rest/mock_recipe.dart 40:12 MockRecipe._handler
If you put an English string in the name of the above implementation code, there is no error.
Why do I get an error when I insert a character other than English?
Please let me know if you know!
A dart programmer struggling alone in Korea
The Response class uses Latin-1 encoding for the body unless something else is specified.
This is not documented clearly on the constructor itself, but the documentation on the body getter does suggest this.
Try setting the charset/encoding in the header, e.g., as:
return Response(test_value, 200, headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=utf-8'
});
I am implementing REST API for merchant system. This merchant system sends payment confirmation POST request to my system to specified URL, and this request is encoded in windows-1251.
I've tried iron-router and restivus to handle this request, both of them failed with error
UnsupportedMediaTypeError: unsupported charset "WINDOWS-1251"
at Object.urlencodedParser [as handle] (/home/dev/builds/bundle/programs/server/npm/node_modules/meteor/simple_json-routes/node_modules/connect/node_modules/body-parser/lib/types/urlencoded.js:102:12)
at next (/home/dev/builds/bundle/programs/server/npm/node_modules/meteor/webapp/node_modules/connect/lib/proto.js:174:15)
at middleware (packages/oauth.js:107:7)
at packages/oauth.js:96:5
My current iron router code:
Router.route('/api/payments/result/',{where: "server", name: 'payments-result'})
.post(function () { });
Router.onBeforeAction(function (req, res, next) {
logger.info('GOT A CONFIRMATION REQUEST');
logger.info(`request headers are: ${req.rawHeaders}`);
logger.info('request is: ');
Object.keys(req.body).forEach(key => {
let val = req.body[key];
logger.info(`${key} : ${val}`);
});
res.end('hello from the server\n');
},{where: 'server', only: ['payments-result']});
None of log statements are executed even during onBeforeAction hook.
The question is how to setup correct request decoding or to avoid body-parser that accepts only utf-8 encoded request, as I've found out.
UPDATE
Ad hock solution was to convert win-1251 to utf8 using nginx
Ad hock solution was to convert win-1251 to utf8 using nginx.
I can log in using username and password here http://localhost:7474/ by typing server: connect and logging in. I can view data from there by executing queries.
Then I immediately switch to a new tab, or in the same tab, and go to: http://localhost:7474/db/data/, and I get:
{
"errors" : [ {
"message" : "No authorization header supplied.",
"code" : "Neo.ClientError.Security.AuthorizationFailed"
} ]
}
And I cannot connect using py2Neo or any NEO4J libraries either using the same password; they return the exact same error.
What am I doing wrong?
add this to your http-headers request:
Authorization: "Basic xxxx"
xxxx = base64(username:password)
All REST API requests must now include the Authorization header. To quote the REST API Authentication and Authorization page of the neo4j manual:
Requests should include an Authorization header, with a value of Basic
<payload>, where "payload" is a base64 encoded string of
"username:password".
That page contains some examples.
Note: you can also disable authentication -- but you should only do this on your personal machine, for development purposes. You do this by setting to false the dbms.security.auth_enabled property in <neo4j-install-dir>/conf/neo4j-server.properties, and then restarting the server.
[UPDATED]
By the way, since your question mentioned py2neo, you should know that its Graph class supports "authorisation".
The request json should look like: (XXX being the Base64 encoding of the user:password string - the string to encode contains the column):
{
method: "POST",
headers: {
"content-type": "application/json",
"Authorization": "Basic XXX"
},
body: {
statements:[
{
statement: query,
parameters: params
}
]
}
}
This has been tested in Javascript (axios) and in Deno.land (fetch API). ES Javascript contains a built in base64 encoding function: btoa()
I have a servlet which accepts HTML content as part of the request param. The HTML is a localized one which may be a french, spanish etc... content.
I'm also using apache HTTP client to make a request to this servlet for test purpose, which has the following header definition:
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("<URL>");
String html = FileUtils.readFileToString(inputHTMLFile, "UTF-8");
method.addParameter("html", html);
method.addRequestHeader("Accept", "*/*");
method.setRequestHeader("accept-charset", "UTF-8");
Whatever HTML is read has the character encoding utf-8, sample text:
Télécharger un fichier
However when i get the html from the request param that texts becomes T?l?charger un fichier
I went through few links such as http://www.oracle.com/technetwork/articles/javase/httpcharset-142283.html which talks about charset and how normally a browser would encode the special characters. If i were to URLEncode the html with UTF-8 and then decode that with same charset in the servlet i get the HTML as expected.
Is this the only thing i can do to preserve the charsets? Am i missing something?
Thanks.
Now that the issue with the file itself is fixed, try modifying your code as follows:
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("<URL>");
postMethod.getParams().setContentCharset("utf-8"); //The line I added
...
Note that the client needs to decode the request as UTF-8 now. French and Spanish worked correctly because their characters are included in the default ISO-8859-1 charset. Chinese characters are not. If the French and Spanish were decoded correctly on client, the client is decoding the request as ISO-8859-1, and sending UTF-8 could fail.
So you could try also adding this:
postMethod.setRequestheader("Content-Type", "application/x-www-form-url-encoded; charset=utf-8");
Just try this for post method.
HttpPost request = new HttpPost(webServiceUrl);
StringEntity str = new StringEntity(YourData);
str.setContentType("application/json");
HttpPost.setEntity(new StringEntity(str, HTTP.UTF_8));
You should better change string to base64 encoded and then send.
I think I've found the cause by examining EntityBuilder decompiled code: the EntityBuilder ignores the contentEncoding field regarding the parameters, it uses the one from contentType field. And by looking on org.apache.http.entity.ContentType the only one predefined value having UTF-8 is org.apache.http.entity.ContentType.APPLICATION_JSON.
So in my case
HttpPost method = new HttPost("<URL>");
EntityBuilder builder = EntityBuilder.create();
builder.setContentType(ContentType.APPLICATION_JSON);
builder.setContentEncoding(StandardCharsets.UTF_8.name());
...
method.setEntity(builder.build());
did the job (although I think setting contentType is redundant here).
I'm using httpclient-osgi version 4.5.4.
PostMethod method = new PostMethod("URL");
method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
I am working on an application in which i have a situation to send text data to backend through Ajax call.
But when the text "%" occurs in the text to be sent through ajax, i get the below response from my rails app
Internal Server Error
invalid %-encoding
i tried adding escape characters infrot of % symbol, but its not useful.
Any suggestion on this issue would be helpful.
Jquery ajax call used :
thanks,
Balan
You can pass jquery an object for the data option and it should correctly escape it for you:
$.ajax({
type: "POST",
url: "/controller",
data: {
text: text_from_text_area,
current_poster: current_poster
},
success: function(data){
alert(data);
}
});
See the docs here: http://api.jquery.com/jQuery.ajax/