Base solution for your next web application
Open Closed

Parameters not being bound from Angular Http Post Body #4566


User avatar
0
buddhit created

Hello, were having great difficulty getting the parameters to be successfully bound after submitting a post from the HttpClient in our project.

Our interceptor to handle the cookies and auth,

@Injectable()
export class AuthConn implements HttpInterceptor {

    private _cookieService;

    constructor(private cookieService: CookieService) {
        this._cookieService = cookieService;
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        if (this._cookieService.check('Abp.AuthToken')) {

            // We have our auth token..
            let bearerToken = this._cookieService.get('Abp.AuthToken');
            // Clone the request to add the new header.
            const authReq = req.clone({headers: req.headers.set('Authorization', 'bearer ' + bearerToken).set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')});
            // Pass on the cloned request instead of the original request.
            return next.handle(authReq);

        } else {
            // @todo This should sign the user out of the application...
            console.log('here');
        }

    }
}

Our endpoint request:

console.log(gridSettingsObject);

        // Finally go and update the server with our new item.
        this.http.post('http://localhost:22742/api/services/app/UserGridSetting/SaveUserGridSettings', {'jsonData': JSON.stringify(gridSettingsObject)}).subscribe((d: any) => {
            console.log(d);
        });

And finally our C# endpoint:

public async Task<int> SaveUserGridSettings(string jsonData)
        {
            var userId = AbpSession.GetUserId();
            var setting = await _repository.FirstOrDefaultAsync(s => s.UgsUserId == userId);

            if (setting == null)
            {
                setting = new UserGridSettingModel();
            }

            setting.UgsColSetting = jsonData;
            setting.UgsUserId = userId;
            setting.CreatorUserId = userId;
            setting.DeleterUserId = 0;

            return await _repository.InsertOrUpdateAndGetIdAsync(setting);

        }

jsonData just simply never makes it into the parameters but the endpoint is reachable. Whats even stranger is that when doing x-www-form-encoding or multipart in postman the parameter is found. Though changing the content type within our interceptor did not provide the same results so we are stuck..


1 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    as far as I see you are using http post to send data.

    this.http.post(..)
    

    read the doc about nSwag. You need to use the proxy classes in Angular typescript. to generate the classses for the app services run

    nswag/refresh.bat
    

    See <a class="postlink" href="https://www.aspnetzero.com/Documents/Development-Guide-Angular#nswag">https://www.aspnetzero.com/Documents/De ... ular#nswag</a>