I am trying to make a GET request with a body to a REST API - without success.
The API sits on top of Elastisearch, which requires a body for requests.
The body contains the filters and requests for specific fields for the request.
I have tried multiple different requests in Postman and they work fine. The body is forwarded and the results are filtered as expected.
The API is state run which means i have no control over it what so ever.
It refuses any request that is not GET (POST, PUT etc.).
I have tried with RestSharp, but it refuses to make a GET request with a body.
Error message is: "Http verb GET does not support body".
(i tried to get the C# code from Postman, but it throws the same exception).
I have also tried with HttpClient, HttpWebRequest and WebClient. Same error in all of them.
To try a new platform i created a PowerShell script to run from C#, but it throws the same exception.
Is this just impossible to do in C#?
My only option now is to create e.g. a PHP (or other language) script that can query the API succefully, and then access this script from C#. But it seems like quite a workaround only because of the GET/body problem.
Any advice or ressources you could provide or refer me to would be greatly appreciated! Thank you.
Btw. i have seen a lot of posts on Stackoverflow on this problem. Many of them are quite old so i am hoping that perhaps something has changed. Also many of them end up with a solution where you use POST instead of GET - which is not a option for me.
You can use NEST (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/nest.html) - client for elastic for .net
Example code:
var elasticUrls = new Uri[]{new Uri("http://localhost:9200") };
var connect = new ConnectionSettings(
new SniffingConnectionPool(elasticUrls, true, null),
(builtin, settings) =>
new JsonNetSerializer(
builtin,
settings,
null,
null,
new JsonConverter[] {new StringEnumConverter()})
);
var client = new ElasticClient(connect);
var searchResult = await client.SearchAsync<ElasticModel>(
d => d
.Query(q => q.Bool(qd => qd
.Must(m => m.MatchAll())
))
.From(0)
.Size(10)
).ConfigureAwait(false);
/// <summary>
/// Some model from Elastic
/// </summary>
class ElasticModel
{
public int Id { get; set; }
}
Using RestSharp, in the request.AddParameter use ParameterType.GetOrPost instead of ParameterType.RequestBody
request.AddParameter("application/json", "things", ParameterType.GetOrPost);
Related
I am trying to use the Nest client to consume an Elasticsearch instance.
However, the /_search endpoint is in fact /search.
Is there any possible way to change the behavior of the client to reflect this change?
I have tried looking into the source code but just can't figure out a way this can be done.
Disclaimer: I have no control over the ES instance, neither I know if there is some sort of proxy in the middle that alters the /_search into /search.
Thanks in advance.
/search is not an API endpoint in Elasticsearch. It is not possible to change the API endpoints within the client, without changing the API specs from which it is generated and recompiling.
You can use the low level client's DoRequest and DoRequestAsync methods to call
a non-standard API
var client = new ElasticClient();
var request = new SearchRequest<LogMessage>
{
Query = new MatchQuery
{
Field = Infer.Field<LogMessage>(f => f.Level),
Query = "warning"
}
};
var response = client.LowLevel.DoRequest<SearchResponse<LogMessage>>(
Elasticsearch.Net.HttpMethod.POST,
"/search",
PostData.Serializable<ISearchRequest>(request)
);
My boss asked how long it would take to build a client to access a web service that will send and receive some basic data and embedded documents. Just starting playing with it to see what's involved. I have been doing web and desktop development for about 20 years but have literally never touched a web service so with that I'm at the extreme newb level.
So far I used the wsdl to create the ServiceReference1 and I can see the methods in intellisense but I don't have the first clue where to start with calling the methods, passing parameters and consuming the response. I feel stupid because I'm sure it's pretty simple but just flailing at the code and looking for on point examples has gotten me nowhere. Usually I can find something through google in minutes that is exactly on point but not having luck here. Would appreciate a push in the right direction.
So basic questions. Proper way to make the calls. How and where to land the returned data. How to add parameters.
Here is my first attempt. This gets a simple list and has no parameters. The result in fiddler returns data but there is a runtime type mismatch error which I think is caused by some stray characters leading the response which appear to be caused by chucking, what ever that is. The response starts with 1ffs every time then contains the remainder of the xml. Secondarily I need to get the list into a dataset or some other container but I was hoping to just be able to step into the code and see a result
ServiceReference1.FilingInfoClient webservice = new FilingInfoClient();
ServiceReference1.courtListRequest cr = new ServiceReference1.courtListRequest();
ServiceReference1.courtListResponse lr = new ServiceReference1.courtListResponse();
lr = webservice .getCourtList(cr);
This is essentially the same but takes a date param. When I run this fiddler shows the parameter is not being sent. No other errors but I'm sure only because it exploded immediately.
ServiceReference1.FilingInfoClient webservice = new FilingInfoClient();
ServiceReference1.messageListRequest mr = new ServiceReference1.messageListRequest();
ServiceReference1.MessageListResponse mlr = new ServiceReference1.MessageListResponse();
mr.latestMessagePullTimestamp = DateTime.Now.AddDays(-5);
mr.endTimestamp = DateTime.Now;
mlr.latestMessagePullTimestamp = DateTime.Now;
mlr = webservice.getMessageList(mr);
This is the info provided by the web service host
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn1="urn:green:partner:ws:schema:FilingInfo">
<x:Header/>
<x:Body>
<urn1:getcourtList>
<urn1:courtListRequest/>
</urn1:getcourtList>
</x:Body>
</x:Envelope>
<x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn1="urn:green:partner:ws:schema:FilingInfo">
<x:Header/>
<x:Body>
<urn1:getMessageList>
<urn1:messageListRequest>
<urn1:latestMessagePullTimestamp>?</urn1:latestMessagePullTimestamp>
</urn1:messageListRequest>
</urn1:getMessageList>
</x:Body>
</x:Envelope>
we've got request and response pairs for each operation in the webservice. think like request => input, response => output, operation => method.
the webservice is an API. things that consume APIs are clients. the WSDL describes the API's operations and their requests and responses. tools like visual studio know how to read WSDLs and build C# code to perform those (SOAP) operations under-the-hood. this is the client (here FilingInfoClient). visual studio'll also generate classes representing each request and response.
this allows for a familiar programming experience. you call a method, give it some input, and it returns some output.
using (var client = new FilingInfoClient())
{
var request = new courtListRequest
{
//TODO fill in relevant properties
};
var response = client.getCourtList(request);
}
I am using the Nuget package Microsoft.Azure.CognitiveServices.Vision.CustomVision.Prediction
I have created a Custom Vision application in the Custom Vision portal and obtained API keys and a project ID.
Whenever I try to make a request to the API, I always get the following exception thrown:
HttpOperationException: Operation returned an invalid status code
'NotFound'
Here is my code:
HttpClient httpClient = new HttpClient();
CustomVisionPredictionClient customVisionPredictionClient = new CustomVisionPredictionClient(httpClient, false)
{
ApiKey = PredictionKey,
Endpoint = PredictionEndpoint,
};
var result = customVisionPredictionClient.PredictImageAsync(CUSTOM_VISION_PROJECT_GUID, imageData);
I have tried several different endpoints:
https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction
https://southcentralus.api.cognitive.microsoft.com/customvision/Prediction/v1.0
https://southcentralus.api.cognitive.microsoft.com/customvision/v1.1/Prediction
though on the portal the listed one is the first of the list. I have also succesfuly exported my app on Azure, which gives me the second endpoint in the list but with no more success.
I have also set a default iteration as suggested in a similar issue that I found ( CustomVision: Operation returned an invalid status code: 'NotFound' ).
I have tried this sample https://github.com/Microsoft/Cognitive-CustomVision-Windows/tree/master/Samples/CustomVision.Sample which uses a deprecated windows client, to at least ensure my project information are correct and I was able to access the API.
Any insight would be appreciated
For the .NET client SDK, you need to specify the base endpoint URL without the version or the rest of the path. The version is automatically added by the client SDK. In other words, you'll want (assuming SouthCentralUS is your region):
PreditionEndpoint = "https://southcentralus.api.cognitive.microsoft.com";
CustomVisionPredictionClient customVisionPredictionClient = new CustomVisionPredictionClient()
{
ApiKey = PredictionKey,
Endpoint = PredictionEndpoint,
};
var result = customVisionPredictionClient.PredictImageAsync(CUSTOM_VISION_PROJECT_GUID, imageData);
As an aside, note that unless you want to fine-tune the behavior, you don't need to pass in an HttpClient object to CustomVisionPredictionClient constructor.
If you need more sample code, please take a look at the QuickStart.
How to use the Prediction API
If you have an image URL:
your endpoint would be something like this
https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{Project-GUID}/url?iterationId={Iteration-ID}
Set Prediction-Key Header to : predictionId
Set Content-Type Header to : application/json
Set Body to : {"Url": "https://example.com/image.png"}
Or If you have an image file:
Endpoint would be like
https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/{ProjectGuid}/image?iterationId={Iteration-Id}
Set Prediction-Key Header to : Predcition-key
Set Content-Type Header to : application/octet-stream
Set Body to : <image file>
Remember, you can mark an iteration as Default so you can send data to it without specifying an iteration id. You can then change which iteration your app is pointing to without having to update your app.
Check my other answer on the similar issue using python
Python custom vision predictor fails
Hope it helps.
I'm trying to perform some actions on a remote server using the XML-RPC .NET library and C#. I have no prior experience using this protocol but most examples seemed pretty straight forward. But the server I'm trying to communicate with seems to parse commands slightly different than most examples I've seen.
All calls are made using a 'perform_actions' function and it expects a list of action(s) along side with it as parameter. Fortunately there's a pretty decent documentation with some code samples included but these examples are done in Ruby/Perl with which I have no experience. I've tried translating these to C# with which I believe I'm on the right path but I'm consistently getting the error"Server returned a fault exception: [400] Invalid request: expected list of actions."
My current code
[XmlRpcUrl("https://DOMAIN/admin/rpc")]
public interface iFace : IXmlRpcProxy
{
[XmlRpcMethod("perform_actions")]
XmlRpcStruct[] perform_actions(XmlRpcStruct struc);
}
public void GetData()
{
XmlRpcStruct actions = new XmlRpcStruct();
actions.Add("name", "registrations.accounts.list");
iFace proxy = XmlRpcProxyGen.Create<iFace>();
proxy.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
XmlRpcStruct[] response = proxy.perform_actions(actions);
}
And here is a Ruby example from the API documentation which I was trying to replicate which is functional
require 'xmlrpc/client'
url = 'https://user:passwd#qmanage.example.com/admin/rpc'
c = XMLRPC::Client.new_from_uri(url)
# Call the action to list the access groups.
ags = c.call('perform_actions', [{
'name' => 'network.accessgroups.list',
'args' => {}
}])
The server doesn't really seem to recognize the XmlRpcStruct I'm sending as the error seems to complain about not receiving a list of actions. (I receive the same error if I send no parameter). However if I change the XmlRpcStruct to a regular string array it will complain about expecting a struct instead so the data isn't ignored entirely.
Is anyone able to help me in the right direction with my problem or does anyone know why this error is returned?
Finally managed to figure out my dilemma. Seems I had to pass an array of XmlRpcStruct's rather than just singular XmlRpcStruct, the following solved my problem:
XmlRpcStruct[] actions = new XmlRpcStruct[1];
XmlRpcStruct action = new XmlRpcStruct();
action.Add("name", "registrations.accounts.list");
actions[0] = action;
I just passed actions as parameter to the perform_actions function.
Good afternoon,
I'm using version 5.4.1 of the Facebook C# SDK. I should note that I am using the source code directly rather than the DLLs (in case this makes any difference).
So the fact that filter attributes are setup is awesome (thank you dev team :).
My issue is occurring during the initial request (before I get to using verifying the GET response from Facebook)
Here is my initial request:
dynamic result = fb.Post(
string.Format("/{0}/subscriptions",
FacebookApplication.Current.AppId),
new Dictionary<string, object>
{
{"object", "user"},
{"fields", "friends"},
{
"callback_url",
"http://localhost:16917/subscription/verify"
},
{
"verify_token",
"77FB802F-1147-48F0-BB0F-E4E9BC1FBCFC"
}
});
I'm finding that an exception is internally being thrown and via Fiddler I'm seeing that the request is never going out. The exception is:
$exception {"(OAuthException) (#15) This method must be called with an app access_token."} System.Exception {Facebook.FacebookOAuthException}
I initially thought this may be related to Facebook.FacebookClient's PrepareRequest method:
if (httpMethod == HttpMethod.Get)
{
// for GET, all parameters goes as querystrings
input = null;
queryString.Append(FacebookUtils.ToJsonQueryString(parameters));
}
else
{
if (parameters.ContainsKey("access_token"))
{
queryString.AppendFormat("access_token={0}", parameters["access_token"]);
parameters.Remove("access_token");
}
}
but commenting out the line parameters.Remove("access_token"); made no difference.
Any help would be greatly appreciated!
What access_token are you using?
Are you using a User's access token, or an App access token?
If you are using an User's access token take a look here http://developers.facebook.com/docs/authentication/#app-login on how to get an App access token.
You will need to use the app access_token.
You can easily create an app access_token using the following constructor.
var fb = new FacebookClient("appid", "appsecret");
dynamic result = fb.Post( .... );