File upload to zoho contacts in C#.Net - c#

I'm trying to upload attachments to Zoho CRM contacts using RestSharp but not able to do it. Every time I get response "Bad Request: the request doesn't contain any file". I'm able to upload files to zoho crm contacts using postman but no success through C#.Net application.
I'm using the following code for my C#.Net application. Can you please guide me what wrong I'm doing.
var client = new RestClient("https://www.zohoapis.com/crm/v2/contacts/{contactID}/Attachments");
var request = new RestRequest(Method.POST);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Authorization", "Zoho-oauthtoken <access-token>");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------`enter code here`WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; path=\"E:\\10.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Thanks in advance.

Related

Instagram Basic Display API: RestSharp Invalid authorization code response

Trying to to add an Instagram feed to my ASP.NET C# web app using this and this documentation
The issue is that I always get the Invalid authorization code error message when trying to get the short lived token via RestSharp call to https://api.instagram.com/oauth/access_token
In Postman the response is successful, is there something wrong with the RestSharp API call?
RestSharp API call
var client = new RestClient(_shortLivedTokenRequestString);
var request = new RestRequest(Method.POST);
request.AlwaysMultipartFormData = true;
request.AddParameter("client_id", _Instagram_App_Id, ParameterType.RequestBody);
request.AddParameter("client_secret", _Instagram_App_Secret, ParameterType.RequestBody);
request.AddParameter("grant_type", "authorization_code", ParameterType.RequestBody);
request.AddParameter("redirect_uri", _Instagram_App_Redirect_Uri, ParameterType.RequestBody);
request.AddParameter("code", authorizationCode, ParameterType.RequestBody);
IRestResponse<GetShortLivedTokenResponse> response = client.Execute<GetShortLivedTokenResponse>(request);
response: {"error_type": "OAuthException", "code": 400, "error_message": "Invalid authorization code"}
response object
Postman request/response
Postman response headers
I recently had a similar problem that involved not removing the #_ added at the end of the code parameter like described here.
I don't know if thats your case but maybe it helps
My problem was that I did not remove the #_ as described in the instructions:

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");

Exception with OAuth2.0 service call

I write some code in Microsoft Visual Studio .NET with C#. I want to call one rest service using RestSharp 106.3.1 library that i get from Microsoft NuGet.
this is a banking service that has no argument and list all customer debit card.
I get bearer access token from another service and I can call getCards service with System.Net.Http.HttpClient successfully. but I want to use RestSharp.
my code is:
var client = new RestSharp.RestClient(webSrvUri);
client.Authenticator = new RestSharp.Authenticators.OAuth2AuthorizationRequestHeaderAuthenticator(accessToken, "Bearer");
var request = new RestSharp.RestRequest(RestSharp.Method.POST);
request.RequestFormat = RestSharp.DataFormat.Json;
request.Parameters.Clear();
request.Resource = "getCards";
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
RestSharp.IRestResponse response = client.Execute(request);
but this code generate following content as result:
{"timestamp":"2018-07-03 10:16:49","status":500,"error":"Internal
Server
Error","exception":"com.fasterxml.jackson.databind.JsonMappingException","message":"No
content to map due to end-of-input\n at [Source: ; line: 1, column:
0]","path":"/core/getCards"}
what do I do?
Thanks in advance.

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.

How to get results from a Jira Rest API request?

I want to be able to take JSON data that I get from a RestRequest to the Jira Rest API and put that information into a graph. However I am unable to even write the response data to the console. I know what information I should be getting because when I make the request in postman I get a response with the expected data in it.
Here is my code that I generated directly from postman to hopefully replicate what postman is doing inside of visual studio.
How do I get the results from the Jira Rest API request that I am making?
I'm thinking it is a relatively simple fix, but I am just not that familiar with interacting with this API.
var client = new RestClient(myCompanyURL/rest/api/2/issue/NID-3.json");
var request = new RestRequest(Method.GET);
//request.AddHeader("postman-token", "tokenGeneratedFromPostman");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("authorization", "Basic myEncodedInformation");
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\n\t\"fields\": {\n\t\t\"project\":\n\t\t{\n\t\t\t\"key\":\"NID\"\n\t\t},\n\t\t\"summary\": \"TEST\",\n\t\t\"description\": \"Test Description\",\n\t\t\"issuetype\": {\n\t\t\t\"name\":\"Task\"\n\t\t}\n\t}\n}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response);

Categories