Using RestSharp to submit a POST request, values are added to header - c#

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)

Related

C# RestSharp Returning Blank Request

i'm new to C# and trying to use RestSharp to make an API request to Pay360
I have the request working in PostMan perfectly however when i add that to my project nothing appears in the console.
I have even tried here https://dotnetfiddle.net/obAJSf
can anyone point me in the right direction as to why i cant see the json response in the console

Why C# .Net Core RestClient return cryptic response. Postman returns readable json

I have a Postman call that successfully returns readable response, such as:
Now, I want to execute the same call via C# and .Net Core RestClient. Postman has an option to generate a code snippet for different languages, here is an example for C#:
But when executing it, within my C# app, I receive an error with response, instead of the same Postman response with valid result:
How come?
Unless I'm grossly misunderstanding, you're getting xml when you want json and the headers visible in screenshots are the only headers on the request?
It sure looks like you want to specify the Accept header.
Accept:application/json
Further Reading
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept

Curling eWay from a terminal yields different response to curling from IIS

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.

Error 406 Not Acceptable JSON

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

Passing multiple request headers to libcurl.net

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.

Categories