反应js获取请求给出错误的请求400代码

时间:2019-12-26 12:26:45

标签: c# reactjs api asp.net-core asp.net-web-api

我有一个API,该API有时会给出400个Bad Request响应状态代码,而有时它会完美地执行200个状态代码。我检查了请求正文,这很好。在Chrome网络标签中诊断请求后

响应头: content-type: application/problem+json; charset=utf-8

响应

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "Bad Request",
    "status": 400,
    "traceId": "|58ab48-4a300ddcea45."
}

即使请求体是完美的,为什么也会发生这种情况,API却给出了400个错误的请求

export const POST = (endpoint, body) => {
  //JS code
    if (endpoint != null) {
        return fetch(endpoint, {
            method: 'POST',
            async: false,
            headers: {
                'content-type': 'application/json',
                'X-CSRF-TOKEN': getCookies("CSRF-TOKEN")
            },
            body: JSON.stringify(EncryptReq(body))
            //body: JSON.stringify(body),
        }).then(res => res.json()).then(data => data)
            .catch(err => {

                // localStorage.clear();
                return serviceUnavaile(endpoint)
            });
    }
}

加密前的请求

{
 EmailID: "myemail",
MobileNo: "1234567891",
CRNNo: "12345678",
Mode: 0,
For: "UserVerfication",
Gateway: 1,
Name: "MyName",
DName: "DeviceName"
}

我正在请求中发送加密的字符串

{"body":"dd/UB7D+2924apLKCu/3elC2XjO96i60JyicCCPvy8eJoQTXhudJ67Y0XIg0We+DKcFpYASvO3j5AmQyIcQCUs4b61XoQR/QLDtyCSePHtevxoEqyAnsmtpIOTWHbrablE4FI1psxtwuzRsmiEO+ABMOj85l0oQCOFAm41Ihnafy/3nHYMv8oxeXYzg57i6Mu8Ic6UUWl1X85v1MYl1++tQ3t2cHKMzG7f/s67gA05z+zRtw/imaDYWTaNGH2sF8J8AInz5UeJUAwxCeYIlx58B6J0v1Ye5tpABABS7AlKPG4Rzg8b2QIXk+J2hUWstB2/iTeJag1BI5AzgGtG825bDGp5g68Fn8lg8HjRPf7R0aVV2Imkfv1jZ6AgSGO4Kl09BRY59cAD36tNHiLlxpIiBJMR2gzCnV/v05iJo+ahoAD1oqhr7WW7zmeoPvG/hrG/507K8e9s1lOcky3A7PT/38BBuEqf5eD+wyRPww3Og="}

我具有解密加密字符串的资源过滤器

public class ResourceFilters: IResourceFilter{
   public void OnResourceExecuting(ResourceExecutingContext context){
    //I decrypt the string here   
 }  
}

我有c#控制器

[HttpPost("[action]")]      
  public async Task<string> verifydata(user model){
}


public class user 
{
    public string EmailID { get; set; }
    public string MobileNo { get; set; }
    public string CRNNo { get; set; }
    public int Mode { get; set; }
    public string For { get; set; }
    public int Gateway { get; set; }
    public string Name { get; set; }
    public string DName { get; set; }
}

0 个答案:

没有答案