I have trouble posting json object from javascript

67 views Asked by At

I have the react snippet:

const model = {
    Code: 'somecode',
    Elements: elements,
    ...more
}

apiPost(url, false, model)
        .then(
)

Then:

export function apiPost(url, addAuth, json) {
    return apiFetch('post', url, addAuth, json)
}



function apiFetch(method, url, addAuth, json, file) {

let opts = {
    method: method,
    headers: {}
}

if (addAuth) {
    opts.headers = { ...opts.headers, ...authHeader() }
}

if (json) {
    opts.headers = { ...opts.headers, ...{ 'content-type': 'application/json' } }
    opts.body = JSON.stringify(json);
}

if (file) {
    opts.body = file;
}

return fetch(baseUrl() + url, opts).then(handleResponse)

}

I am receiving it at the api side:

    [HttpPost]
    public async Task<JsonResult> Post([System.Web.Http.FromBody] SurveyEntry model)
    {
        try
        {
            ...
        }
        catch (Exception ex)
        {
            ...
        }
    }

but the model comes null.

I am puzzled why this happens.

Any suggestions what I am doing wrong?

0

There are 0 answers