I need to update a record in CRM 2015 using web api in C#. However when processing, I got this error "Method not allowed" Status Code 405.
Here is some sample code.
HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("username", "password", "domain") });
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
HttpRequestMessage retrieveReq = new HttpRequestMessage(HttpMethod.Post, serviceUrl + "SalesOrderSet(guid'" + orderId + "')");
retrieveReq.Headers.Accept.Clear();
retrieveReq.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
retrieveReq.Headers.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-us"));
retrieveReq.Headers.Add(HttpRequestHeader.ContentType.ToString(), "application/json");
retrieveReq.Headers.Add("Prefer", "odata.include-annotations=\"*\"");
retrieveReq.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
retrieveReq.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
retrieveReq.Headers.Host = server_name;
retrieveReq.Headers.Add(HttpRequestHeader.ContentLength.ToString(), "117");
retrieveReq.Headers.Connection.Add("Keep-Alive");
retrieveReq.Headers.Pragma.Add(new NameValueHeaderValue("no-cache"));
var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("name", "value"));
HttpContent content = new FormUrlEncodedContent(postData);
retrieveReq.Content = content;
await client.SendAsync(retrieveReq).ContinueWith(
(postTask) =>
{
postTask.Result.EnsureSuccessStatusCode();
});
Any help?
Thanks in advance
Error 405 usually means you are trying to send a method different than the expected by the server (normally the HttpVerb is wrong). You are using POST so could you try with GET? HttpMessage.Get.
Edit: looks like update operations require a PATCH verb. Others will require a PUT, and deletions must use DELETE.
https://msdn.microsoft.com/en-us/library/mt607664.aspx
Related
When I try to do the code below, it just results in Invalid Content Type (with error number 612).
I'm trying to delete a lead id from a static list. I can add lead ids or get the static list leads fine.
The post and get calls I make are working fine, although the post calls I make seem to require the data right on the url string (as in $"{endpointURL}/rest/v1/lists/{listID}/leads.json?id={leadID}"; If I include the id as a json object, it fails too. This might be a clue to what I'm doing wrong with the delete call.
string url = $"{endpointURL}/rest/v1/lists/{listID}/leads.json?id={leadID}";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
client.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue("Bearer", _access_token);
HttpResponseMessage response = await client.DeleteAsync(url);
The response here always results in Invalid Content Type.
If I add this line before I do the deleteasync call, it gives me a different error before it even hits the deleteAsync call.
client.DefaultRequestHeaders.Add("Content-Type", "application/json");
Error is "Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."
Try using HttpRequestMessage in your code like this
string url = $"{endpointURL}/rest/";
HttpClient client = new HttpClient
{
BaseAddress = new Uri(url)
};
//I'm assuming you have leadID as an int parameter in the method signature
Dictionary<string, int> jsonValues = new Dictionary<string, int>();
jsonValues.Add("id", leadID);
//create an instance of an HttpRequestMessage() and pass in the api end route and HttpMethod
//along with the headers
HttpRequestMessage request = new HttpRequestMessage
(HttpMethod.Delete, $"v1/lists/{listID}") //<--I had to remove the leads.json part of the route... instead I'm going to take a leap of faith and hit this end point with the HttpMethod Delete and pass in a Id key value pair and encode it as application/json
{
Content = new StringContent(new JavaScriptSerializer().Serialize(jsonValues), Encoding.UTF8, "application/json")
};
request.Headers.Add("Bearer", _access_token);
//since we've already told the request what type of httpmethod we're using
//(in this case: HttpDelete)
//we could just use SendAsync and pass in the request as the argument
HttpResponseMessage response = await client.SendAsync(request);
The solution turned out to be a combination of a couple of suggestions.
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, data);
// The key part was the line below
request.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
if (!string.IsNullOrEmpty(_access_token))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _access_token);
}
HttpResponseMessage response = await client.SendAsync(request);
This worked for me.
Everything works fine on Postman with x-www-from-urlencoded and basic auth. Now trying to get my hands dirty, I just get status code 200 with nothing on mailgun, no logs recorded.
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://api.mailgun.net/v3");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("api", "key-withheld-till-a-verdict-has-passed");
var msg = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("from",
$"Excited User <mailgun#sandboxSOMEGUIDHERE.mailgun.org>"),
new KeyValuePair<string, string>("to","approved-to-receive#mailgun"),
new KeyValuePair<string, string>("subject", "Test Please Do Not Reply"),
new KeyValuePair<string, string>("text","Thanks for borrowing me your inbox")
};
var request = new HttpRequestMessage(HttpMethod.Post,
"sandboxSOMEGUIDHERE.mailgun.org/messages");
request.Content = new FormUrlEncodedContent(msg);
var response = await client.SendAsync(request);
// I get 200 status code
var result = await response.Content.ReadAsStringAsync();
//I get result = "Mailgun Magnificent API"
}
First, it turns out I was getting the BaseAddress not right. I had to place a slash at the end of the BaseAddress.
client.BaseAddress = new Uri("https://api.mailgun.net/v3/");
without the slash, I was posting to (note v3 is missing),
https://api.mailgun.net/sandboxSOMEGUIDHERE.mailgun.org/messages
After sorting that out, another problem emerged 401 - ANAUTHORIZED. And with the help of this SO answer I do,
var byteArray = new UTF8Encoding()
.GetBytes("api:key-withheld-till-a-verdict-has-passed");
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
But the reason why mailgun was responding with Ok still remain mysterious. To investigate further, I used Postman to post to,
https://api.mailgun.net/sandboxSOMEGUIDHERE.mailgun.org/messages
and to my surprise, Mailgun mysteriously responded with Ok.
I am attempting to use the PayPal RESTful API through Xamarin Forms and am running into trouble when I try and obtain the OAuth token. Here is the code I am running, and error occurs when on Android. The authentication header I have changed here so that my client id and secret are not openly available. This is the curl command I am basing this HTTP request on also: https://developer.paypal.com/docs/integration/direct/make-your-first-call/. Thank you.
var getToken = new HttpClient(new NativeMessageHandler());
getToken.BaseAddress = new Uri("https://api.sandbox.paypal.com/v1/oauth2/token");
getToken.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
getToken.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en_US"));
getToken.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("**client_id**", "**secret**");
getToken.DefaultRequestHeaders.TryAddWithoutValidation("content-type", "application/x-www-form-urlencoded");
OAuthDetails clientCredentials = new OAuthDetails("client_credentials");
HttpResponseMessage tokenResponse = await getToken.PostAsJsonAsync("/v1/oauth2/token", clientCredentials);
AccessResponse accessInfo = await tokenResponse.Content.ReadAsAsync<AccessResponse>();
The way to send a post request with application/x-www-form-urlencoded is like this: instead of TryAddWithoutValidation you need to do this and then include this as the content in the request instead of client credentials like I did previously.
var tokenContent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"
});
I have an application that uses the outlook REST API for creating events on the user's calendar. The creation of the event works perfectly, but once I tried to to exactly as this post indicates, I get 405 Method Not Allowed.
the error details are as follow:
{"error":{"code":"ErrorInvalidRequest","message":"The OData request is not supported."}}
here's a part of my code:
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, new Uri("https://outlook.office365.com/api/v1.0/me/events/"+meeting.OutlookEventId));
var auth = "Bearer " + token;
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", auth);
var converters = new List<JsonConverter>();
converters.Add(new MyStringEnumConverter());
var createResponse = #"{
'Location': {
'DisplayName': 'Your office'
}
}";
request.Content = new StringContent(createResponse);
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.SendAsync(request);
I have the user token sotred on the "token" variable, as well as the outlook event Id on the "meeting.OutlookEventId" variable.
Any ideas?
Thank you very much!
I feel like a total fool...
I was sending a POST when this request required a PATCH
I just replaced
HttpMethod.Post
for
new HttpMethod("PATCH")
I'm trying to use an HttpClient for a third-party service that requires basic HTTP authentication. I am using the AuthenticationHeaderValue. Here is what I've come up with so far:
HttpRequestMessage<RequestType> request =
new HttpRequestMessage<RequestType>(
new RequestType("third-party-vendor-action"),
MediaTypeHeaderValue.Parse("application/xml"));
request.Headers.Authorization = new AuthenticationHeaderValue(
"Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "username", "password"))));
var task = client.PostAsync(Uri, request.Content);
ResponseType response = task.ContinueWith(
t =>
{
return t.Result.Content.ReadAsAsync<ResponseType>();
}).Unwrap().Result;
It looks like the POST action works fine, but I don't get back the data I expect. Through some trial and error, and ultimately using Fiddler to sniff the raw traffic, I discovered the authorization header isn't being sent.
I've seen this, but I think I've got the authentication scheme specified as a part of the AuthenticationHeaderValue constructor.
Is there something I've missed?
Your code looks like it should work - I remember running into a similar problem setting the Authorization headers and solved by doing a Headers.Add() instead of setting it:
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "username", "password"))));
UPDATE:
It looks like when you do a request.Content, not all headers are being reflected in the content object. You can see this by inspecting request.Headers vs request.Content.Headers. One thing you might want to try is to use SendAsync instead of PostAsync. For example:
HttpRequestMessage<RequestType> request =
new HttpRequestMessage<RequestType>(
new RequestType("third-party-vendor-action"),
MediaTypeHeaderValue.Parse("application/xml"));
request.Headers.Authorization =
new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "username", "password"))));
request.Method = HttpMethod.Post;
request.RequestUri = Uri;
var task = client.SendAsync(request);
ResponseType response = task.ContinueWith(
t =>
{ return t.Result.Content.ReadAsAsync<ResponseType>(); })
.Unwrap().Result;
This would also work and you wouldn't have to deal with the base64 string conversions:
var handler = new HttpClientHandler();
handler.Credentials = new System.Net.NetworkCredential("username", "password");
var client = new HttpClient(handler);
...
Try setting the header on the client:
DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", userName, password))));
This works for me.
Also, consider that Redirect-Handler will clear the Authorization header if your request gets redirected.
So if you call an HTTP endpoint and it redirected to the HTTPS one, you will lose your authorization header.
request.Headers.Authorization = null;
Framework: .NET v6.0
Actually your problem is with PostAsync- you should use SendAsync. In your code - client.PostAsync(Uri, request.Content); sends only the content the request message headers are not included.
The proper way is:
HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = content
};
message.Headers.Authorization = new AuthenticationHeaderValue("Basic", credentials);
httpClient.SendAsync(message);