[SOLVED] How to send body in post using app.request.post

Here’s my code:

Blockquote

    var params = {
        "otp": payTmOTPval.otp,
        "state": payTmOTPval.state
    };
       var headers = {
            contentType: 'application/json',
            Authorization: 'Basic ' + clientidsecretbase64
        }

        app.request({
            method: 'POST',
            url: `${payTMUrl}signin/validate/otp`,
            headers: headers,
            params: JSON.stringify(params),
            success: function(data) {
                console.log(data)
            },
            error: function(err) {
                console.log(err)
            }
        })

Blockquote

The problem here is, when I submit it it auto attach “application/x-www-form-urlencoded” in the header and the reason my request fails. I want data to not go as params but as body raw JSON(application/json) and header shouldn’t not auto include “application/x-www-form-urlencoded”.

kindly help me

Set headers: https://framework7.io/docs/request.html#request-setup

PostJSON: https://framework7.io/docs/request.html#postjson

Thanks for such instant reply.

I did try both of the techniques and I had a issue.

setting headers using app.request.setup setup’s the header for whole app which I don’t want.

and I am not able to understand how to setup header in app.request.postJson().

Can you please help me with how to setup header in app.request.postJson ?

Check the API http://framework7.io/docs/request.html#api there is no params option, should be data. If you need to send it as JSON then also add these parameters:

contentType: 'application/json',
processData: false,
1 Like

Thanks it worked,

I was trying to pass

contentType: 'application/json'

inside header and it was not working. Did it this way and it worked.

Where to pass contentType. I have been stuck here since 10 hours.