The twitter API seems to escape ampersand, <, > but nothing else? - twitter

I wrote the following test tweet:
&“”‘’®©™¶•·§–—
Then fetched it using the 'user_timeline' api call. The following json was returned:
...
"text": "&“”‘’®©™¶•·§–—",
...
It seems strange the ampersand is the only escaped symbol.
Are there any other escaped symbols? I can't find a definitive list in the docs.
Alternatively, is it possible to specify if the api should return escaped/ unescaped characters?
Edit
Test tweet:
<>=+
Returns:
...
'text': '<>=+'
...

Related

Is possible to parse specials characters in an URL for Apple News?

I have an article with an URL in one body paragraph, with '[' and '[' (special characters). Apple News in their documentation said that you can parse special characters with '', but when I add it the News Preview App said that is a malformed JSON document. If I leave the URL as it, the error changes to an invalid URL. Is it possible to fix this?
Apple News Documentation => https://developer.apple.com/documentation/apple_news/apple_news_format/components/using_html_with_apple_news_format
Error: "URL" was "http://www.example.letter[1].pdf" should be valid URL with one of ["http","https","#","webcal","stocks","action"] scheme(s)
document.components[3].additions[0].URL
Error
Just encode the unaccepted characters, for example: '[' = '%5B'

Unable to URL encode { } for object filters in softlayer REST URI

I am calling a Get method through Rest and my URI contains { } for object filters in Softlayer. I have used %7B for { and %7D for }, but I get java.net.URISyntaxException.
The URI also contains # which I have replaced with %40. This is working. I am using http Client to execute my Rest Call. The URI works fine on Postman, both with and without URL Encoding.
Here two way to skip or handle #:
how is '#' handled in the softlayer Rest calls
Another way to skip special characters would be, using "\" at the start from any of these chars, here an example of this:
SoftLayer getUsers by userStatusID
Also, it's necessary to skip {}": (special chars in objectFilters), if you are continue hitting with the exception, would be great if you could provide an example or the exactly code that you are trying, for further assistance
How to get Virtual Guests for a specific datacenter
Here a rest request for dal05 datacenter:
https://api.softlayer.com/rest/v3/SoftLayer_Account/getVirtualGuests?objectMask=mask%5Bid%2Chostname%5D&objectFilter=%7B%22virtualGuests%22%3A%7B%22datacenter%22%3A%7B%22name%22%3A%7B%22operation%22%3A%22dal05%22%7D%7D%7D%7D

HTTParty is escaping JSON

I'm using HTTParty to send data to a remote API, however the API is complaining because the JSON being sent by HTTParty appears to be being escaped, and is thus deemed invalid.
Here's what I'm doing:
query = {"count"=>1,
"workspaces"=>
{123445=>
{"title"=>"Test Project",
"description"=>"",
"start_date"=>"2015-06-01T00:00:00.000Z",
"due_date"=>"2015-08-31T00:00:00.000Z",
"price_in_cents"=>8000,
"currency"=>"USD",
"status_key"=>130,
"custom_field_values_attributes"=>[],
"workspace_groups_attributes"=>
[{"created_at"=>"2015-07-13T11:06:36-07:00",
"updated_at"=>"2015-07-13T11:06:36-07:00",
"name"=>"Test Customer",
"company"=>true,
"contact_name"=>nil,
"email"=>nil,
"phone_number"=>nil,
"address"=>nil,
"website"=>nil,
"notes"=>nil,
"id"=>"530947",
"custom_field_values_attributes"=>[]}],
"id"=>123445}},
"results"=>[{"key"=>"workspaces", "id"=>123445}]}
Calling to_json on query escapes the JSON too:
"{\"count\":1,\"workspaces\":{\"123445\":{\"title\":\"Test Project\",\"description\":\"\",\"start_date\":\"2015-06-01T00:00:00.000Z\",\"due_date\":\"2015-08-31T00:00:00.000Z\",\"price_in_cents\":8000,\"currency\":\"USD\",\"status_key\":130,\"custom_field_values_attributes\":[],\"workspace_groups_attributes\":[{\"created_at\":\"2015-07-13T11:06:36-07:00\",\"updated_at\":\"2015-07-13T11:06:36-07:00\",\"name\":\"Test Customer\",\"company\":true,\"contact_name\":null,\"email\":null,\"phone_number\":null,\"address\":null,\"website\":null,\"notes\":null,\"id\":\"530947\",\"custom_field_values_attributes\":[]}],\"id\":123445}},\"results\":[{\"key\":\"workspaces\",\"id\":123445}]}"
Is this expected behavior to escape the JSON? Or I'm wondering if the hash I'm building for query is invalid for JSON purposes?
Any help would be greatly appreciated.
Calling to_json on query doesn't yield escaped JSON.
Try puts query.to_json to see that.
You see backslashes because #inspect method on String (and this method is called to display contents of variables to console) displays String enclosed in double quotes, and it has to escape quotes which are in the given string itself.
Your problem is probably not having proper Content-Type headers. You should do something like this:
result = HTTParty.post(url, body: query.to_json, headers: {'Content-Type' => 'application/json'})

