I've got a Rails 3.2 app running on Ruby 1.9.3 that returns JSON data stored in a MongoDB database. The data seems to be stored correctly in mongo, e.g. (look at the name attribute):
{ "_id" : ObjectId("4f986cbe4c8086fdc9000002"), "created_at" : ISODate("2012-04-25T21:31:45.474Z"), "updated_at" : ISODate("2012-04-26T22:07:23.901Z"), "creator_id" : ObjectId("4f6b4d3c4c80864381000001"), "updater_id" : null, "name" : "Trädgår'n", "sort" : "tradgarn", "address" : "Nya Allén 11", "coordinates" : [ 11.9764791, 57.7045625 ], "phone" : "46031102080", "url" : "http://www.profilrestauranger.se/tradgarn/", "user_ids" : [ ] }
But when I issue a request that returns this record, I get something like this back (now look at the name attribute):
{"address":"Nya All\u00e9n 11","coordinates":[11.9764791,57.7045625],"created_at":"2012-04-25T23:31:45+02:00","id":"4f986cbe4c8086fdc9000002","name":"Tr\u00e4dg\u00e5r'n","phone":"46031102080","sort":"tradgarn","updated_at":"2012-04-27T00:07:23+02:00","url":"http://www.profilrestauranger.se/tradgarn/"}
The response headers for anyone interested:
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Thu, 26 Apr 2012 22:41:13 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 909
Connection: keep-alive
Status: 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Max-Age: 1000
Access-Control-Allow-Headers: *,x-requested-with
X-UA-Compatible: IE=Edge
ETag: "d2a95f06bec10d8087c3188280292d3c"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: fdd042568195df279e59affe45bdcd37
X-Runtime: 0.037134
I cannot seem to figure out why or where the encoding is getting messed up? What gives? Help? :)
The issue is indeed one of JSON UTF-8 encoding. The #to_json method is escaping unicode characters. This can be observed by something like:
user.to_json
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"tr\\u00e4dg\\u00e5r#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
When parsed, however, this is converted back to how you would expect it. It is possible, however, to generate the JSON using JSON.generate, with which the #as_json method can be used, along with any options for the construction. This doesn't escape the unicode. To do such:
JSON.generate(user.as_json)
# => "{\"created_at\":\"2012-04-19T18:48:01Z\",\"email\":\"trädgår#example.com\",\"id\":10,\"updated_at\":\"2012-04-27T18:37:10Z\"}"
Turns out the problem I was seeing was with the gem colorful_json. I was running the JSON thru its CLI utility cjson, and it was messing up the Unicode. I reported the issue and the new version of the gem fixes this.
Related
I'm trying to access 'nested' headers with the REST Client extension for VS Code.
I defined a GET CSRF_Token request that stores the "Set-Cookie" header inside a #token_response variable.
However the header looks like this:
'csrftoken=aGHuGkRbApH3qAksHHR3uaOKG0eZsYne; expires=Wed, 22 Nov 2023 08:40:05 GMT; Max-Age=31449600; Path=/; SameSite=Lax'
I already tried to access it with something like:
#csrf_token = {{token_response.0.split(';').0.split('=').1}}
and
#csrf_token = {{token_response.//[0].//split(';')[0].//split('=')[1]}}
But this does not seem to be the correct syntax.
Also the documentation does not contain any hints on that yet, sadly.
Now my question is:
Is there a way to only extract the part of the header that contains the value of the "csrftoken" key?
And if so, how?
Here is my code:
# Get CSRF Token
# #name getToken
GET http://{{host}}/api/csrf_token
#token_response = {{getToken.response.headers.Set-Cookie}}
#csrf_token = {{token_response}}
######
# Edit existing xyz
PATCH http://{{host}}/api/xyz/1
content-type: application/json
x-csrftoken: {{csrf_token}}
origin: http://{{host}}
{
"name": "some name"
}
Thanks a lot in advance! :)
The syntax for the HTTP Cache-Control no-cache directive of a response allows a field-name argument.
RFC7234#5.2.2.2 says:
5.2.2.2. no-cache
Argument syntax:
#field-name
// ...
If the no-cache response directive specifies one or more field-names,
then a cache MAY use the response to satisfy a subsequent request,
subject to any other restrictions on caching.
// ...
The specs state that the no-cache directive may "specif[y] one or more field-names". RFC7230#3.2 defines a field-name as being a token.
What the spec doesn't seem to cover is how multiple field names are delimited/separated.
How are Cache-Control no-cache field names delimited/separated?
Example with no field name (most common usage)
Cache-Control: no-cache
Example with single field name (never personally seen it, technically valid)
Cache-Control: no-cache=foo
Multiple field names ?
Cache-Control: no-cache=foo,bar
Cache-Control: no-cache=foo,bar
Cache-Control: no-cache=foo, bar
Cache-Control: max-age=60, private, no-cache=foo, bar, min-age=30
Cache-Control: max-age=60, private, no-cache="foo, bar", min-age=30
To me, only the last above example (the double-quoted no-cache value) is unambiguous.
What is the correct form to use?
I ask as I need to correctly parse arbitrary Cache-Control header values.
RFC's BNF at RFC2616#14.9 goes:
"no-cache" [ "=" <"> 1#field-name <"> ] ; Section 14.9.1
RFC2616#2.1 explains the # rule is a list:
#rule
A construct "#" is defined, similar to "*", for defining lists of
elements. The full form is "#element" indicating at least
and at most elements, each separated by one or more commas
(",") and OPTIONAL linear white space (LWS). This makes the usual
form of lists very easy; a rule such as
( *LWS element *( *LWS "," *LWS element ))
can be shown as
1#element
RFC2616#2.2 tells you <"> just means "
<"> = <US-ASCII double-quote mark (34)>
Therefore it would be `Cache-Control: max-age=60, private, no-cache="foo, bar", min-age=30`, as you did in your last one.
I'm trying to figure out SoapUI, and so far it's been a great tool. However, I cannot figure out this transferring of property stuff. I've read so much and just can't seem to find the answer I'm looking for.
I have one request: TC01_vorbereitenKunde
I receive the following Response Payload back:
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
SOAPAction: "http://..."
Accept: text/xml
Content-Type: text/xml; charset=UTF-8
Content-Language: en-US
Date: Tue, 22 Sep 2015 15:58:01 GMT
Content-Length: 414
Content-Encoding: gzip
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<nova-kunden:vorbereitenKundeResponse xmlns:nova-kunden="http://...>
<nova-kunden:vorbereitungKundeAntwort>
<status>true</status>
<tkid>31f64d0f-b076-4304-95ab-15cb0de38adb</tkid>
<meldungen/>
</nova-kunden:vorbereitungKundeAntwort>
</nova-kunden:vorbereitenKundeResponse>
</soapenv:Body>
I then want to take the "tkid" value and place it in the following request: TC02_offeriereLeistungen
I've tried: ${TC01_vorbereitenKunde#Response#//tkid}
"TC01_vorbereitenKunde" is the name of the Test Step where the response payload is from to no avail.
What am I missing? Thank you so much in advance for your help!
Have script assertion for the first step as given below:
import com.eviware.soapui.support.XmlHolder
def xml = new XmlHolder(context.response)
def responseValue = xml.getNodeValue("//*:tkid")
assert null != responseValue, "Response does not have value"
context.testCase.setPropertyValue('TK_ID', responseValue)
In the second step, use ${#TestCase#TK_ID} where value is needed.
Using JAX-RS and Glassfish 3.1.2 I am trying to get the charset added to the content-type.
I've tried a few of the answers on SO, but I have one issue:
#Produces(
{
MediaType.APPLICATION_JSON + "; charset=utf-8",
MediaType.APPLICATION_XML + "; charset=utf-8",
})
results in a header like:
Content-Type: application/xml;charset=utf-8
However some other code (a Python library) requires it to be like:
Content-Type: application/xml; charset=utf-8
(note the space between the ; and charset).
From what I can tell the space is optional, but the Python library (out of our control) requires it.
Any ideas on what I need to do to convince JAX-RS to spit out the space?
This is my first quesiton, but what I'm trying to do is send mail with an attachment in rails console, using one or two lines. I dont want to instantiate a class like ..
class Mailer < ActionMailer::Base
...
end
I want to try it this way:
m=ActionMailer::Base.mail(:to => "harry#example.com", :from => "test#example.com", :subject=>"test from zip", :content_type=>"multipart/mixed")
m.attachments['file.zip']={:mime_type => "application/zip", :data=>File.read("#{Rails.root}/tmp/test.zip")}
m.deliver
This will send an email, but the attachment called noname, which can't be unzipped. It seems that its not parsing the data correctly for the attachment. If I look at the raw email the attachment contents looks something like this:
--
Date: Tue, 06 Mar 2012 06:59:42 -0800
Mime-Version: 1.0
Content-Type: application/zip;
charset=UTF-8
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename=file.zip
Content-ID: <4f56264f16e82_498a46e93467093#ip-10-125-15-127.mail>
UEsDBBQAAAAIAE9iZUBSMYOwkKgZANRakgAQABUAbG9hbl9kZXRhaWxzLmNz
dlVUCQADlh9VT0QfVU9VeAQA6APoA8xdW3PiuLZ+37+Ch6ldZ1dZGUvyNW/c
EwKBQLiENze4gytgZ9tmMplff5YMlgQWmV1tk5qufiAkwV8trcu3bko/8sLa
m/+p9dmLJPXSfaI1oyR4Df21Non28crPvt+MfS/117Uo5C+9VKu/v8fRH4e3
O0HobWte9g68gHdaQfJjHyeHb4/9/+79JPu9XbQPU22y2kTRVuv74dqPa7G/
...
1) is it even possible to send an email with an attachment like this, with out using something like the pony gem
An estimation to why it isn't working
According to SO post Invalid filename in email (ActionMailer) it seems to be ActionMailer wanting to automagically glean information from files, something which is unavailable from the console.
I noted that the following, albeit messy, works (sufficiently for my purposes) from the console:
File.open("magical_elephant_potato.txt", 'w') {|f| f.write("Heyyyy youuu!") }
m=ActionMailer::Base.mail(:to => "rainbowpony#company.com", :from => "noreply#railsapp.com", :subject=>"Behold my MEP attache", :content_type=>"multipart/mixed")
m.attachments['magical_elephant_potato.txt']=File.read("magical_elephant_potato.txt")
m.deliver
FileUtils.rm('magical_elephant_potato.txt')
Given that writing and removing files via console works, perhaps the files required by ActionMailer can be written, utilised then deleted? We're heading into sticky work-around territory here though. A problem is that ActionMailer will look for the appropriate mailer view, but how and can we tell ActionMailer where to look for the mailer files? (As in, the filename)
As for the information not being encoded correctly, I think the problem is that its being wrapped in the 'noname' file with some header info. The data is likely intact, as with my example I get:
--
Date: Tue, 08 Jan 2013 11:08:57 +0000
Mime-Version: 1.0
Content-Type: text/plain;
charset=UTF-8;
filename=magical_elephant_potato.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename=magical_elephant_potato.txt
Content-ID: <50bbfe4898ac_6d7febf6a312062#ws9.companydev.com.mail>
Heyyyy youuu!
----
:when I open 'noname' with a text editor.