-1

I am going to generate e-invoice from C# code using put method.I have tested e-invoice api in sandbox environment using postmen tool.It works fine as per our requirement.I would like to know how to pass API Header information and body information from C# Code

Header :-

Content-Type : application/json
owner_id: zxererer45454545_4545456
gstin : 29AAFCD5862R000

Body :-
[
  {
    "transaction": {
      "Version": "1.03",
      "TranDtls": {
        "TaxSch": "GST",
        "SupTyp": "B2B",
        "RegRev": "Y",
        "EcmGstin": null,
        "IgstOnIntra": "N"
      },
      "DocDtls": {
        "Typ": "INV",
        "No": "AS/20/0009",
        "Dt": "08/09/2020"
      },
      "SellerDtls": {
        "Gstin": "29AAFCD5862R000",
        "LglNm": "K.H Exports India Private Limited",
        "TrdNm": "K.H Exports India Private Limited",
        "Addr1": "142/1,Trunk Road",
        "Addr2": "142/1,Trunk Road",
        "Loc": "Perumugai",
        "Pin": "560037",
        "Stcd": "29",
        "Ph": "04162253164",
        "Em": "edp.kharind@khindia.com"
      },
]

I am getting an error message when I use below code,Please check attached screenshot

screenshot error message

ppc1
  • 1
  • 2

2 Answers2

1

You can use HttpClient to make request to an API in C#. Below is a sample code

public async Task<TResponse> SendPutRequestAsync<TRequest, TResponse>(TRequest data, string url, string accessToken = null)
        {
var httpClient = new HttpClient
{
BaseAddress = "BaseAddress of your API"
};

            if (!string.IsNullOrWhiteSpace(accessToken))
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                httpClient.DefaultRequestHeaders.Add("HeaderKey", "HeaderValue");

            var response = await httpClient.PutAsJsonAsync(url, data);

            if (response.IsSuccessStatusCode)
                return JsonConvert.DeserializeObject<TResponse>(await response.Content.ReadAsStringAsync());
            else
                throw new HttpRequestException(response.ReasonPhrase);
}

This is the extension add data in the body of the request.

 public static class HttpClientExtensions
    {

        public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient httpClient, string url, T data)
        {
            var dataAsString = JsonConvert.SerializeObject(data);
            var content = new StringContent(dataAsString);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return httpClient.PutAsync(url, content);
        }
}
Purushothaman
  • 519
  • 4
  • 16
0

Here the response is of JSON format. You can use the standard package Newtonsoft.Json dll which will be available to download in the nuget server.

After referencing the Newtonsoft.json to your project do like

JObject jobject = JObject.Parse(response);

JObject header = jobject["Header"];

JObject body = jobject["Body"];

and now you have Header and Body as separate JObject. If you want to fetch any header or body item just do same as like above.

ex to get "gstin" from header :

string gstin = header["gstin"];

for better clarity read on Newtonsoft.Json documentation for how to parse a json string.

Mahesh Anakali
  • 344
  • 1
  • 8