简体   繁体   中英

The remote server returned an error: (400) Bad Request in C# Code

I am trying to pass Json data to an API using PUT method via C# code, but I get an error

The remote server returned an error: (400) Bad Request

But it's working fine when I execute the same API call in Postmen. Please check my source code and JSON data and advise how to resolve this issue.

Source code:

using (var client = new WebClient())
{
    client.Headers.Add("taxable_entities", "<Entity_Id>)");
    client.Headers.Add("X-Cleartax-Auth-Token", "<Auth_Token>");
    client.Headers[HttpRequestHeader.ContentType] = "application/json";

    string url = "https://ewbbackend-preprodpub-http.internal.cleartax.co/gst/v0.1/taxable_entities/<Entity_Id>/ewaybill/GLD23985?activity_type=GENERATE_EWB";

    string res = client.UploadString(url, "PUT", DATA);
}

JSON data:

{
  "id": "GLD23985",
  "transaction_date": "26/10/2020",
  "source": "USER",
  "document_number": "BQ/20/0251",
  "type": "OUTWARD",
  "transport_mode": "ROAD",
  "dispatch_from_state": "HARYANA",
  "sub_supply": "Supply",
  "distance": "90",
  "vehicle_number": "TN32N1049",
  "document_type": "Tax Invoice",
  "seller": {
    "address1": "142/1,Trunk Road",
    "address2": "Perumugai",
    "city": "Via Vellore",
    "gstin": "29AEKPV7203E1Z9",
    "name": "K.H Exports India Private Limited",
    "state": "HARYANA",
    "zip_code": "121102"
  },
  "receiver": {
    "address1": "4/74, VOC Street, Seenerkuppam Village, ",
    "address2": "Poonamalle, Chennai 600 056",
    "city": "",
    "gstin": "33AAACR1714R1ZA",
    "name": "KH EXPORTS INDIA PVT.LTD. (LGD)",
    "state": "TAMIL NADU",
    "zip_code": "600003"
  },
  "consignee": {
    "city": "",
    "state": "TAMIL NADU",
    "zip_code": "600003"
  },
  "line_items": [
    {
      "cess_rate": "0",
      "cess_val": "0",
      "cgst_rate": "0",
      "cgst_val": "0",
      "description": "STYLE;91311 COLOUR;SVFD7 BELT & PA",
      "gst_code": "4203",
      "igst_rate": "28.00",
      "igst_val": "16800.000000",
      "item_code": "STYLE;91311 COLOUR;SVFD7 BELT & PA",
      "quantity": "3.00",
      "serial_number": "1",
      "sgst_rate": "0",
      "sgst_val": "0",
      "taxable_val": "600.0000",
      "unit_of_measurement": "NUMBERS"
    },
    {
      "cess_rate": "0",
      "cess_val": "0",
      "cgst_rate": "0",
      "cgst_val": "0",
      "description": "STYLE;91307 COLOUR;ABFD2 BELT & PA",
      "gst_code": "4203",
      "igst_rate": "28.00",
      "igst_val": "16800.000000",
      "item_code": "STYLE;91307 COLOUR;ABFD2 BELT & PA",
      "quantity": "3.00",
      "serial_number": "2",
      "sgst_rate": "0",
      "sgst_val": "0",
      "taxable_val": "600.0000",
      "unit_of_measurement": "NUMBERS"
    }
  ]
}

Postmen response:

{
    "errors": {
        "err_1": {
            "code": "BAD_REQUEST",
            "message": "Cannot update an EWay Bill which is already generated and has no transporter updates made in it.",
            "error_group_code": 0,
            "error_id": 0
        }
    },
    "error_sources": {
        "ewb_status": {
            "error_refs": [
                "err_1"
            ]
        }
    }
}

You could create a Wrapper class to return your api responses to the application(s). For example:

public class ApiResult<T>
{
     public T Data { get; set; }
     public HttpStatusCode StatusCode { get; set; }
     public string Message { get; set; }
     public bool Success { get; set; }
}

And when returning your data from the controllers have something like:

public ApiResponse<YourObjectDataType> GetData(int parameter1) 
{ 
       // does some work
       return new ApiResponse<YourObjectDataType>
       {
            Success = true,
            Data = new YourObjectDataType { // object properties... }
       };
}

This is not a code issue, the API is responding telling you what is wrong.

"Cannot update an EWay Bill which is already generated and has no transporter updates made in it"

You need to make sure you satisfy that condition.

Rather worryingly you seem to have posted an auth token (X-Cleartax-Auth-Token) in plain text in this question? It would be wise to remove it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM