I am able to curl ewayAPI following examples for "transparent redirect" in their API.
However converting the exact same curl request into C# code I get the following error codes: V6011 and V6047
I have checked and rechecked and even used wireshark to make sure that what I am sending through C# has the same package content as what I am sending through terminal.
I think there is some encoding issue or setting in IIS on my dev box that is distorting the curl request to eway which doesn't happen in terminal.
Does anyone know if there is a setting I have to select in IIS to ensure the curl requests get through?
I found the answer, it turns out that eway is very touchy about the request headers.
request.Method = Method.POST;
request.AddHeader("Accept", "application/json");
request.Parameters.Clear();
request.AddParameter("application/json", strJSONContent, ParameterType.RequestBody);
Adding the header as well as the content type for "application/json" solved it using restsharp.
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)
I am trying to get some data downloaded from a URL using HttpWebRequest from a C# console applicaiton. In browser and Postman, it works fine but not from the applicaiton - it does not return the expected data. Using Fiddler, I inspected the request and I figured out that the request initially sent is actually gets redirected thrice (I see three http 302 in Fiddler before the final Http 200 response) and eventually returns the data. However, from my C# console application I get only the final response - the HttpWebResponse status always gives"OK" (200).
I noticed in fiddler that the http 302 returns few cookies and the subsequent request sends the cookies in its header. This is handled correctly in browser/postman but I am not able to do this in my application. Any help will be highly appreciated.
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
I believe that you are looking for AllowAutoRedirect property of HttpWebRequest class. Setting it to false should do the thing:
HttpWebRequest request = new HttpWebRequest(someUri);
request.AllowAutoRedirect = false;
You can read more info about HttpWebRequest here.
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 recently started using libcurl.net with one of my projects as a replacement to the HttpWebRequest and HttpWebResponse classes. The reason I chose to use libcurl.net instead of the managed classes is that libcurl.net mimics the behavior of cURL from PHP and I was porting over some code from PHP. I attempted to use the built-in managed classes, but the CookieContainer class was not capturing all of the cookies correctly from the website that I was trying to capture cookies from. I may end up going back to the managed classes if I can figure out how to capture the cookies correctly.
My PHP script works perfectly fine in capturing cookies so I ported most of the cURL functionality using libcurl.net to my C# project. The problem I'm having is when I have to send more than one request header with the CURLOPT_HTTPHEADER cURL option and I have to use an Slist datatype to pass in more than one header like so:
Slist headers = new Slist();
headers.Append("Content-Type: application/x-www-form-urlencoded");
headers.Append("X-Requested-With: XMLHttpRequest");
easy.SetOpt(CURLoption.CURLOPT_HTTPHEADER, headers);
I sometimes have to fake an AJAX request but it does not seem to pass the X-Requested-With: XMLHttpRequest header with the request as the website I'm scraping does not return any results for these "fake" AJAX requests. If I set the CURLOPT_HTTPHEADER do I need to set the Content-Type header or is that always defaulted to Content-Type: application/x-www-form-urlencoded?
It turns out that I was adding multiple headers correctly. I simply made an Slist object and added my headers to the request using the CURLOPT_HTTPHEADER option. In this way, one can "fake" AJAX requests or any other type of request sent by a web browser. The problem was that I wasn't sending the correct POST data with my request.
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.