Alice52 / c-tutorial

The repository is about c, including c, csharp, cpp.
MIT License
0 stars 0 forks source link

[http call] api call #6

Closed Alice52 closed 4 years ago

Alice52 commented 4 years ago

core: so can get request detail by fiddler

  1. URI: may lead to 404 http code
  2. Header: may lead to 400 http code
  3. Method: may lead to 405 http code
  4. Body: may lead to 400 http code

this issue is trace Http Call processor and method

  1. HttpClient, can using Named Client

    • 404:

      • this URI is specify, it's BaseAddress must be endwith /
      • and relative uri must not be start with /
    • 400

      • DateTime is strange in .net framework, it must be formatted like this /Date(12346464)/
      • so must SerializeObject by DateFormatHandling.MicrosoftDateFormat
    • 500

      • can call target service success, but service error
    • sample

    // startip.cs
    services.AddHttpClient(ClientsConstants.TABLE_SERVICE_CLIENT, client =>
    {
        client.BaseAddress = new Uri(TableServiceConfig.GetSection("BaseUrl").Value);
        // this will get Misused header name error
        // client.DefaultRequestHeaders.Add(RequestHeaders.CONTENT_TYPE, RequestHeaders.JSON_CONTENT);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.Timeout = TimeSpan.FromMilliseconds(Convert.ToInt32(TableServiceConfig.GetSection("Timeout").Value));
    });
    
    // service class
    var jsonBody = JsonUtil.SerializeObject(new AutoRollRequestBody(tableId, gamingDay), DateFormatHandling.MicrosoftDateFormat);
    
    var client = _clientFactory.CreateClient(ClientsConstants.TABLE_SERVICE_CLIENT);
    StringContent content = new StringContent(jsonBody);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var response = client.PostAsync(ServiceURLConstants.TABLE_SERVICE_PATH, content).Result;
    
    response.EnsureSuccessStatusCode();
    return JsonUtil.DeserializeObject<ReceiveTableInventory>(response.Content.ReadAsStringAsync().Result);
  2. RestClient: same issue with HttpClient

    • request body: addJsonBody()

    • sample

    public static IRestResponse Post(string requestPath, int timeout, object jsonBody, IDictionary<string, string> headerParams, string clientPath)
    {
        var request = new RestRequest(requestPath, Method.POST) { Timeout = timeout };
        request.AddHeader(RequestHeaders.CONTENT_TYPE, RequestHeaders.JSON_CONTENT);
        request.RequestFormat = DataFormat.Json;
    
        if (headerParams != null && headerParams.Count > 0)
        {
            foreach (string key in headerParams.Keys)
            {
                request.AddHeader(key, headerParams[key]);
            }
        }
    
        if (jsonBody != null)
        {
            request.AddJsonBody(jsonBody);
        }
        var client = new RestClient(clientPath);
        return client.Execute(request);
    }

reference

  1. https://d-fens.ch/2014/04/12/httpclient-and-how-to-use-headers-content-type-and-postasync/
  2. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-2.2
Alice52 commented 4 years ago

8