I've been here for 2 days now driving me nuts.
All I want to do it call a webservice at:
https://use-land-property-data.service.gov.uk/api/v1/datasets
Which returns some JSON object.
It requires the "Authorization" header to be set with an API Key that I have.
I've tried it in POSTMAN and it works.
However trying to get a Webclient or Httpclient version working is currently beyond me. I've tried countless examples here on SO. None return the same responses as POSTMAN. All return "Request Rejected"
e.g.
Using client = New HttpClient()
client.DefaultRequestHeaders.Add("Authorization", "MYKEY")
Dim response = Await client.GetStringAsync("https://use-land-property-data.service.gov.uk/api/v1/datasets")
Return response
End Using
what is the equivalent in httpclient to replicate the postman Authorization header?
Try:
httpClient.DefaultRequestHeaders.Authorization = New AuthenticationHeaderValue("Bearer", "Your Key")
Related
I'm trying to upload crash manually to HockeyApp using public API. When calling the api link using Postman and uploading crash.log file it works fine but when I try to do the same from C# code I get 404 error.
Here is my code:
string log = ""; //log content
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("*/*"));
var content = new MultipartFormDataContent();
var stringContent = new StringContent(log);
stringContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("text/plain");
content.Add(stringContent, "log", "crash.log");
var response = await this.client.PostAsync("https://rink.hockeyapp.net/api/2/apps/[APP_ID]/crashes/upload", content);
}
I was using WireShark to analyse the request that Postman is sending and tried to make mine look exactly the same. The only difference I see is that request from C# code has filename* field in Content-Disposition for the attachment while the one from Postman doesn't:
Content-Disposition: form-data; name="log"; filename="crash.log"; filename*=utf-8''%22crash.log%22
It might be worth mentioning that the code is written in portable library in Xamarin project.
Following #Lukas Spieß sugestion I asked the question on HockeyApp support. Apparently they don't handle quotes in the boundary header. The one thing I missed comparing Postman request and mine.
Here is the solution:
var contentTypeString = content.Headers.ContentType.ToString().Replace("\"", "");
content.Headers.Remove("Content-Type");
content.Headers.TryAddWithoutValidation("Content-Type", contentTypeString);
I have a web api that is working great in test using an access token / bearer authentication. I authenticate and make requests using HttpClient. Easy.
Here is the basic web client setup. The base address is a constant that I change when moving to production.
public static HttpClient GetClient()
{
HttpClient Client = new HttpClient();
Client.BaseAddress = new Uri(Ics2Constants.ICS2APIBaseAddress);
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return Client;
}
I build the token request login info like so:
var values = new Dictionary<string, string>();
values.Add("grant_type", "password");
values.Add("username", "niceUser");
values.Add("password", "NiCePaSsWord");
var loginContent = new FormUrlEncodedContent(values);
And then I make the request for the access token:
var loginResponse = await client.PostAsync("/Token", loginContent);
In test mode, perfect. I can get my access token, pass it back on subsequent requests. All is good.
When I move to production. I get a bad request 400 on the request for access token. I do have the base address right because if I take off the authorize attribute I can get data back.
Something is different about the request for access token in production, but I have no clue what to change.
Well, the answer ended up being two part:
1) Issue with the web host. They had a corruption on their end.
2) After they fixed their issue I still received a 404 (not found)... so I had to take out the "/" in my PostAsync. So the new line looks like so:
var loginResponse = await client.PostAsync("Token", loginContent);
It worked in debug on the local side with the "/", but the production side was not happy.
I'm up and working now. :)
I'm trying to send HEAD request to server to get a file length from Content-Length header, but I always get 'Not Acceptable' in response when I'm using RestSharp. Id I create request with simple .NET WebRequest it work good.
I tried to clear all header and other stuff from request and client, but had no success
Is it possible to do this request with RestSharp and how?
Thanks in advance
Try this
var request = new RestRequest("/resource", Method.HEAD);
I'm using the following approach if someone is interested:
using RestSharp;
var client = new RestClient("/resource");
var request = new RestRequest();
var response = client.Head(request);
Solved the problem with
restRequest.Parameters.Clear();
restRequest.AddHeader("Accept", "*/*");
I've read and re-read stackoverflow and google searches in general and I just can't seem to find a solution to my issue. I'm positive it is my ignorance.
I am trying to reproduce a cUrl call in .Net (c# specifically) and am having a devil of a time wading through and figuring out why it isn't working. this cUrl call uses a token and the username is not required when I do so.
The following cUrl call works as expected when called from the command line. The API token has been changed to protect the innocent.
curl -u x:8cb60a319c71be3356da2ea6d7c7650b -H "Content-Type: application/json" https://deskapi.gotoassist.com/v1/incidents.json
I have tried the following:
WebRequestHandler webHandler = new WebRequestHandler();
webHandler.Credentials = new System.Net.NetworkCredential("x", "8cb60a319c71be3356da2ea6d7c7650b");
HttpClient client = new HttpClient(webHandler);
client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("incidents.json");
if (response.IsSuccessStatusCode)
{
Console.Write("Success!");
}
The response gives a "Bad Request". The actual requestmessage is:
response.RequestMessage {Method: GET, RequestUri: 'https://deskapi.gotoassist.com/v1/incidents.json', Version: 1.1, Content: , Headers:
{
Accept: application/json
}}
If that helps any.
Based on other stackoverflow examples I also tried:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://deskapi.gotoassist.com/v1/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("8cb60a319c71be3356da2ea6d7c7650b");
HttpResponseMessage response = await client.GetAsync("incidents.json");
if (response.IsSuccessStatusCode)
{
Console.Write("Success!");
}
Console.Write(response.RequestMessage.ToString());
With another "Bad Request". The message is similar:
response.RequestMessage {Method: GET, RequestUri: 'https://deskapi.gotoassist.com/v1/incidents.json', Version: 1.1, Content: , Headers:
{
Accept: application/json
Authorization: 8cb60a319c71be3356da2ea6d7c7650b
}} System.Net.Http.HttpRequestMessage
The actual uri looks correct to me. The content type looks correct to me. The only thing I can't find a definitive example to do is send that "x:8cb60a319c71be3356da2ea6d7c7650b" other than the ways I have tried it.
Any thoughts? This is my first time trying to do anything like this so hopefully I'm just doing some ignorant something-something. I've tried to follow example after example but nothing gets me an successful call. Thank you in advance for helping.
The cUrl command uses the Content-Type header which is only intended for when you are sending a body e.g. with PUT or POST. It is not the same header as Accept. Accept is the correct header to send with a GET request.
When I try hitting the server without any auth header, I get back a 400 with the message "Unknown OAuth signature method". The response for missing auth should really be a 401 and a www-authenticate header that tells us what kind of scheme to use.
Assuming that the server is actually looking for a OAuth2 bearer token, then you might want to try,
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer","8cb60a319c71be3356da2ea6d7c7650b");
Debug your code using Fiddler
By using Fiddler you will exactly know what is happenning during these calls,
Run it against CURL command line
Run it against your code
Basically you have nothing to do, just install it, run it and do some Internet activity. You will then see what's being intercepted.
To fix your problem, look at the Inspectors/Raw tab and compare them.
Here's an example of an issue I've had and what I found out that was missing with Fiddler :
Equivalent of "curl -F" parameter for System.Net.Http.MultipartFormDataContent?
I am trying to send a HTTP GET request to a service secured with BASIC authentication and https. If I use the RESTClient Firefox plugin to do so there is no problem. I am defining the basic-header and sending the GET to the url and I am getting the answer (data in json).
Now I am working on a Windows Store App in C# which is meant to consume the service. I enabled all required capabilities in the manifest and wrote the following method:
private async void HttpRequest()
{
string basic = "Basic ...........";
Uri testuri = new Uri(#"https://...Servlet");
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", basic);
Task<HttpResponseMessage> response = client.GetAsync(testuri);
var text = await response;
var message = text.RequestMessage;
}
I tried out many different possibilites like getting the response-string but everything lead to an 401 Status Code answer from the Server.
I looked at many similar problems and my understanding of the communication is the following: Client request -> Server response with 401 -> Client sends Authorization header -> Server response with 200 (OK)
What I don't understand is why I am getting the 401 "Unauthorized" Status Code although I am sending the Authorization header right at the beginning. It would be interesting if someone knows how this is handled in the RESTClient.
The BASIC header is definetly correct I was comparing it with the one in the RESTClient.
It would be great if someone could help me with this.
Thanks in advance and kind regards,
Max
Was having a similar problem, i added a HttpClientHandler to HttpClient.
var httpClientHandler = new HttpClientHandler();
httpClientHandler.Credentials = new System.Net.NetworkCredential("","")
var httpClient = new HttpClient(httpClientHandler);
Credentials should be encoded, before adding to the header. I tested it in WPF app, It works...
string _auth = string.Format("{0}:{1}", "username", "password");
string _enc = Convert.ToBase64String(Encoding.UTF8.GetBytes(_auth));
string _basic = string.Format("{0} {1}", "Basic", _enc);
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization",_basic);