i am submitting a value "02AP0040¶" in my grails 3 application, when the form is submitted via ajax the value in the controller is not altered but when the form is submitted normally the value is changed to "02AP0040¶"  is added. I have changed the mime types in application.yml, form: application/x-www-form-urlencoded to form: multipart/form-data Part of my configuration file (application.yml) is this :
grails:
applicationVersion: '#info.app.version#'
mime:
disable:
accept:
header:
userAgents:
- Gecko
- WebKit
- Presto
- Trident
types:
all: '*/*'
atom: application/atom+xml
css: text/css
csv: text/csv
form: multipart/form-data
html:
- text/html
- application/xhtml+xml
js: text/javascript
json:
- application/json
- text/json
multipartForm: multipart/form-data
pdf: application/pdf
rss: application/rss+xml
text: text/plain
hal:
- application/hal+json
- application/hal+xml
xml:
- text/xml
- application/xml
file.extensions: true
use.accept.header: false
urlmapping:
cache:
maxsize: 1000
controllers:
upload:
maxFileSize: 2097152 #2MB
maxRequestSize: 2097152 #2MB
defaultScope: prototype
converters:
encoding: UTF-8
views:
default:
codec: none
gsp:
encoding: UTF-8
htmlcodec: xml
codecs:
expression: none
scriptlets: html
taglib: none
staticparts: none
The submitting form is also submitting the correct value to the controller but when it comes to the controller the value is altered.
I have found a solution might not be the perfect one but solved my problem.
I have added enctype attribute as <form action="" method="post" enctype=multipart/form-data> to each form that submits such ascii characters. I tried to change the mime type in application.yml but it didn't work so i used javascript in my main page to add the enctype to every form which currently solved the issue.
What i found was, even though the method is post, the default form enctype is application/x-www-form-urlencoded which treats the request body as queryparameters and hence encode/escape the ascii characters so for to transfer these kind of characters we have to use enctype as multipart/form-data which doesn't encode the content.
Related
Where can I find a list of all the different content_type category that Active Storage has? I am trying to change an icon based on the file type.
For example:
In the documentation, there is application/pdf. I have also seen that there is image/png, video, audio or text. What interests me is the part before the file extension (therefore image or application). In which category would a Word or Powerpoint document be for example? How many content_type category there are?
Thank you in advance!
The content type is the MIME type of the uploaded document. Unless you excluded certain types it could be basically anything because a MIME type should have a certain structure but at least the suffix depends on the application.
Some common media types are:
application/javascript
application/json
application/msword (.doc)
application/pdf
application/vnd.api+json
application/vnd.ms-excel (.xls)
application/vnd.ms-powerpoint (.ppt)
application/vnd.oasis.opendocument.text (.odt)
application/vnd.openxmlformats-officedocument.presentationml.presentation (.pptx)
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet (.xlsx)
application/vnd.openxmlformats-officedocument.wordprocessingml.document(.docx)
application/x-www-form-urlencoded
application/xml
application/zip
audio/mpeg
image/gif
image/jpeg
image/png
multipart/form-data
text/csv
text/html
text/plain
text/xml
Rails maintains the Marcel library to do any content-type stuff. The canonical list of every content-type available in Rails is here:
https://github.com/rails/marcel/blob/main/lib/marcel/tables.rb
I'm trying to send json data as a response body in grails. I've tried setting the Content-Type header to application/json using the following methods:
render (status: httpServletResponse, text: responseToRender as JSON, contentType: "application/json")
Each time the resulting header is as follows:
Content-Type: application/json;charset=utf-8
How do I get rid of the charset=UTF-8 postfix?
You can not get rid of the charset postfix.
You can change it with the charset parameter defined here:
https://docs.grails.org/latest/ref/Controllers/render.html
You can also provide no information by just handing json to render, such as:
response.setContentType("application/json")
render JsonOutput.toJson(responseToRender);
However, this will default to the standard encoding required by HTTP 1.1 which is ISO-8859-1. therefore your result would be application/json;charset=ISO-8859-1
https://www.w3.org/International/articles/http-charset/index.en
So, if you somehow need to use this parameter, you may use .split(";")[0] to access only the first part.
I have an API call which responds 200 OK and returns an HTML. I would like to add this to my API documentation
(especially since i validate it using dredd and unless i provide it with the expected response body the test fails). How
would i do this in Swagger?
--- More details ---
My Response to an API call is 200 OK and with a one line Response Body:
<html><body>You are being redirected.</body></html>
I can easily define the Response Body in Blueprint in the following form:
+ Response 302 (text/html; charset=utf-8)
+ Body
`<html><body>You are being redirected.</body></html>`
But i'm not sure how to do this in Swagger. Almost all examples i can find are for application/json responses (understandably) and i'm having trouble
guessing the correct syntax for this kind of response.
The relevant swagger text in my document is this (so far without specifying the response body, so with an empty body dredd
fails because the response body should be <html><body>You are being redirected.</body></html>):
# this is my API spec in YAML
swagger: '2.0'
info:
title: My API (Swagger)
description: blablabla
version: "1.0.0"
# the domain of the service
host: my.domain.com
# array of all schemes that your API supports
schemes:
- https
# will be prefixed to all paths
basePath: /
produces:
- application/json; charset=utf-8
paths:
/users/password:
post:
summary: Password Reset
description: |
Handles Reset password for existing user.
consumes:
- application/x-www-form-urlencoded
produces:
- text/html; charset=utf-8
parameters:
- name: "user[email]"
description: email
in: formData
required: true
type: string
default: "user#gmail.com"
tags:
- Reset Password
responses:
200:
description: Success
Please comment if you have any suggestions on this. Thanks!
Figured it out. the response object has a field called "examples" which allows to show examples to your API response.
The syntax is clearly shown in the specification and basically says you need to identify the MIME-type for the example response, in my case text/html and the value is the actual text.
so like this:
responses:
200:
description: success and returns some html text
examples:
text/html:
<html><body>Your HTML text</body></html>
That's it. Now whether or not dredd knows to take this value and compare it is another story. their docs state that any response other than JSON is validated as plain text so this should work.
Hope this helped.
In case someone needs this in the openapi version 3.0 you can do it this way:
Save your HTML. For example give this at the end of your file:
components:
schemas:
error401:
type: string
example: '<html>HTML text</html>'
Then you can reference this scheme in responses wherever you want. For example:
responses:
'401':
description: Error
content:
text/html:
schema:
$ref: '#/components/schemas/error401'
What is the default character encoding for sent requests in Wildfly?
Setting the encoding in contentType header of a request would insure that it will be used?
Thanks,
Tiberiu
You are talking about two different encodings
Request encoding: This is the encoding of parameters in the URL for example. By default is UTF-8 but if you want to change it to ISO-8859-1 (for example) it can be done with <http-listener url-charset="ISO-8859-1" .../> in your configuration file under the undertow subsystem.
Content Type encoding: This is the encoding that you are saying your files have and this is controlled by Content-Type http header and the charset parameter. Content-Type: text/html; charset=ISO-8859-1
I just noticed that the respond method in controllers is returning HTML responses in the ISO-8859-1 charset (which garbles my unicode characters). It uses UTF-8 if I set the format to JSON. The render method also uses UTF-8.
I'm using Grails 2.4.4 and the Tomcat plugin v. 7.0.55 in development mode without overriding web.xml. Both grails.converters.encoding and grails.views.gsp.encoding are set to UTF-8. I have <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> in my template if that influences anything.
I've also tried forcing the charset by using respond myInstance, [encoding: "UTF-8"] but it didn't change anything.
Is there something I am not seeing or have I hit a bug?
EDIT: my config.groovy contains the following mime type definitions:
grails.mime.types = [ // the first one is the default format
all: '*/*', // 'all' maps to '*' or the first available format in withFormat
atom: 'application/atom+xml',
css: 'text/css',
csv: 'text/csv',
form: 'application/x-www-form-urlencoded',
html: ['text/html','application/xhtml+xml'],
js: 'text/javascript',
json: ['application/json', 'text/json'],
multipartForm: 'multipart/form-data',
rss: 'application/rss+xml',
text: 'text/plain',
hal: ['application/hal+json','application/hal+xml'],
xml: ['text/xml', 'application/xml']
]
It would seem this is a Grails bug, I've narrowed down the specific case when it happens: you need to have a static responseFormats = ['html', ...] limitation on the controller to trigger it. The fact that Grails' generate-restful-controller includes the responseFormats block automatically makes developers even more likely to encounter this issue.
I've filed a bug report.
EDIT: to keep the responseFormats block but still have UTF-8 responses, it's possible to set the encoding manually, perhaps like this:
def beforeInterceptor = {
response.characterEncoding = 'UTF-8' //workaround for https://jira.grails.org/browse/GRAILS-11830
}