How to debug HTTP AUTH params in Rails?

Rubyists,
something's wrong with my HTTP AUTH params that are coming into my Rails 3 app. The password has some whitespace at the end. I was debugging my client app and it looks like it is sending it correctly.
I am doing this in my app:
params[:auth_username], params[:auth_password] = user_name_and_password(request)
Then I am sending this into Warden.
I would like to see the raw data to see if the whitespace is there. How to do that?
Edit: I have debugged the wire between httpd and thin process and I am pretty sure the data are coming correctly. It must be something wrong in my Rails 3.0.10. I was able to decode the base64 string that is coming in the headers and it did not contain any whitespace.
This really looks like BASE64 decoder issue. Maybe a padding problem. My string is:
Qmxvb21iZXJnOnRjbG1lU1JT
which decodes to
Bloomberg:tclmeSRS
correctly using non-Ruby base64 decoders. Even in Ruby:
>> Base64.decode64 "Qmxvb21iZXJnOnRjbG1lU1JT"
=> "Bloomberg:tclmeSRS"
I don't get it. Searching for a bugreport in Rails or something like that.
Edit: So it turns out our Apache httpd proxy adds something to the header:
Authorization: Basic Qmxvb21iZXJnOnRjbG1lU1JT, Basic
This leads to the incorrect characters at the end of the password, because:
>> Base64.decode64('Basic Qmxvb21iZXJnOnRjbG1lU1JT, Basic'.split(' ', 2).last || '')
=> "Bloomberg:tclmeSRS\005\253\""
The question is now - is this correct? Is it a bug in httpd or rails?
Rails user_name_and_password method makes a call to decode_credentials that performs the following, then splits using ":" :
::Base64.decode64(request.authorization.split(' ', 2).last || '')
Applied to your data :
::Base64.decode64("Qmxvb21iZXJnOnRjbG1lU1JT".split(' ', 2).last || '').split(/:/, 2)
=> ["Bloomberg", "tclmeSRS"]
Everything seems to be ok, the problem sits elsewhere IMO. To dump the authorization data from your controller :
render :text => "Authorization: #{request.authorization}"

Twitter double encode entity references?

Why is twitter double encoding XML entity references?
Here's an example tweet:
xml entity ref test < & '
The response from statuses/friends_timeline:
<status>
<created_at>Wed Jun 24 00:16:15 +0000 2009</created_at>
<id>2302770346</id>
<text>xml entity ref test &lt; & '</text>
<source>web</source>
<truncated>false</truncated>
shouldn't it be
< & &apos;
I did some more test, here's what happens in the http post to send the update:
sniff again < & '
post data:
authenticity_token=secret_sauce_removed&status=sniff+again+%3C+%26+'&twttr=true&return_rendered_status=true
I've confirmed Justin's observation that only < > is double encoded. First line is the xml repsonse, 2nd line json.
<text>" & ' &lt; &gt;</text>
"text":"\" & ' < >"
Twitter documentation says "escaped and HTML encoded status body", I guess escaped means xml encoding < >.
But i still don't understand why they're doing it. No web pages are involved in the whole process. The tweet is sent through the rest API url-encoded, and it is retrieved as xml or json.
It's double coded because the text property is quasi HTML Encoded text (looks like they're only encoding < and > so that you don't start/end a new html element in your tweet). Therefore, before the XML parses it for communication across the wire, you'd have:
xml entity ref test < & '
That string then gets encoded again (so that when it is decoded, it is still the proper HTML Encoded text) which turns it in to the:
xml entity ref test &lt; & '
That you are getting back.
It looks like it's taking the HTML code, and sticking that inside of an XML field, so when you use your XML parser on the XML, you get valid HTML.

Resources