Ok, your key is valid, yep (not a good idea to make that public btw…).
Can you print the full URL, passed parameters and headers? It needs Content-Type: application/json and well, the POST request actually has to post a JSON object (not sure if your lib does that automatically from that string you pass). That shouldn’t result in this API error, but the rest actually looks fine to me.
We can’t help if you don’t give us one full exact HTTP request that fails: full URL, POST parameters and headers. Please print them in your debug window and paste them here (minus the Authorization header pls).
Paste. Please. This is not a nice way to request free support… And READ!! Your content-type is text/plain! If you use our POST endpoint, you have to POST a JSON, not a string! That’s what I was telling you here:
Can you print the full URL, passed parameters and headers? It needs Content-Type: application/json and well, the POST request actually has to post a JSON object (not sure if your lib does that automatically from that string you pass). That shouldn’t result in this API error, but the rest actually looks fine to me.
This is C# i suppose? Due to limited human resources we cannot test every code example. They are generated automatically from a template which might not be working for C#.
The problem should be with either the AuthenticationHeaderValue or the TryAddWithoutValidation part.
If you get this to work, you might want to provide us with your solution, so other people won’t run into the same issue
I still don’t see any POST parameters… I don’t see how I can help you… It works for everyone else, so I’m pretty sure that’s a basic programming mistake on your side and we can’t fix that for you.
No idea… Please format the code, this is markdown. It’s painful to look at… Unfortunately that’s not only applying to the formatting…
No idea about C#, but this looks wrong: Content: System.Net.Http.StringContent as the content for a POST request. Why a string? Compare with
There it’s a Content: System.Net.Http.ObjectContent, which makes a whole lot more sense. But like I said, no idea about C#… Please post a question on stack overflow, this is not a an issue with our software, but your code…
private async void buttonTestOpenRouteService_ClickAsync(object sender, EventArgs e)
{
//Common testing requirement. If you are consuming an API in a sandbox/test region, uncomment this line of code ONLY for non production uses.
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
//Be sure to run "Install-Package Microsoft.Net.Http" from your nuget command line.
var baseAddress = new Uri("https://api.openrouteservice.org");
using (var httpClient = new HttpClient { BaseAddress = baseAddress })
{
httpClient.DefaultRequestHeaders.Clear();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json; charset=utf-8");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "5b3 .. c5");
using (var content = new StringContent("{\"coordinates\":[[8.678,49.551],[8.502,49.531]],\"attributes\":[\"avgspeed\",\"percentage\",\"detourfactor\"],\"elevation\":\"true\",\"extra_info\":[\"suitability\",\"surface\",\"waycategory\",\"waytype\",\"traildifficulty\",\"osmid\",\"countryinfo\"],\"instructions_format\":\"html\",\"language\":\"de\",\"preference\":\"recommended\"}", Encoding.UTF8, "application/json"))
{
using (var response = await httpClient.PostAsync("/v2/directions/cycling-regular/json", content))
{
string responseData = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject(responseData);
}
}
}
}
And inside the designer ( it’s a winforms application):
this.buttonTestOpenRouteService.Click += new System.EventHandler(this.buttonTestOpenRouteService_ClickAsync);
Some update on the template: I did add some of your changes, but the escaping of the backslashes sadly didn’t work. I added a comment, to escape quotes.
In a bit more detail: We use a highlighting plugin for the different code templates for readability. Before the template is passed, the quotes actually are escaped. But after beautifying it, escaped quotes are replaced by normal ones There is a setting to disable this which had no effect.
To further look into this is not really worth the outcome right now.
I know it should be a one click running template, but it has to stay like this for now.
We are using the vue-markdown component to render the templates and PrismJS for highlighting if someone wants to investigate this.