API problem with ruby on rails : resultat?Message=Authorization+has+been+denied+for+this+request - ruby

I have a problem with API connection. I see this message in the URL:
resultat?Message=Authorization+has+been+denied+for+this+request.
My code is the following :
def find_with(siren)
#request = HTTParty.get('https://api.datainfogreffe.fr/api/v1/Entreprise/notapme/performance/(siren)?millesime=2020?token=KEY')
#result = JSON.parse(#request.body)
end
I can connect with the command line and I have an API key.

I do not know the API you try to use. But the query parameters look weird to me. Usually, query parameters start with a ? and are separated with an &.
Additionally, it looks like you try to use string interpolation to add the value of siren to the URL. String interpolation only works in strings with are double quotes ("), single quotes (') do not support string interpolation. And string interpolation is done with #{...}, not with ordinary parentheses.
Therefore I suggest changing the method to:
def find_with(siren)
#request = HTTParty.get(
"https://api.datainfogreffe.fr/api/v1/Entreprise/notapme/performance/#{siren}?millesime=2020&token=KEY"
)
#result = JSON.parse(#request.body)
end
It is not clear from the question where siren is coming from and how a siren might look like. Please keep in mind that it has to be in a specific format to generate a valid URL. If siren is provided by the user and it is not guaranteed that it will be a valid query param then I would suggest using a proper URL builder to ensure proper URL encoding of the siren.

Related

String interpolation does not work with some url

I don't know how to replicate the problem here... I let you see the problem I have:
I must build an url and I am using string interpolation:
var url = $"{_baseAddress}/api​/freightcategories/​{categoryCode}";
Here the result:
As you can see last part of the url is white.... and if I call that url via HttpClient I get a 404 error...
I replaced the line above with this line:
var url = string.Format("{0}/api​/freightcategories/{1}", _baseAddress, categoryCode);
I get this:
Now if I call the url via HttpClient everything works correctly.
Any idea?
Thank you
Bas Paap reply on url in interpolated string breaks syntax coloring:
When using an interpolated string that contains an url followed by an
interpolated value, the syntax highlighting thinks the interpolated
value is part of the url and formats it as a link, up to and including
the first parameter.
I guess the url is analyzed this way: When an http or https sequence is encountered at the beginning of a string, the process of building the url starts, and values are appended to url until there is something unknown, like an interpolated value.
According to your case, _baseAddress might be interpolated first, which results in url having the following value: https://yoururl.whatever/api/freightcategories/​, which is a valid url and it is formatted properly, and I guess here the formatting stops because the next value is an interpolated one, which is unknown at the moment (_baseAddress value is know, but categoryCode not yet), so it stops the formatting and when categoryCode is appended, is simply does not care about formatting the url further. (This is how I think it works and it makes sense).
Here is a quick solution to build an url using string interpolation:
var url = "h" + $"{_baseAddress.Substring(1)}/api​/freightcategories/​{categoryCode}";
When h will meet ttps://yoururl.whatever/api...., the https is encountered at the beginning and the formatting starts. _baseAddress and categoryCode values are already a part of second string.
It is their issue and, as far as I know, Microsoft didn't solve it yet.
Also, you can try to specify an Uri instance rather that a string when sending a request, something like
var uri = new Uri($"{_baseAddress}/api​/freightcategories/​{categoryCode}");
client.GetAsync(uri);

Remove specific parts from url

Lets suppose I have a url like this:
https://www.youtube.com/watch/3e4345?v=rwmEkvPBG1s
What is the best and shorthest way to only get the 3e4345 part?
Sometimes it doesn't contain additional params in ?
I don't want to use any gems.
What I did was:
url = url.split('/watch/')
url = url[1].split('/')[0].split('?')[0]
Is there a better way? Thanks
possibly the safest and best one. use URI.
URI("https://www.youtube.com/watch/34345?v=rwmEkvPBG1s").path.split("/").last
For more refer How to extract URL parameters from a URL with Ruby or Rails?
You could do the following and using the match function to find a match based on a regular expression statement. The value at [1] is the first capture from the regular expression. I have included a breakdown from regexper.com to help illustrate what the expression is accomplishing.
You will notice parentheses around the \d+ which are what captures the digits out of the URL when it matches.
url.to_s.match(/\/watch\/(\d+).*$/)[1]
x = "https://www.youtube.com/watch/34345?v=rwmEkvPBG1s"
File.basename(URI(x).path)
=> "34345"

