Problems with Visual Studio c# and Restclient httpwebrequests / Postman - c#

I try to make some httpwebrequests with VS C#. The API on the server works with GET and PATCH requests. First i have tested every request with Postman and everything works fine. Then i try to test it in VS (Export C# Restapi and also with httpwebrequest. The result is that the GET requests works fine but on the PATCH requests i get not response (Timeout). There is no exception. It looks like the server is just waiting.
The RestApi Code from Postman:
var client = new RestClient("https://192.168.10.13/Service.dll/v1/terminal");
client.Timeout = -1;
var request = new RestRequest(Method.PATCH);
request.AddHeader("api_auth", "123456");
request.AddHeader("Content-Type", "application/json");
var body = #"{" + "\n" + #" ""status"": ""service""" + "\n" + #"}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
The client hangs in Line 11 while waiting on a response. After the timeout the response is empty.
Is it possible to capture the whole request to see whats different between Postman and VS?
Could it be a problem with the validation of the server certificate. I have to delegate the validation to true. But this works with the GET requests.
Im still hanging like my requests. Can someone help me with this problem. I have no idea anymore.

Related

How To Get OAuth 2.0 Access Token From Postman Using C#

Im trying to make an app that allows you to upload files to dropbox.
I created upload system etc. on this app and created postman page to get Token.
My Postman Auth2.0 Page
When i try to get access token by clicking "Get New Access Token" its works like a charm!
But i dont have any idea to get this token in c# i tried to create php post function but its return this page
my code is this i think im taking wrong url or something please help;
var client = new RestClient("https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=<clientid>&redirect_uri=https%3A%2F%2Foauth.pstmn.io%2Fv1%2Fbrowser-callback");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer <author. code>");
request.AddHeader("Content-Type", "text/plain");
request.AddHeader("Cookie", "locale=en; t=<t>; __Host-js_csrf=<auto-code>; gvc=<gvc>");
var body = #"\";
request.AddParameter("text/plain", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
print(response.Content);
I get this code by taking the link and clicked code and converted to c# on postman website https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=<clientid>&redirect_uri=https%3A%2F%2Foauth.pstmn.io%2Fv1%2Fbrowser-callback

REST request getting blocked?

Using RestRequest to a REST endpoint (Asana). Some machines get the request returned properly and some don't get anything. Doing more troubleshooting I think that the machines that do not get responses have IE as their default browser.
If I test the url manually in a Chrome browser I get the response. If I test in the IE browser then I get a "Do you want to Open or Save" prompt. I=My theory is that an IE setting of somekind is blocking the request with this question and since it is an app it just blocks.
My code is simple:
var client = new RestClient(#"https://app.asana.com/api/1.0/workspaces/99999999999999/tasks/search);
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Bearer " + asanaToken);
IRestResponse response = client.Execute(request);
How can I get around this?

Postman C# code snippet working on postman and not in app

I have an XML file that is sent as form-data to a remote server. When I use postman, I receive the response that I was supposed to receive.
Postman response
Then I use the code snippet generator to port the call to my C# app, and when I run on the app it does not work.
C# response
The code snippet is:
var client = new RestClient("http://172.20.27.1/xxxxxx/xxxxxx");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "1d9df75d-2c8f-4c92-b861-9f4291145846");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"Metas\"; filename=\"C:\\Users\\agufprad\\Desktop\\Pepillo\\FLEX_REMAN_CfgRec_Final_00-00-00-4D_20190311-102223_1010039188_7000AHB.xml\"\r\nContent-Type: application/xml\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Is there anything I am not seeing here?
Can you export + upload the postman collection. That would help a lot.
A couple of things I see off the bat
1) You have a postman token header - why do you need this in C#?
2) Does none multi-part data work?
3) Are you sure content type is supposed to have the boundary data?
4) There is no file associated with the snippet. Pretty sure you can't just AddParameter multi-part data. Look for AddFile, which may be your issue.
5) Does an example off the web work? Currently this looks like it would be a malformed multi-part request
Thanks to everyone for the responses, gave an idea of where to look.
Finally i added a header on the post method request.AddHeader("Accept", "text/plain");
And added the file request.AddFile(fileName, Properties.Settings.Default.SaveRoute + fileName);
Thanks a lot!
I would advice you remove any content-length param, postman-token, cookie param generated from the code generator on Postman, and it should work as its own specific request.
Remove
request.AddHeader("Postman-Token", "1d9df75d-2c8f-4c92-b861-9f4291145846");

Postman to C#(Restsharp) code snippet issue

I am feeling pretty stupid right now I can not figure this out. I am using postman to generate some C#(Rest sharp) to post a file to a restAPI.
In postman it works file the code snippet that it generates for C# does not work. The response code shows an internal server error in C#. I can not figure out where the problem is.
Here is the code snippet.
client = new RestClient("https://login.mydgsi.ca/WebAPI/Attachment?aboutType=Candidate&referenceID=1314180%20&attachmentTypeID=Resume&Name=Resume&expirationDate=1900/01/01&Note=Candidate%20REeume");
request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "b339e68d-9257-44eb-8698-1f3f0c86ebfe");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Bearer " + secruityToken.access_token);
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"\"; filename=\"C:\\Users\\jbungay\\Documents\\MyJabberFiles\\ddemchuk#domain.not.set\\3vertical Number 1 Resume.docx\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Here is fiddler shows on the postman post. I am new to Fiddler and Postman.
Screen of the inspector from Fidder
Try changing "Postman-Token" to just "token". This worked for me on another project. Keep in mind the request parameters change with every request.

RestSharp code generated form Postman does not work

I am testing the QuickPay API in Postman and everything works fine. When I use the Postman Code Generator to generate a RestSharp snippet of the succesful result, the code does not return a status 200 against the API, and nothing happens.
I have posted the generated code her (altered the auth code though)
var client = new RestClient("http://api.quickpay.net/subscriptions/18612/recurring?Accept-Version=v10&id=18427612&amount=9900&order_id=test1234");
var request = new RestRequest(Method.POST);
request.AddHeader("postman-token", "145e85d9-0e36-4e6a-2742-b36fb3dccadb");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("accept-version", "v10");
request.AddHeader("authorization", "Basic OjczYWY3NzlmZDYzMjIxNGUx33yYj5266ZkO222Z322221M34334433434344ZWY=");
IRestResponse response = client.Execute(request);
Any help is very much appreciated.
Best regards
I found out that instead of posting parameters into the url as querystring parameters, I should post them as parameters into the reqeuest method. No need for the Postman token nor the cache-control headers. And importantly, the url should be https://api.quickpay.net/subscriptions/18612/recurring

Categories