I'm trying to put a Twitter share link in an email. Because this is in an email I can't rely on JavaScript, and have to use the "Build Your Own" Tweet button.
For example, sharing a link to Google:
<a href="http://www.twitter.com/share?url=http://www.google.com/>Tweet</a>
This works fine. The problem I'm having is when the URL has a query string.
<a href="http://www.twitter.com/share?url=http://mysite.org/foo.htm?bar=123&baz=456">Tweet</a>
URLs with query strings confuse Twitter's URL shortening service, t.co. I've tried URL encoding this in various ways and cannot get anything to work. The closest I have gotten is by doing this.
<a href="http://www.twitter.com/share?url=http://mysite.org/foo.htm%3Fbar%3D123%26baz%3D456">Tweet</a>
Here I've encoded only the query string. When I do this, t.co successfully shortens the URL, but upon following the shortened link, it takes you to the encoded URL. I see http://mysite.org/foo.htm%3Fbar%3D123%26baz%3D456
in the address bar, and get the following error in the browser
Not Found
The requested URL /foo.htm?bar=123&baz=456 was not found on this server.
I'm at a loss as to how to solve this problem.
Edit: Re: onteria_
I've tried encoding the entire URL. When I do that no URL shows up in the Tweet.
I have tried to follow the documentation but was not able to use urlparse.parse.quote_plus()
in Python 3
:
from urllib.parse import urlparseparams = urlparse.parse.quote_plus({'username': 'administrator', 'password': 'xyz'})
I get
AttributeError: 'function' object has no attribute 'parse'
I am following the instruction from this page. I am building a slack slash command handling server and I can't rebuild the signature to validate slash request authenticity.
here is the code snippet from my django application (the view uses the django rest-framework APIView):
@propertydef x_slack_req_ts(self): if self.xsrts is not None: return self.xsrts self.xsrts = str(self.request.META['HTTP_X_SLACK_REQUEST_TIMESTAMP']) return self.xsrts@propertydef x_slack_signature(self): if self.xss is not None: return self.xss self.xss = self.request.META['HTTP_X_SLACK_SIGNATURE'] return self.xss@propertydef base_message(self): if self.bs is not None: return self.bs self.bs = ':'.join(["v0", self.x_slack_req_ts, self.raw.decode('utf-8')]) return self.bs@propertydef encoded_secret(self): return self.app.signing_secret.encode('utf-8')@propertydef signed(self): if self.non_base is not None: return self.non_base hashed = hmac.new(self.encoded_secret, self.base_message.encode('utf-8'), hashlib.sha256) self.non_base = "v0=" + hashed.hexdigest() return self.non_base
This is within a class where self.raw = request.body
the django request and self.app.signing_secret is a string with the appropriate slack secret string. It doesn't work as the self.non_base
yield an innaccurate value.
Now if I open an interactive python repl and do the following:
>>> import hmac>>> import hashlib>>> secret = "8f742231b10e8888abcd99yyyzzz85a5">>> ts = "1531420618">>> msg = "token=xyzz0WbapA4vBCDEFasx0q6G&team_id=T1DC2JH3J&team_domain=testteamnow&channel_id=G8PSS9T3V&channel_name=foobar&user_id=U2CERLKJA&user_name=roadrunner&command=%2Fwebhook-collect&text=&response_url=https%3A%2F%2Fhooks.slack.com%2Fcommands%2FT1DC2JH3J%2F397700885554%2F96rGlfmibIGlgcZRskXaIFfN&trigger_id=398738663015.47445629121.803a0bc887a14d10d2c447fce8b6703c">>> ref_signature = "v0=a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503">>> base = ":".join(["v0", ts, msg])>>> hashed = hmac.new(secret.encode(), base.encode(), hashlib.sha256)>>> hashed.hexdigest()>>> 'a2114d57b48eac39b9ad189dd8316235a7b4a8d21a10bd27519666489c69b503'
You will recognise the referenced link example. If I use the values from my django app with one of MY examples, it works within the repl but doesn't within the django app.
MY QUESTION: I believe this is caused by the self.raw.decode() encoding not being consistent with the printout I extracted to copy/paste in the repl. Has anyone encountered that issue and what is the fix? I tried a few random things with the urllib.parse library... How can I make sure that the request.body encoding is consistent with the example from flask with get_data() (as suggested by the doc in the link)?
UPDATE: I defined a custom parser:
class SlashParser(BaseParser):"""Parser for form data."""media_type = 'application/x-www-form-urlencoded'def parse(self, stream, media_type=None, parser_context=None): """ Parses the incoming bytestream as a URL encoded form, and returns the resulting QueryDict. """ parser_context = parser_context or {} request = parser_context.get('request') raw_data = stream.read() data = QueryDict(raw_data, encoding='utf-8') setattr(data, 'raw_body', raw_data) # setting a 'body' alike custom attr with raw POST content return data
To test based on this question and the raw_body in the custom parser generates the exact same hashed signature as the normal "body" but again, copy pasting in the repl to test outside the DRF works. Pretty sure it's an encoding problem but completely at loss...
Whenever I try to put special characters in the URL as follows in the Mozilla Firefox browser
or
Tomcat is returning HTTP Status 400 – Bad Requestand exception stack trace as follows.
Type Exception ReportMessage Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986Description The server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).Exceptionjava.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986 org.apache.coyote.http11.Http11InputBuffer.parseRequestLine(Http11InputBuffer.java:474) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:294) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748)Note The full stack trace of the root cause is available in the server logs.
Surprisingly same URL with special characters works properly in Google Chrome browser.
I have the following questions1) What changes should I do in the tomcat configuration file so that it shouldn't return 400 status code and exception stack trace?2) Why Mozilla Firefox is failing to encode the URL?
Note: I have already configured one common page for the status code of type 4XX in my application deployed at tomcat 9 but problem is tomcat is returning 400 bad request http status code in response at first place itself.
I have an angular 6 application in which i have some html audio player sections. I have encoded mp3
files via encodeURIComponent
and used in src
tag. Audio plays fine for below player
<audio controls> <source src="www.test.com/api/downloadaudio?filename=test.mp3" type="audio/mp3"></audio>
For special symbols like #, & and + it was encoded as per encodeuricomponenet
and applied in source tag like below
File name - Test#1.mp3
File name - Test&2.mp3
File name - Test+1.mp3
<audio controls> <source src="www.test.com/api/downloadaudio?filename=test%231.mp3" type="audio/mp3"> <source src="www.test.com/api/downloadaudio?filename=test%262.mp3" type="audio/mp3"> <source src="www.test.com/api/downloadaudio?filename=test%2B3.mp3" type="audio/mp3">
At the API side(C#) it was not properly decoded. I get the below value in C# side
For 1,2 and 3 case-> test was the value I got for file name instead of test#1
,test&2
,test+3
Let me know what am doing wrong here...
Thanks,
Selva
Domácí - Mapa stránek - Soukromí - Links - Copyright © 2019 Cortex IT Ltd : Kontakt : admin @ cortexit.co.uk
Please note that by viewing our site you agree to our use of cookies (see Soukromí for details). You will only see this message once.