Jmeter - How Can I Replace a string and resend it?

I'm trying to create a script that will take a URL out of a response and send it out again.
Using the regular expression extractor I've succeeded in taking the wanted URL, but it holds "&" so naturally when sending it out the request fails.
Example:
GET http://[ia-test01.inner-active.mobi:8080/simpleM2M/ClientUpdateStatus?cn=WS2006&v=2_1_0-iOS-2_0_3_7&ci=99999&s=3852719769860497476&cip=113-170-93-111&po=642&re=1&lt=0&cc=VN&acp=&pcp=]/
I'm trying to replace the "&" with a "&".
I've tried: ${__javaScript(${url}.replace("&","&"))}
But it did not work. I've tried the regex function as well- the same.
I'm not sure the IP field in the request supports the us e of functions.
I'm currently trying to use the beanshell post-processor. But I'm pretty sure there is a simpler solution I'm missing.
Not sure what you're trying to get by replacing & with & however will try to respond.
First of all: given multiple & instances you need to use replaceall function, not replace
Second: replace / replaceall functions take a RegEx as parameter, so you'll need to escape your &
If you're trying to substitute URL Path in realtime, you'll need Beanshell Pre Processor, not the Post Processor
Sample Beanshell Pre-Processor code
import java.net.URL;
URL myURL = sampler.getUrl();
String path = myURL.getPath();
String path_replaced = path.replaceAll("\\&", "&");
vars.put("NEW_PATH", path_replaced);
After that put ${NEW_PATH} to "Path:" section of your HTTP Request.
Hope this helps.
Solution with less code:
Install the Custom JMeter Functions plugin
Use the following syntax
${__strReplace(ImAGoodBoy,Good,Bad,replaceVar)}
‘ImAGoodBoy’ is a string in which replacement will take place
‘Good’ is a substring to be replaced
‘Bad’ is the replacement string
‘replaceVar’ is a variable to save result string
Refer this URL for more info!
Thank a lot. However, i see from a recent experience that to replace a character that is actually a RegExp special character, like \ " ( ) etc, you need to put 3 backslashes and not 1, not 2. This is weird.
so you write
var res = str.replaceAll("\\\\u003c", "<");
to replace \u003c with <

How to return file path without url link?

I have
http://foobar.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg
How do I return
uploads/users/15/photos/12/foo.jpg
It is better to use the URI parsing that is part of the Ruby standard library
than to experiment with some regular expression that may or may not take every
possible special case into account.
require 'uri'
url = "http://foo.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg"
path = URI.parse(url).path
# => "/uploads/users/15/photos/12/foo.jpg"
path[1..-1]
# => "uploads/users/15/photos/12/foo.jpg"
No need to reinvent the wheel.
"http://foobar.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg".sub("http://foobar.s3.amazonaws.com/","")
would be an explicit version, in which you substitute the homepage-part with an empty string.
For a more universal approach I would recommend a regular expression, similar to this one:
string = "http://foobar.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg"
string.sub(/(http:\/\/)*.*?\.\w{2,3}\//,"")
If it's needed, I could explain the regular expression.
link = "http://foobar.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg"
path = link.match /\/\/[^\/]*\/(.*)/
path[1]
#=> "uploads/users/15/photos/12/foo.jpg"
Someone recommended this approach as well:
URI.parse(URI.escape('http://foobar.s3.amazonaws.com/uploads/users/15/photos/12/foo.jpg')).path[1..-1]
Are there any disadvantages using something like this versus a regexp approach?
The cheap answer is to just strip everything before the first single /.
Better answers are "How do I process a URL in ruby to extract the component parts (scheme, username, password, host, etc)?" and "Remove subdomain from string in ruby".

what does the empty regex match in ruby?

following a RoR security tutorial (here), i wrote something along the lines of
##private_re = //
def secure?
action_name =~ ##private_re
end
the idea is that in the base case, this shouldn't match anything, and return nil. problem is that it doesn't. i've worked around for the time being by using a nonsensical string, but i'd like to know the answer.
The empty regular expression successfully matches every string.
Examples of regular expressions that will always fail to match:
/(?=a)b/
/\Zx\A/
/[^\s\S]/
It is meant to not change the behavior of the controller in any way, as // will match every string.
The idea is that ##private is meant to be set in the controller to match things you DO want to be private. Thus, that code is meant to do nothing, but when combined with
##private = /.../ in the controller, gives you a nice privacy mechanism.

Resources