Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1. //https://api.mouser.com/api/docs/ui/index#/SearchApi/SearchApi_SearchByKeyword
  2.  
  3.  public string MouserCreateApiJson(string transistor) {
  4.             string json = "{ \"SearchByKeywordRequest\": { \"keyword\": \""+transistor+"\", \"records\": 0, \"startingRecord\": 0, \"searchOptions\": \"string\", \"searchWithYourSignUpLanguage\": \"string\" }}";
  5.            // json.Replace("{0}", transistor);
  6.  
  7.            return json;
  8.         }
  9.  
  10.         public string MouserApiRequest(string json) {
  11.             try
  12.             {
  13.                 string content = string.Empty;
  14.                 string ResponseStatus;
  15.                 string StatusCode;
  16.                 string StatusDescription;
  17.                 ServicePointManager.SecurityProtocol =  SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
  18.                 string url = _mouser_api_url + "api/v1/search/keyword?apiKey=" + _mouser_api_key;
  19.                 var client = new RestClient(url);
  20.                 var request = new RestRequest(Method.POST);            
  21.                 request.RequestFormat = DataFormat.Json;
  22.                 request.AddHeader("Content-Type","application/json");          
  23.                 request.AddJsonBody(json);
  24.  
  25.                 client.ExecuteAsync(request, response =>
  26.                 {
  27.                     content = response.Content;
  28.                     ResponseStatus = response.ResponseStatus.ToString();
  29.                     StatusCode = response.StatusCode.ToString();
  30.                     StatusDescription = response.StatusDescription;
  31.                 });
  32.                 return content;
  33.             }
  34.             catch (Exception ex) {
  35.                 Console.WriteLine(ex.Message);
  36.                 return string.Empty;
  37.             }
  38.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement