I'm trying to Post an OFX request to a bank server with RestSharp. I have succeeded in getting the desired transaction data from the server. However, when the request body exceeds 1024 bytes, an Expect header is being added to the request. Normally, I'd just avoid the complexity and try to keep the request under 1024 bytes, but that is unavoidable with a few of the queries I want to run.
Unfortunately, the server does not support expect headers and throws an error if it receives one. I have been unable so far to identify how to prevent RestSharp from adding the Expect header. The code I'm using looks roughly like what I have below:
var client = new RestClient("https://bankwebsite.com");
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/x-ofx");
request.AddHeader("Accept", "*/*, application/x-ofx");
request.AddParameter("application/x-ofx", ofxString, ParameterType.RequestBody);
var response = client.Execute(request);
Is there a setting I'm missing? Is this intrinsic to how these requests are formed? I'm relatively new to working with HTTP, so I'm most likely missing something. I've tried setting ServicePointManager.Expect100Continue to false, which didn't change any behavior.
Using HttpClient instead of RestSharp has caused huge issues, so I would like to avoid that if I can.
I'm using RestSharp 106.2.1 with .Net Core 2.0 on MacOS.
Related
I am trying to submit some data to an API using RestSharp in C# and it seems all of my parameters are added to the Headers collection - or that is just how they are catalogued in VS.
Here is my code
var client = new RestClient("https://api.com");
var request = new RestRequest("/recognize", Method.POST);
request.AddHeader("app_id", "");
request.AddHeader("app_key", "");
request.AddHeader("Content-Type", "multipart/form-data");
request.AddParameter("gallery_name", model.gallery_name);
request.AddParameter("image", model.image);
var response = client.Execute(request);
The error back from the API says the request is missing the gallery_name and image parameters, but looking into the request object they are there in the Headers collection.
I can make the call in Postman where the method is set to Post and the Body is set to form-data, along with 2 key/value pairs listed.
What am I doing wrong?
In your question you says "I can make the call in Postman ...".
Normally if the Postman request is successful then should the generated RestSharp code from Postman works, if there are no open issues in the RestSharp library that can cause your problem.
It's hard to debug and reproduce the problem from here, but I can give the following that you can check.
Check this:
If your Postman request works and is successful then check the Restsharp code generated by Postman:
Click on "code"
Choose in the combobox for "C# (RestSharp)"
Try the RestSharp code that you see in the window, if the code doesn't work check this URL for known issues.
On a sidenote: If you see a known issue that can cause your error, try to test with a previous version of RestSharp or an alpha version.
I hope you can now further investigate and debug the problem.
(I had similar problems and resolved it successful with this way of investigating and debugging)
http://www.jfrog.com/confluence/display/RTF/Artifactory+REST+API#ArtifactoryRESTAPI-CreateorReplaceRepositoryConfiguration
I am using the Create or Replace Repository Configuration call. However I am getting a 406 Error: Not Acceptable. Other PUT calls are working but do not return JSON. I believe JSON is the source of the error but have not been able to resolve or prove this.
I have added the code as below
RestClient Client = new RestClient(uriString);
RestRequest Request = new RestRequest(requestType);
Request.AddHeader("Authorization", "Basic " + credentials);
Request.AddHeader("Accept", "application/json");
I've seen threads where adding the header to accept JSON resolves the error but this has not worked for me.
A 406 HTTP status means that if a web server detects that the data it wants to return is not acceptable to the client, it returns a header containing the 406 error code.
The client can define the characteristics of the data it will accept back from the web server by using the accept headers.
In this case you declare the you would like to accept application/json:
Request.AddHeader("Accept", "application/json");
however the REST API method you are invoking is returning text/plain.
You should change the code to accept text/plain:
Request.AddHeader("Accept", "text/plain");
Wanted to add this for for future users stuck like me. I was having the same issue and tried the request with Postman and saw that the Content-Type was "application/hal+json" I was trying it with application/json without luck.
So running a test in postman I was able to figure out what the server needed exactly.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/hal+json"));
I faced the same 406 Error: Not Acceptable when trying to get JSON on another site. In my case I could see correct JSON when typed url in my browser address field. But downloading it from the same url via my C# code have been producing 406 Error.
None of the answers in this topic solved my problem directly. But at least they pointed out to me that's the point is HTTP headers.
So I googled that page:
https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending
and added all browser headers to my code, and voila! It started to work.
In my case it was enough to fill some data in user-agent header.
First, the Accept header states what the client is ready to get back, not what the client sends.
The header that states what the client sends is Content-Type.
Also, this method does not accept application/json. As clearly stated in the docs, it accepts one of the following:
application/vnd.org.jfrog.artifactory.repositories.LocalRepositoryConfiguration+json
application/vnd.org.jfrog.artifactory.repositories.RemoteRepositoryConfiguration+json
application/vnd.org.jfrog.artifactory.repositories.VirtualRepositoryConfiguration+json
I have a web service in a RESTful web server (java) which consumes media of type APPLICATION_FORM_URLENCODED and produces media of type MULTIPART_FORM_DATA. Now I'm working on a REST client (C#) and trying to use this web service. I'm using RestSharp as the REST client. My code goes as follows:
RestRequest request = new RestRequest("getDataFileChunkIS", Method.POST);
request.AddParameter("sessionId", sessionId);
request.AddParameter("dataFileId", dataFileId);
request.AddParameter("offset", offset);
request.AddParameter("chunkSize", chunkSize);
request.AddParameter("checksumFlag", checksumFlag);
RestClient client = new RestClient(url);
RestResponse response = (RestResponse)client.Execute(request);
But in this response I'm getting HTTP Status 406 - Not Acceptable. It says "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers." Maybe I'm doing it in a wrong way. So my question is that how can I execute this request whose response will contain MULTIPART_FORM_DATA ?
1) how can I execute this request whose response will contain MULTIPART_FORM_DATA?
request.AddHeader("Accept", "multipart/form-data")
2) how can I read this response header(contains JSON) using RestClient?
See answers to this question. Particularly the third one, which shows how to do it just with .NET 4.5 libraries.
You may need to implement IDeserializer to get access to the raw HttpResponse for consumption.
So I'm testing Neo4J in pure REST (no Neo4JClient) and I have this code:
var client = new RestClient("http://url");
string requestText = "{ \"query\" : \"start x = node(1) match path = (x--IsFriendOf) return path, IsFriendOf.name\", \"params\" : { }}";
var request = new RestRequest();
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
request.Resource = "/foo/bar";
request.AddHeader("Content-Length", requestText.Length.ToString());
request.AddHeader("Host", "ip:port");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Basic encoded64credentials");
request.AddBody(requestText);
IRestResponse response = client.Execute(request);
If I do the RAW request in Fiddler itself, I get the desired result,
But if I do it in a console application in .Net 4.5, I see this:
I created a rule in my firewall to allow incoming/outgoing requests for the console executable, and I deactivated IE protected mode, but still, no luck.
Do anyone have some idea about this issue?
There are a number of things wrong here.
The first thing to understand is that you're not actually seeing the request at all-- the request you're showing in this screenshot is IE downloading Compatibility View List information, not any request you've made yourself.
You should probably start by reading http://blogs.msdn.com/b/fiddler/archive/2011/09/14/fiddler-and-windows-8-metro-style-applications-https-and-private-network-capabilities.aspx to understand how Windows 8 / Windows Server 2012 have changed and what you need to do to capture their traffic in a local loopback proxy.
I can't believe what I'm writting, but apparently the error was produced by Fiddler itself, I will find out what are the inner mechanisms of Fiddler to interrupt my calls.
So if you are making an http request to a REST API that is not on port 80, and you are either using WireShark or Fiddler for example, the request never reaches the endpoint.
I don't know how to solve it, but I do know how to avoid it, and that is, close all traffic monitors on the server. In my case I had Wireshark and Fiddler4 opened to help me debug the http request content, but of course I could never realize what was happening because I was debugging with the tool that was provoking the error.
I'm using Fiddler4 in windows server 2012, with VS2012 and .Net Framework 4.5 if this is useful for anyone that has any hint on why that happens.
I'm narrowing in on an underlying problem related to two prior questions.
Basically, I've got a URL that when I fetch it manually (paste it into browser) works just fine, but when I run through some code (using the HttpWebRequest) has a different result.
The URL (example):
http://208.106.250.207:8192/announce?info_hash=-%CA8%C1%C9rDb%ADL%ED%B4%2A%15i%80Z%B8%F%C&peer_id=01234567890123456789&port=6881&uploaded=0&downloaded=0&left=0&compact=0&no_peer_id=0&event=started
The code:
String uri = BuildURI(); //Returns the above URL
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Proxy = new WebProxy();
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
... Parse the result (which is an error message from the server claiming the url is incorrect) ...
So, how can I GET from a server given a URL? I'm obviously doing something wrong here, but can't tell what.
Either a fix for my code, or an alternative approach that actually works would be fine. I'm not wed at all to the HttpWebRequest method.
I recommend you use Fiddler to trace both the "paste in web browser" call and the HttpWebRequest call.
Once traced you will be able to see any differences between them, whether they are differences in the request url, in the form headers, etc, etc.
It may actually be worth pasting the raw requests from both (obtained from Fiddler) here, if you can't see anything obvious.
Well, the only they might differ is in the HTTP headers that get transmitted. In particular the User-Agent.
Also, why are you using a WebProxy? That is not really necessary and it most likely is not used by your browser.
The rest of your code is fine.. Just make sure you set up the HTTP headers correctly. Check this link out:
I would suggest that you get yourself a copy of WireShark and examine the communication that happens between your browser and the server that you are trying to access. Doing so will be rather trivial using WireShark and it will show you the exact HTTP message that is being sent from the browser.
Then take a look at the communication that goes on between your C# application and the server (again using WireShark) and then compare the two to find out what exactly is different.
If the communication is a pure HTTP GET method (i.e. there is no HTTP message body involved), and the URL is correct then the only two things I could think of are:
make sure that your are send the right protocol (i.e. HTTP/1.0 or HTTP/1.1 or whatever it is that you should be sending)
make sure that you are sending all required HTTP headers correctly, and obviously that you are not sending any HTTP headers that you shouldn't be sending.
There could be something wrong with the URL. Instead of using a string, it's usually better to use an instance of System.Uri:
String url = BuildURI(); //Returns the above URL
Uri uri = new Uri(url);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Proxy = new WebProxy();
using (WebResponse resp = req.GetResponse()) {
using (Stream stream = resp.GetResponseStream()) {
// whatever
}
}
I think you need to see exactly what's flowing to your server in the HTTP request. Does sound likely that the headers are interestingly different.
You can introduce a some kind of debugging proxy between your request and the server (for example RAD has such a capability in the box).