String interpolation does not work with some url - .net-5

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);

Related

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

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.

visit a Full URL with parameter value

I'm trying to make a bash script to send a GET requests
also I'm trying to provide a URL access after changing some parameter value
problem:
when I hover over the link it will only underline URL without parameters values(check the following screen)
when I entered the URL it will redirect me to:
http://testphp.vulnweb.com/artists.php?artist=
I'm wondering if there is a solution for this case or not
Thank you in advance
Note that it's the terminal and not your script that decides what's considered part of a URL for the purpose of selecting or clicking.
But what I can suggest is to Use the URL encoding for those special characters instead of explicit special characters:
%27 = Single Quote **'**
%3C = Less than **<**
%3E = More than **>**
For example
URL=http://testphp.vulnweb.com/artists.php?artist=
PARTIAL_URL=$URL%3Cscript%3E
That will be shown http://testphp.vulnweb.com/artists.php?artist=<script>
Maybe that would do the trick

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 split multi character in MVC3?

Hello everyone I'm trying to remove some characters in my text box
For example
In my textbox, user fill with this text as you can see at below
http://youtu.be/YBkPomr40pg
I want to remove these characters http://youtu.be/
So how can I do could you show me an example ?
Load the URL into a Uri and get the relative URL from it. http://msdn.microsoft.com/en-us/library/system.uri.aspx
See this question for how to get the relative URL: Getting the relative uri from the absolute uri
Note that you will get the URL with the starting /. You can trim that yoruself.
If you're saying that you are ONLY going to be getting youtube URLs and you want simply the youtube video ID, this same technique can work, but you mgiht be better off just using string split to split on '/' and getting the last item.
var parts = "http://youtu.be/YBkPomr40pg".Split("/".ToCharArray());
var id = parts[parts.Length - 1]; // just get the last one.

How to get the last word from a URL?

Is there a method to extract just the last word from the URL example below? I would like to be able to use this as a heading on a page, i.e the "Account" page.
I found that by using request.path it will give me the path without the root but I'm not sure how to get just the last path name.
/users/1234/account
Try:
request.path.split('/').last
If you want "Account" (instead of "account"), call the capitalize method on the result.
I am not familiar with Ruby, but you can try this approach.
Try string splitting request.path with '/' as the separator and take the last element from the resulting array
users/1234/account will be split to {'user', '1234', 'account'}
Even though this doesn't answer your question directly, I hope it gives you a start
URLs are a simple string consisting of a scheme showing how to connect to a site, the host where the resource is located, plus a path to that resource. You can use File.basename to get the last part of that path, just like we'd use on a file on our disk:
File.basename('/users/1234/account')
=> "account"
Suppose you have URL like https://www.google.com/user/lastword
If you want to store last word of the URL which is lastword in a variable then use the following and pass url as value to finalVal.
var getLastWordFromUrl = finalVal.split("/").last()

Resources